我想找到一組單詞沒有之前知道他們雖然 與功能,我需要什麼
之前提供給他們試試這個:
mb_internal_encoding('UTF-8');
$joinedExcerpts = implode(".\n", $excerpts);
$sentences = preg_split('/[^\s|\pL]/umi', $joinedExcerpts, -1, PREG_SPLIT_NO_EMPTY);
$wordsSequencesCount = array();
foreach($sentences as $sentence) {
$words = array_map('mb_strtolower',
preg_split('/[^\pL+]/umi', $sentence, -1, PREG_SPLIT_NO_EMPTY));
foreach($words as $index => $word) {
$wordsSequence = '';
foreach(array_slice($words, $index) as $nextWord) {
$wordsSequence .= $wordsSequence ? (' ' . $nextWord) : $nextWord;
if(!isset($wordsSequencesCount[$wordsSequence])) {
$wordsSequencesCount[$wordsSequence] = 0;
}
++$wordsSequencesCount[$wordsSequence];
}
}
}
$ngramsCount = array_filter($wordsSequencesCount,
function($count) { return $count > 1; });
我假設你只想重複一組單詞。 的var_dump($ngramsCount);
的輸出中是:
array (size=11)
'i' => int 3
'i love' => int 2
'love' => int 2
'cheap' => int 3
'cheap red' => int 3
'cheap red apples' => int 3
'red' => int 5
'red apples' => int 5
'apples' => int 6
'are' => int 2
'my' => int 2
的代碼可以被優化,以,例如,使用較少的存儲器。
爲了繼續,我會查找n-gram算法,然後決定哪個適合在這個數據集上實現。第一次電話:[關於N-grams的維基百科](http://en.wikipedia.org/wiki/N-gram)。 – 2014-10-19 22:14:58
感謝您的建議,這是我所做的,但我需要任何解決方案或至少具體的例子,它們會給我我提供的最終輸出。 – mattspain 2014-10-20 11:42:22
你好, 這個圖書館爲你服務:https://packagist.org/packages/drupol/phpngrams 讓我知道它是怎麼回事! – 2018-02-05 20:53:04