你好,我有一个 python 函数正在工作,但没有按照我期望的方式工作,我不确定我的代码在哪里。
def preprocess(text):
case = truecase.get_true_case(text)
doc = nlp(case)
return doc
def summarize_texts(texts):
actions = {}
entities = {}
for item in texts:
doc = preprocess(item)
for token in doc:
if token.pos_ == "VERB":
actions[str.lower(token.text)] = actions.get(token.text, 0) +1
for token in doc.ents:
entities[token.label_] = [token.text]
if token.text not in entities[token.label_]:
entities[token.label_].append(token.text)
return {
'actions': actions,
'entities': entities
}
当我调用句子列表的函数时,这是我得到的输出:
docs = [
"Play something by Billie Holiday, and play again",
"Set a timer for five minutes",
"Play it again, Sam"
]
summarize_texts(docs)
output: {'actions': {'play': 1, 'set': 1},
'entities': {'PERSON': ['Sam'], 'TIME': ['five minutes']}}
它正在查找操作键和实体键,但我遇到两个问题。
它没有计算正确的动作
它只存储每个实体的最后一个值。
输出应该是:
output: {'actions': {'play': 3, 'set': 1},
'entities': {'PERSON': ['Billie','Sam'], 'TIME': ['five minutes']}}
任何帮助都会很棒!我有一种感觉,这很简单,但太烧脑了,看不到它。
沧海一幻觉
九州编程
相关分类