|
41 | 41 |
|
42 | 42 | // $suggestions = []; |
43 | 43 |
|
44 | | -// "<title> <parent.title>" で検索 |
| 44 | +// "<title> <parent.title> <tag1> <tag2> <tag3> ..."で検索 |
45 | 45 | // ただし, parent は rootではない |
46 | | -$titleQuery = NotBlankText([$currentContent->title, basename($currentContent->path)]) . |
| 46 | +$query = NotBlankText([$currentContent->title, basename($currentContent->path)]) . |
47 | 47 | (($parent === false || $parent->IsRoot()) ? '' : ' ' . NotBlankText([$parent->title, basename($parent->path)]) ); |
48 | 48 |
|
49 | | -$titleSuggestions = SearchEngine\Searcher::Search($titleQuery); |
50 | | -// Debug::Log($titleSuggestions); |
51 | | -// score 0.5 未満 は除外 |
52 | | -foreach($titleSuggestions as $i => $suggestion){ |
53 | | - if($suggestion['score'] < 0.5){ |
54 | | - unset($titleSuggestions[$i]); |
55 | | - } |
56 | | -} |
57 | | -// Debug::Log($workSuggestions); |
58 | | -// $suggestions = array_merge($suggestions, $workSuggestions); |
59 | | - |
60 | | -// "<tag1> <tag2> <tag3> ..." で検索 |
61 | | -$tagQuery = ''; |
62 | 49 | foreach($currentContent->tags as $tag){ |
63 | 50 | if(!in_array($tag, array('noindex', 'noindex-latest', '編集中', 'editing'))){ |
64 | | - $tagQuery .= $tag . ' '; |
| 51 | + $query .= ' ' . $tag; |
65 | 52 | } |
66 | 53 | } |
67 | | -$tagSuggestions = SearchEngine\Searcher::Search($tagQuery); |
68 | 54 |
|
69 | | -// 全タグをAND検索したとき, score 0.3 未満のものは除外 |
70 | | -foreach($tagSuggestions as $i => $suggestion){ |
71 | | - if($suggestion['score'] < 0.3){ |
72 | | - unset($tagSuggestions[$i]); |
| 55 | +$suggestions = SearchEngine\Searcher::Search($query); |
| 56 | + |
| 57 | +$terms = explode(' ', $query); |
| 58 | +$termCount = count($terms); |
| 59 | +for($i = $termCount - 1; $i >= 0; $i--){ |
| 60 | + $terms[$i] = trim($terms[$i]); |
| 61 | + if(empty($terms[$i])){ |
| 62 | + array_splice($terms, $i, 1); |
73 | 63 | } |
74 | 64 | } |
75 | | -// $suggestions = array_merge($suggestions, $workSuggestions); |
76 | | - |
77 | | -// // 重複を除外 |
78 | | -// $uniqueKeys = []; |
79 | | -// foreach($suggestions as $i => $suggestion){ |
80 | | -// if(array_key_exists($suggestion['id'], $uniqueKeys)){ |
81 | | -// unset($suggestions[$i]); |
82 | | -// continue; |
83 | | -// } |
84 | | -// $uniqueKeys[$suggestion['id']] = true; |
85 | | -// } |
| 65 | +// Debug::Log(count($terms)); |
| 66 | +// Debug::Log(0.7 / count($terms)); |
| 67 | +// Debug::Log(count($terms)); |
| 68 | +// Debug::Log(0.5 / (1+log10(count($terms)))); |
| 69 | +// Debug::Log($suggestions); |
| 70 | + |
| 71 | +// フィルタ例: |
| 72 | +// 0.3以上のもの: |
| 73 | +// 問題点: |
| 74 | +// termが増えるごとに, 限定されていき, ヒットしずらくなる |
| 75 | +// |
| 76 | +// 各termごと, 平均して7割以上類似するもの: |
| 77 | +// 0.7 / count($terms) |
| 78 | +// 問題点: |
| 79 | +// termが増えるごとに, scoreが0に近づき, ヒットしやすくなる |
| 80 | +// |
| 81 | +// 考えは, 上のままでtermsの数が増えるごとにスコアの下降を抑える |
| 82 | +// 0.5 / (1+log(count($terms))) |
| 83 | +// |
| 84 | +foreach($suggestions as $i => $suggestion){ |
| 85 | + if($suggestion['score'] < (0.5 / (1+log(count($terms))))){ |
| 86 | + unset($suggestions[$i]); |
| 87 | + } |
| 88 | +} |
| 89 | + |
86 | 90 | $childPathList = []; |
87 | 91 | if($parent !== false){ |
88 | 92 | $childCount = $parent->ChildCount(); |
|
93 | 97 | } |
94 | 98 | } |
95 | 99 | // Debug::Log($childPathList); |
96 | | -$titleSuggestions = SelectDifferentDirectoryContents($titleSuggestions, $currentContent->path, $childPathList); |
97 | | -$tagSuggestions = SelectDifferentDirectoryContents($tagSuggestions, $currentContent->path, $childPathList); |
| 100 | +// $titleSuggestions = SelectDifferentDirectoryContents($titleSuggestions, $currentContent->path, $childPathList); |
| 101 | +$suggestions = SelectDifferentDirectoryContents($suggestions, $currentContent->path, $childPathList); |
98 | 102 |
|
99 | 103 | // End 関連コンテンツの検索 ================================================= |
100 | 104 |
|
|
125 | 129 |
|
126 | 130 | $body = ''; |
127 | 131 |
|
128 | | -if(count($titleSuggestions) > 0){ |
129 | | - $body .= '<h3>タイトル「' . trim($titleQuery) . '」に関連する</h3>'; |
130 | | - $body .= CreateSuggestedContentList($titleSuggestions); |
131 | | -} |
132 | | - |
133 | | -if(count($tagSuggestions) > 0){ |
134 | | - $body .= '<h3>タグ「' . trim($tagQuery) . '」に関連する</h3>'; |
135 | | - $body .= CreateSuggestedContentList($tagSuggestions); |
| 132 | +if(count($suggestions) > 0){ |
| 133 | + // $body .= '<h3>「' . trim($query) . '」に関連する</h3>'; |
| 134 | + $body .= CreateSuggestedContentList($suggestions); |
136 | 135 | } |
137 | 136 |
|
138 | 137 |
|
139 | | -if(count($titleSuggestions) + count($tagSuggestions) > 0){ |
140 | | - $vars['contentSummary'] = '<p>「コンテンツ: <a href="' . CreateContentHREF($currentContent->path) . '">' . |
141 | | - NotBlankText([$currentContent->title, basename($currentContent->path)]) . |
142 | | - '</a>」と関連するコンテンツが, 別階層で<em>' . (count($titleSuggestions) + count($tagSuggestions)) .'件</em>見つかりました.</p>'; |
| 138 | +if(count($suggestions) > 0){ |
| 139 | + $vars['contentSummary'] = '<p><em>「' . trim($query) . '」</em>に関連するコンテンツが, 別階層で<em>' . count($suggestions) .'件</em>見つかりました.</p>'; |
143 | 140 | } |
144 | 141 | else{ |
145 | | - $vars['contentSummary'] = '<p>「コンテンツ: <a href="' . CreateContentHREF($currentContent->path) . '">' . |
146 | | - NotBlankText([$currentContent->title, basename($currentContent->path)]) . |
147 | | - '</a>」と関連するコンテンツが, 別階層で見つかりませんでした.</p>'; |
| 142 | + $vars['contentSummary'] = '<p><em>「' . trim($query) . '」</em>に関連するコンテンツが, 別階層で見つかりませんでした.</p>'; |
148 | 143 | } |
149 | 144 |
|
150 | 145 | $vars['contentBody'] = $body; |
|
0 commit comments