【问题标题】:Python, word frequency of each sentences for summarizationPython,用于总结的每个句子的词频
【发布时间】:2021-03-15 13:22:02
【问题描述】:

我正在尝试基于图形的文本摘要。

sentences_rank_frek=[]
for sentence in copy_sent:
    for word in sentence:
        if word in freq:
            sentences_rank_frek[sentence]+=freq[copy_sent[sentence][word]] 

此代码给出错误:列表索引必须是整数或切片,而不是列表 我可以显示这个 freq[copy_sent[0][1]] = 4

【问题讨论】:

  • 如果您的sentence 不是整数,您将无法访问列表sentences_rank_frek
  • 句子是整数
  • copy_sent[sentence][word] 呢?
  • copy_sent 是一个文本列表。// copy_sent[0][1] = "sale" // copy_sent[0]= 文本中的第一句
  • 你是说你正在尝试使用文本访问列表freq

标签: python text nlp summarization


【解决方案1】:

在尝试访问sentences_rank_frek[sentence] 时,您需要检查sentence 变量的类型。我猜......这是一个句子,所以它必须是一个字符串或字符串列表。您正在尝试使用字符串访问列表中的索引。

您可能希望您的 sentences_rank_frek=[] 成为字典而不是数组。

【讨论】:

  • {#frequency for sentence in copy_sent: for word in sentence: if word in freq: freq[word]=freq[word]+1 else : freq[word]=1}
  • 我可以计算每个单词的频率
  • 能不能清楚的列出每个变量的不同数据类型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 2020-04-16
  • 2019-02-20
  • 1970-01-01
相关资源
最近更新 更多