在 php 中使用 PorterStemmer 后的输入数组和不同的输出


Input array and different output after using PorterStemmer in php

我正在使用PorterStemmer来词干,就像"工作"一样,在调用PorterStemmer类后它将是"工作",它对我有用。

但是我想去一个句子,例如,如果我把这个句子给我的代码:

以前

"我在踢足球,努力工作,因为我有足够的力量"

"我踢足球,努力工作,因为我有足够的力量"

似乎我在php中使用"foreach"循环时遇到了问题,因为我的代码只对一个单词进行词干提取。

我的代码:

$str=('I am playing football and working hard because I have powerful enough');
$parts = explode(' ', $str);
$word_to_stem = $str;
$stem = PorterStemmer::Stem($word_to_stem);

现在,$parts将我的句子作为一个数组包含在内,我如何去读每个单词,然后将新句子放入名为 $str2 的新变量中

所以如果你想去掉每个单词:

$str=('I am playing football and working hard because I have powerful enough');
$parts = explode(' ', $str);
$stemmed_parts = array();
foreach ($parts as $word) {
    $stemmed_word = PorterStemmer::Stem($word);
    $stemmed_parts[] = $stemmed_word;
}
echo implode(' ', $stemmed_parts);