Stack Overflow通过从当前问题的标题中删除谷歌认为最常见的10,000个英语单词来实现其“相关问题”功能。然后将剩余的单词作为全文搜索提交以查找相关问题。
我该如何获取这样一个最常用的英语单词列表?或其他语言中最常用的单词?这是我可以直接从谷歌网站上获取的吗?
你需要的是一个词频列表。你也可以自己制作一个,或者为特定领域定制一个,这是熟悉一些好的库的好方法。从这个问题中讨论的一些文本开始,然后尝试一些这个“信封背面”脚本的变体:
from nltk.stem.porter import PorterStemmer
import os
import string
from collections import defaultdict
ps = PorterStemmer()
word_count = defaultdict(int)
source_directory = '/some/dir/full/of/text'
for root, dirs, files in os.walk(source_directory):
for item in files:
current_text = os.path.join(root, item)
words = open(current_text, 'r').read().split()
for word in words:
entry = ps.stem_word(word.strip(string.punctuation).lower())
word_count[entry] += 1
results = [[word_count[i], i] for i in word_count]
print sorted(results)
这是对下载的几本书中最常见单词的统计结果:
[2955, 'that'], [4201, 'in'], [4658, 'to'], [4689, 'a'], [6441, 'and'], [6705, 'of'], [14508, 'the']]