【发布时间】:2022-01-01 02:17:37
【问题描述】:
我的文本由一系列基本句子组成,例如 “她是医生”、“他是个好人”,以及等等。我正在尝试编写一个只返回名词和前面的代词(例如她、他、它)的程序。我需要它们成对打印,例如(she, doctor) 或(he, person)。我正在使用SpaCy,因为这样我也可以处理类似的法语和德语文本。
This 是我在本网站其他地方找到的最接近我需要的东西。到目前为止,我一直在尝试的是在文本中生成一个名词列表,然后在文本中搜索列表中的名词,并在其前面打印名词和单词 3 个位置(因为这是大多数的模式句子,大多数对我的目的来说已经足够好了)。这就是我创建列表所需要的:
def spacy_tag(text):
text_open = codecs.open(text, encoding='latin1').read()
parsed_text = nlp_en(text_open)
tokens = list([(token, token.tag_) for token in parsed_text])
list1 = []
for token, token.tag_ in tokens:
if token.tag_ == 'NN':
list1.append(token)
return(list1)
但是,当我尝试对其进行任何操作时,我会收到一条错误消息。我试过使用枚举,但我也无法让它工作。这是我在文本中搜索列表中单词的当前代码(我还没有开始添加应该预先在几个地方打印单词的部分,因为我仍然停留在搜索部分):
def spacy_search(text, list):
text_open = codecs.open(text, encoding='latin1').read()
for word in text_open:
if word in list:
print(word)
我得到的错误是在第 4 行,"if word in list:", and it says "TypeError: Argument 'other' has incorrect type (expected spacy.tokens.token.Token, got str)"
有没有更有效的方法来使用SpaCy 打印PRP、NN pair?或者,如何修改我的代码以使其在文本中搜索列表中的名词? (它不需要是一个特别优雅的解决方案,它只需要产生一个结果)。
【问题讨论】:
标签: python spacy pos-tagger