gpt4 book ai didi

nlp - 识别句子中是否包含祈使句的方法

转载 作者:行者123 更新时间:2023-12-02 03:17:07 25 4
gpt4 key购买 nike

寻找句子中是否包含祈使句(例如,将“点击下方”归类为祈使句,而“这里有一些信息”则不包含)。

这可能吗?斯坦福解析器?作为引用,主站点 ( http://nlp.stanford.edu/software/lex-parser.shtml ) 指示“改进了命令式的识别”,但是依赖项手册并未指示它们的归档 http://nlp.stanford.edu/software/dependencies_manual.pdf )

或者,还有其他可行的方法吗?

最佳答案

我也未能找到任何(直接)解决“命令式检测”的图书馆或文献(它必须有一个不同的官方名称......)。这是我通过阅读祈使句语法、了解 chunking 得出的结论。和一些实验。

(Python + NLTK)

from nltk import RegexpParser
from nltk.tree import Tree

def is_imperative(tagged_sent):
# if the sentence is not a question...
if tagged_sent[-1][0] != "?":
# catches simple imperatives, e.g. "Open the pod bay doors, HAL!"
if tagged_sent[0][1] == "VB" or tagged_sent[0][1] == "MD":
return True

# catches imperative sentences starting with words like 'please', 'you',...
# E.g. "Dave, stop.", "Just take a stress pill and think things over."
else:
chunk = get_chunks(tagged_sent)
# check if the first chunk of the sentence is a VB-Phrase
if type(chunk[0]) is Tree and chunk[0].label() == "VB-Phrase":
return True

# Questions can be imperatives too, let's check if this one is
else:
# check if sentence contains the word 'please'
pls = len([w for w in tagged_sent if w[0].lower() == "please"]) > 0
# catches requests disguised as questions
# e.g. "Open the doors, HAL, please?"
if pls and (tagged_sent[0][1] == "VB" or tagged_sent[0][1] == "MD"):
return True

chunk = get_chunks(tagged_sent)
# catches imperatives ending with a Question tag
# and starting with a verb in base form, e.g. "Stop it, will you?"
elif type(chunk[-1]) is Tree and chunk[-1].label() == "Q-Tag":
if (chunk[0][1] == "VB" or
(type(chunk[0]) is Tree and chunk[0].label() == "VB-Phrase")):
return True

return False

# chunks the sentence into grammatical phrases based on its POS-tags
def get_chunks(tagged_sent):
chunkgram = r"""VB-Phrase: {<DT><,>*<VB>}
VB-Phrase: {<RB><VB>}
VB-Phrase: {<UH><,>*<VB>}
VB-Phrase: {<UH><,><VBP>}
VB-Phrase: {<PRP><VB>}
VB-Phrase: {<NN.?>+<,>*<VB>}
Q-Tag: {<,><MD><RB>*<PRP><.>*}"""
chunkparser = RegexpParser(chunkgram)
return chunkparser.parse(tagged_sent)

尚未测试算法的性能,但根据我的观察,我认为精确度可能比召回率更好。请注意,性能很大程度上取决于 POS 标签的正确性。

关于nlp - 识别句子中是否包含祈使句的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com