【发布时间】:2021-01-01 14:50:44
【问题描述】:
您好,所以我有一个 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']}}
任何帮助都会很棒!我有一种感觉,这很容易,但太费脑筋了,看不到它。
【问题讨论】:
标签: python aggregate-functions