spaCy NLP 使用 wordlist 提取意图

问题描述 投票:0回答:1

我正在学习 Mastering spaCy 一书中的示例,我想使用以下代码使用单词列表提取句子的意图:

from spacy.matcher import Matcher
from collections import Counter
from spacy import displacy
import en_core_web_md
import pandas as pd
import spacy 

nlp = en_core_web_md.load()

doc = nlp("i want to make a reservation for a flight")
dObj =None
tVerb = None

# Extract the direct object and its transitive verb
for token in doc:
    if token.dep_ == "dobj":
        dObj = token
        tVerb = token.head

# Extract the helper verb
intentVerb = None
verbList = ["want", "like", "need", "order"]
if tVerb.text in verbList:
    intentVerb = tVerb
else:
    if tVerb.head.dep_ == "ROOT":
        helperVerb = tVerb.head

# Extract the object of the intent
intentObj = None
objList = ["flight", "meal", "booking"]

if dObj.text in objList:
      intentObj = dObj
else:
      for child in dObj.children:
        if child.dep_ == "prep":
            intentObj = list(child.children)[0]
            break
        elif child.dep_ == "compound":
            intentObj = child
            break 
print(intentVerb.text + intentObj.text.capitalize())

想要的结果是

想要飞行

但是我得到了这个错误

34 print(intentVerb.text + intentObj.text.capitalize())
AttributeError: 'NoneType' object has no attribute 'text'

提取助动词时出现错误

任何帮助将不胜感激

干杯

python nlp spacy
1个回答
0
投票
tVerb = None # <- init with None

for token in doc:
    if token.dep_ == "dobj":
        dObj = token
        tVerb = token.head # <- overwrites tVerb for each token, at the end 
                           # tverb will be the .head of 'flight'

verbList = ["want", "like", "need", "order"]
if tVerb.text in verbList:
    intentVerb = tVerb # <- 'flight' is not in verbList so intentVerb is never set 
                       # and still the init: None

print(intentVerb.text + intentObj.text.capitalize())  # <- None type has no attribute '.text' therefore you get your error.

作为一般建议,查找调试并理想地选择具有调试支持的 IDE。

© www.soinside.com 2019 - 2024. All rights reserved.