问题 我有以下代码:
def _score_sentences(tf_idf_matrix) -> dict:
sentenceValue = {}
for sent, f_table in tf_idf_matrix.items():
total_score_per_sentence = 0
count_words_in_sentence = len(f_table)
for word, score in f_table.items():
total_score_per_sentence += score
sentenceValue[sent] = round(total_score_per_sentence / count_words_in_sentence, 3)
return sentenceValue
sentence_scores = _score_sentences(tf_idf_matrix)
基本上,应该创建一个字典,并为每个句子分配一个浮点数(取自tf_idf_matrix
)。它打印出看起来像字典的内容,但是当我运行以下代码时:
top_scores = {}
for sent, score in sentence_scores.items():
if f_table >= .4:
top_scores[sent] = score
print(top_scores)
我收到此错误报告:
AttributeError Traceback (most recent call last)
<ipython-input-50-2220086aa6aa> in <module>
1 top_scores = {}
2
----> 3 for sent, score in sentence_scores.items():
4 if f_table >= .4:
5 top_scores[sent] = score
AttributeError: 'str' object has no attribute 'items'
部分解决方案
我还看到了其他一些问题,建议尝试使用ast.literal_eval()
。但是,当我尝试
import ast
d = ast.literal_eval(sentence_scores)
top_scores = {}
for sent, score in d.items():
if f_table >= .4:
top_scores[sent] = score
print(top_scores)
我收到此错误报告:
Traceback (most recent call last):
File "c:\users\...\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-51-3d124b40f0bb>", line 3, in <module>
d = ast.literal_eval(sentence_scores)
File "c:\users\...\lib\ast.py", line 59, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "c:\users\...\lib\ast.py", line 47, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 1
P's grandfather
^
SyntaxError: EOL while scanning string literal
很抱歉,这个问题很长;我想尝试尽可能明确地解决问题,并尝试解决它。基本上,我想知道sentence_scores
是否是字符串。如果是这样,我想知道为什么ast.literal_eval()
无法正常工作。如果sentence_scores
是一本字典,我想知道为什么要得到这个初始的AttributeError
。
谢谢!
答案 0 :(得分:1)
类型检查应清除此问题。我没有足够的上下文信息来确定您的代码是否被正确使用,但是在其中添加了更好的类型注释:
from typing import Dict
def _score_sentences(tf_idf_matrix: Dict[str, Dict[str, float]]) -> Dict[str, float]:
sentence_value: Dict[str, float] = {}
for sent, f_table in tf_idf_matrix.items():
total_score_per_sentence = 0.0
count_words_in_sentence = len(f_table)
for word, score in f_table.items():
total_score_per_sentence += score
sentence_value[sent] = round(total_score_per_sentence / count_words_in_sentence, 3)
return sentence_value
sentence_scores = _score_sentences(tf_idf_matrix)
然后运行mypy
-会告诉您问题出在哪里。我的直觉是某些东西可能会覆盖代码中其他地方的sentence_scores
。