gpt4 book ai didi

python - NLP 问题处理带连词的句子

转载 作者:行者123 更新时间:2023-12-05 03:51:24 26 4
gpt4 key购买 nike

我想做什么

我想预处理包含连词的句子,如下所示。我不关心主语后面的动词时态和转换。我想要的是保留两个分别具有主语和动词的新句子。

**Pattern1**
They entered the house and she glanced at the dark fireplace.
["They entered the house ", "she glanced at the dark fireplace"]

**Pattern2**
Felipa and Alondra sing a song.
["Felipa sing a song”, "Alondra sing a song"]

**Pattern3**
“Jessica watches TV and eats dinner.
["Jessica watch TV, “Jessica eat dinner”]

问题

我能够用下面的代码解决 Pattern1 的句子,但我正在考虑用下面的代码 2 来思考 Pattern2 和 3 的解决方案。

使用the NLP library spaCy ,我能够弄清楚连词被识别为 CCONJ。但是,没有任何线索可以实现我想像上面那样做的事情。

请给我你的建议!

当前代码

模式1

text = "They entered the house and she glanced at the dark fireplace."
if 'and' in text:
text = text.replace('and',',')
l = [x.strip() for x in text.split(',') if not x.strip() == '']
l

#output
['They entered the house', 'she glanced at the dark fireplace.']

工作代码

text = "Felipa and Alondra sing a song."
doc_dep = nlp(text)
for k in range(len(doc_dep)):
token = doc_dep[k]
print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_)
if token.pos_ == 'CCONJ':
print(token.text)

#output
Felipa felipa NOUN NN nsubj
SPACE _SP
and and CCONJ CC cc
and
SPACE _SP
Alondra Alondra PROPN NNP nsubj
sing sing VERB VBP ROOT
a a DET DT det
song song NOUN NN dobj
. . PUNCT . punct
text = "Jessica watches TV and eats dinner."
doc_dep = nlp(text)
for k in range(len(doc_dep)):
token = doc_dep[k]
print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_)
if token.pos_ == 'CCONJ':
print(token.text)
#output
Jessica Jessica PROPN NNP nsubj
watches watch VERB VBZ ROOT
TV tv NOUN NN dobj
and and CCONJ CC cc
and
eats eat VERB VBZ conj
dinner dinner NOUN NN dobj
. . PUNCT . punct

开发环境

python 3.7.4

spaCy 版本 2.3.1

jupyter-笔记本:6.0.3

最佳答案

没有理由认为相同的代码应该能够处理所有这些情况,因为单词“and”的功能在每种情况下都非常不同。在模式 1 中,它连接两个独立的子句。在模式 2 中,它正在创建一个复合主题。在模式 3 中,它是并列动词短语。

我要提醒您,如果您的最终目标是以这种方式“拆分”所有包含单词“and”(或任何其他并列连词)的句子,那么您将面临一项非常具有挑战性的工作。并列连词在英语中以多种不同的方式发挥作用。有许多不同于您在此处列出的常见模式,例如非组成协调(“Bill went to Chicago on Wednesday and New York on Thursday”),您可能希望将其变成 [“Bill went to Chicago on Wednesday”, “Bill went to New York on Thursday”])——注意与“Bill went to Chicago and New York on Thursday”之间微妙但关键的区别,这需要变成 [“Bill went to Chicago on Thursday”,“Bill went to Chicago and New York”星期四去纽约"];协调动词(“玛丽看到并听到他走上台阶”),等等。当然,可以协调两个以上的成分(“Sarah、John 和 Marcia...”),并且许多模式都可以组合在同一个句子中。

英语是复杂的,处理这将是一项艰巨的工作,即使对于在所有要涵盖的情况下句法上有很强的掌握能力的语言学家也是如此。即使只是描述英语协调的行为方式也很困难,因为 this paper that considers just a handful of patterns说明。如果您认为您的代码必须处理带有多个 'and' 的真实句子,并且正在做不同的事情(例如,“自动驾驶汽车将保险责任和道德责任转移给制造商,并且看起来这不会很快改变”) ,任务的复杂性变得更加清晰。

就是说,如果您只对处理最常见和最简单的情况感兴趣,您至少可以通过处理像 the one built into NLTK 这样的选区解析器的结果来取得一些进展。 ,或像 benepar 这样的 SpaCy 插件.这至少会清楚地告诉您连词正在协调句子的哪些元素。

我不知道你的最终任务是什么,所以我不能自信地说,但我怀疑你通过这种方式预处理获得的 yield 是否值得付出努力。您可能会考虑退后一步,思考您试图实现的最终任务,并研究(和/或询问 StackOverflow)是否有任何已知的预处理步骤通常可以提高性能。

关于python - NLP 问题处理带连词的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62938465/

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