b = ["food", "stuffing", "hobbitses"]
y = ["ing", "es", "s", "ly"]
def stemming():
for i in range(len(b)):
if b[i].endswith(tuple(y)):
b[i] = b[i] - #???
print b
def remove_stems(word, stems):
for stem in stems:
if word.endswith(stem):
return word[:-len(stem)]
else:
return word
b_without_stems = [remove_stem(word, stems) for word in b]
def stemming(strings, endings):
for i, string in enumerate(strings):
for ending in endings:
if string.endswith(ending):
strings[i] = string[:-len(ending)]
continue
你需要知道找到了哪个结尾,所以你需要一次检查一个,而不是一次检查所有的结尾。一旦你找到了一个结尾,你可以用切片把它切掉。在
更好的方法是使用正则表达式:
^{pr2}$然后,您可以使用列表理解轻松地进行词干分析:
我建议将词干删除分离成它自己的函数,然后对整个列表使用列表理解或单独的函数。这里有一种方法
假设你想去掉找到的第一个后缀,这个就可以了
相关问题 更多 >
编程相关推荐