Porter Stemmer Algorithm 不返回预期输出?当修改为 def

我正在使用 PorterStemmerPython 版本

Porter 词干提取算法(或称‘Porter 词干器’)是一种用于从英语单词中移除常见的形态和屈折词尾的过程。其主要用途是在设置信息检索系统时,作为术语标准化过程的一部分。

对于以下内容…

你需要做的另一件事是将每个单词简化为其词干。例如,单词 singsingssinging 都有相同的词干,即 sing。有一种被广泛接受的方法来做到这一点,称为 Porter 算法。你可以从 http://tartarus.org/martin/PorterStemmer/ 下载实现它的工具。

我已经修改了代码…

if __name__ == '__main__':    p = PorterStemmer()    if len(sys.argv) > 1:        for f in sys.argv[1:]:            infile = open(f, 'r')            while 1:                output = ''                word = ''                line = infile.readline()                if line == '':                    break                for c in line:                    if c.isalpha():                        word += c.lower()                    else:                        if word:                            output += p.stem(word, 0,len(word)-1)                            word = ''                        output += c.lower()                print output,            infile.close()

以便从 input 读取而不是从预处理的字符串文件中读取,并返回输出。

def algorithm(input):    p = PorterStemmer()    while 1:        output = ''        word = ''        if input == '':            break        for c in input:            if c.isalpha():                word += c.lower()            else:                if word:                    output += p.stem(word, 0,len(word)-1)                    word = ''                output += c.lower()        return output

请注意,如果我将 return output 缩进到与 while 1: 相同的级别,它会变成一个 无限循环

使用示例

import PorterStemmer as psps.algorithm("Michael is Singing");

输出

Michael is

预期输出

Michael is Sing

我做错了什么?


回答:

看起来问题出在它目前没有将输入的最后部分写入 output(例如,尝试 “Michael is Singing stuff”,它应该正确写入所有内容并省略 ‘stuff’)。可能有更优雅的方法来处理这个问题,但你可以尝试在 for 循环中添加一个 else 子句。由于问题在于最后一个单词没有被包含在 output 中,我们可以使用 else 来确保在 for 循环完成时添加最后一个单词:

def algorithm(input):    print input    p = PorterStemmer()    while 1:        output = ''        word = ''        if input == '':            break        for c in input:            if c.isalpha():                word += c.lower()            elif word:                output += p.stem(word, 0,len(word)-1)                word = ''                output += c.lower()        else:            output += p.stem(word, 0, len(word)-1)          print output        return output

这个版本已经通过两个测试用例进行了广泛测试,所以显然它是无懈可击的 🙂 可能还有一些边缘情况存在,但希望这能帮你起步。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注