Skip to content

Commit d042b74

Browse files
bug fix. show major tags
1 parent 1910f10 commit d042b74

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

Frontend/tag-viewer.php

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@
136136
'</div>';
137137
$vars['navigator'] = CreateNavi([], $tag2path, $path2tag, $vars['rootDirectory'], $vars['layerName']);
138138

139+
$majorTags = GetMajorTags($tag2path);
140+
$vars['contentBody'] = CreateTagCardsElement($majorTags, $vars['rootDirectory'], $vars['layerName']);
141+
139142
// ビルド時間計測 終了
140143
$stopwatch->Stop();
141144
$vars['pageBuildReport']['times']['build']['ms'] = $stopwatch->Elapsed() * 1000;
@@ -575,22 +578,35 @@ function CreateTagGroupElement($tagGroup, $contentMap, $tagPathParts, $rootDirec
575578
}
576579
foreach($tagGroup['tags'] as $tag => $paths) {
577580
$html .= '<div class="card-wrapper">';
578-
$tagHref=CreateTagMapHREF(array_merge($tagPathParts, [[$tag]]), $rootDirectory, $layerName);
581+
$tagHref = CreateTagMapHREF(array_merge($tagPathParts, [[$tag]]), $rootDirectory, $layerName);
579582
$html .= CreateTagCard($tag, $tagHref);
580583
$html .= CreateContentListElement($paths, $contentMap);
581584
$html .= '</div><div class="splitter"></div>';
582585
}
583586
return $html;
584587
}
585588

589+
function CreateTagCardsElement($tags, $rootDirectory, $layerName) {
590+
$html = '';
591+
if(!empty($tags)) {
592+
$html .= '<div class="card-wrapper">';
593+
foreach($tags as $tag => $_) {
594+
$tagHref = CreateTagMapHREF([[$tag]], $rootDirectory, $layerName);
595+
$html .= CreateTagCard($tag, $tagHref);
596+
}
597+
$html .= '</div><div class="splitter"></div>';
598+
}
599+
return $html;
600+
}
601+
586602
function CreateContentListElement($paths, $contentMap){
587603
$html = '';
588604
foreach ($paths as $path => $_) {
589605
$content = $contentMap[$path];
590606
$parent = $content->Parent();
591-
$text=GetDecodedText($content);
592-
$href=CreateContentHREF($content->path);
593-
$title=NotBlankText([$content->title, basename($content->path)]) .
607+
$text = GetDecodedText($content);
608+
$href = CreateContentHREF($content->path);
609+
$title = NotBlankText([$content->title, basename($content->path)]) .
594610
($parent === false ? '' : ' | ' . NotBlankText([$parent->title, basename($parent->path)]));
595611
$html .= CreateContentCard($title, $text['summary'], $href);
596612
}
@@ -635,4 +651,52 @@ function CreateTagGroup($contentMap, $path2tag, $selectedTags) {
635651
}
636652
}
637653
return $tagGroup;
654+
}
655+
656+
/**
657+
*/
658+
function GetMajorTags($tag2path) {
659+
$tags = []; $nt = 0; $ts = [];
660+
foreach($tag2path as $tag => $paths) {
661+
$count = count($paths);
662+
$tags[$tag] = $count;
663+
$ts[$count] = 0;
664+
$nt++;
665+
}
666+
if($nt < 2) return [];
667+
ksort($ts); $first = key($ts); unset($ts[$first]);
668+
669+
foreach($ts as $thres => $s) {
670+
$u0 = 0;
671+
$u1 = 0; $v1 = 0 ; $n1 = 0;
672+
$u2 = 0; $v2 = 0 ; $n2 = 0;
673+
674+
foreach ($tags as $tag => $count) {
675+
if($count < $thres) { $n1++; $u1 += $count; } // class 1
676+
else { $n2++; $u2 += $count; } // class 2
677+
$u0 += $count;
678+
}
679+
$u0 /= ($n1 + $n2);
680+
$u1 /= $n1; $u2 /= $n2;
681+
682+
foreach ($tags as $tag => $count) {
683+
if($count < $thres) { $v1 += ($count - $n1) * ($count - $n1); } // class 1
684+
else { $v2 += ($count - $n2) * ($count - $n2); } // class 2
685+
}
686+
$v1 /= $n1; $v2 /= $n2;
687+
688+
$vw = ($n1 * $v1 + $n2 * $v2) / ($n1 + $n2);
689+
$vb = ($n1 * ($u1 - $u0) * ($u1 - $u0) + $n2 * ($u2 - $u0) * ($u2 - $u0)) / ($n1 + $n2);
690+
$ts[$thres] = $vb / $vw;
691+
}
692+
693+
asort($ts); $thres = array_key_last($ts);
694+
695+
$majorTags = [];
696+
foreach ($tags as $tag => $count) {
697+
if($count >= $thres) { $majorTags[$tag] = $count; }
698+
}
699+
700+
arsort($majorTags);
701+
return $majorTags;
638702
}

Module/ContentsDatabase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,7 @@ function SetContent($contentPath)
479479

480480
} // End 各行ごとの処理
481481
} // End Header処理
482-
483-
if(strrpos($this->summary, '\n', -1) !== false) {
482+
if($this->summary !== '' && substr($this->summary, -1) === "\n") {
484483
// summaryの最後の改行を取り除く
485484
$this->summary = substr($this->summary, 0, -1);
486485
}

0 commit comments

Comments
 (0)