【发布时间】:2015-02-02 01:28:41
【问题描述】:
我正在参考书学习用python分析文档。当我阅读书中的一些代码时,我感到很困惑,这里的代码是: Example 6. A document summarization algorithm based principally upon sentence detection and frequency analysis within sentences
让我困惑的是:
s = [...words of sentence...]
word_idx = []
# For each word in the word list...
for w in important_words:
try:
# Compute an index for where any important words occur in the sentence.
word_idx.append(s.index(w))
except ValueError, e: # w not in this particular sentence
pass
word_idx.sort()
为什么不使用这个:
for i in range(len(s)):
w = s[i]
if w in important_words:
word_idx.append(i)
它们之间的区别在于: 前者不计重复词,后者计,例如:
s = [u'fes', u'watch', u'\u2014', u'e-paper', u'watch', u',', u'including', u'strap', u'.']
前者打印[0, 1, 2, 3, 5, 6, 7, 8],后者打印[0, 1, 2, 3, 4, 5, 6, 7, 8]
那么当我计算句子的分数时,我应该计算重复的单词吗?
【问题讨论】:
-
我很困惑为什么你得到
..., 5, 6, ...而不是..., 4, 6, ...:-) -
@AaronDigulla 很抱歉我没有说清楚。
s[4] = u'watch',你也可以看到s[2] = u'watch',所以前一种算法不会将4附加到word_idx -
哎呀,你是对的。我在脑子里数的时候索引错了。
标签: python algorithm nlp data-analysis