0

我正在使用 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

4

1 回答 1

0

因此,如果您想阻止每个单词:

$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);
于 2016-03-17T13:07:15.260 回答