确定句子的时间Python

2024-06-16 09:43:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在其他几篇文章之后,[例如Detect English verb tenses using NLTKIdentifying verb tenses in pythonPython NLTK figure out tense]我编写了以下代码,以使用POS标记确定Python中句子的时态:

from nltk import word_tokenize, pos_tag

def determine_tense_input(sentence):
    text = word_tokenize(sentence)
    tagged = pos_tag(text)

    tense = {}
    tense["future"] = len([word for word in tagged if word[1] == "MD"])
    tense["present"] = len([word for word in tagged if word[1] in ["VBP", "VBZ","VBG"]])
    tense["past"] = len([word for word in tagged if word[1] in ["VBD", "VBN"]]) 
    return(tense)

这将返回过去/现在/将来动词用法的值,然后我通常将最大值作为句子的时态。准确度还算不错,但我想知道是否有更好的方法。

例如,现在是否偶然有一个包,它更专注于提取句子的时态?[注-3个堆栈溢出柱中有2个已经4年了,所以现在情况可能已经改变]。或者,我应该使用不同于nltk的解析器来提高准确性吗?如果没有,希望上面的代码可以帮助别人!


Tags: 代码inforlenif句子wordverb
3条回答

http://dev.lexalytics.com/wiki/pmwiki.php?n=Main.POSTags开始,标记的意思是

MD  Modal verb (can, could, may, must)
VB  Base verb (take)
VBC Future tense, conditional
VBD Past tense (took)
VBF Future tense
VBG Gerund, present participle (taking)
VBN Past participle (taken)
VBP Present tense (take)
VBZ Present 3rd person singular (takes)

所以你的代码是

tense["future"] = len([word for word in tagged if word[1] in ["VBC", "VBF"])

你可以通过各种方式加强你的方法。你可以考虑更多的英语语法,并根据你观察到的东西添加更多的规则;或者你可以推动统计方法,提取更多的(相关的)特征,并把所有的东西都扔给一个分类器。NLTK提供了大量的分类器供您使用,它们在NLTK书中有很好的文档记录。

你可以拥有两个世界中最好的:手写规则可以是输入到分类器的特性的形式,分类器将决定何时可以依赖它们。

您可以使用Stanford Parser来获得句子的依赖性分析。依赖分析的根将是定义句子的“主要”动词(我不太确定具体的语言术语是什么)。然后可以使用这个动词的POS标记来查找它的时态,并使用它。

相关问题 更多 >