【发布时间】:2020-11-06 08:16:19
【问题描述】:
我想做的事
我想预处理包括如下连词的句子。 我不在乎主语后面的动词时态和变形。 我想要的是保留两个分别具有主语和动词的新句子。
**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
【问题讨论】:
-
不太符合您的模式。 Pattern2Felipa 和 Alondra 在哪里写歌。 [“Felipa 唱歌”,“我去购物”] Pattern3 Jessica 看电视吃晚饭。 [“尼克看电视,“杰西卡吃晚餐”] ....................您的意思是说:Pattern2Felipa 和 Alondra 唱歌一首歌。[“Felipa sing a song”,“Alondra sing a song”] Pattern3 Jessica 看电视吃晚饭。[“Jessica 看电视,“Jessica 吃晚饭”] ??
-
是的。感谢您的评论。
标签: python python-3.x nlp stanford-nlp spacy