我正在使用空间处理一个NLP过程,并试图获得一个字典的结果(分析的结果)来交叉检查完整的字典(由我预先确定)。我试着取一个句子,在space中运行它,看看它是否包含被认为是一个合适的英语句子的所有要求。
我尝试了两种不同的方法,但都不起作用。
集my_phrase
my_phrase = nlp(u"It is a beautiful day today.")
方法1
检查句子的词性
for word in my_phrase:
print(f'{word.text:{12}} {word.pos_:{10}} {word.tag_:{8}} {spacy.explain(word.tag_)}')
创建字典
my_phrase_ = defaultdict()
for _, value in enumerate(my_phrase):
key = "part_of_speech: " + value.pos_
my_phrase_[key] = value
是我要检查的POS的子集
english_sent = {
"DET": "Determiner",
"NOUN": "Noun",
"PRON": "Pronoun",
"PROPN": "Proper Noun",
"VERB": "Verb",
}
打印两个字典
print(english_sent)
print(my_phrase_)
检查来自english_sent的每个所需的POS是否至少有一个在my_phrase_字典
def checkKey(english_sent, my_phrase_):
if my_phrase in english_sent:
print("Present, ", end =" ")
print("value =", english_sent[my_phrase_])
else:
print("Not present")
checkKey(english_sent, my_phrase_)
它总是返回"Not present";我甚至试着把字典翻过来,让完整的单词成为键,缩写成为值。
方法2
我也尝试了一个简单的if语句,但也没有工作
if "NOUN" and "PRON" and "VERB" and "DET" in my_phrase_:
print("Valid statement")
不返回任何值
my_phrase_
的键是奇怪的-他们像part of speech: NOUN
时,他们应该像NOUN
。你应该这样修改你的代码:
my_phrase_ = defaultdict()
for _, value in enumerate(my_phrase):
my_phrase_[value.pos_] = value