我想找到一组单词没有之前知道他们虽然 与功能,我需要什么
之前提供给他们试试这个:
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