【发布时间】:2011-12-07 12:46:16
【问题描述】:
我有一个英语单词列表(大约 10000 个),我想按照它们在文学、报纸、博客等中出现的用法对它们进行排序。我可以用 Python 或其他语言对它们进行排序吗?我听说过NLTK,这是我所知道的最近的可以提供帮助的图书馆。还是其他工具的任务?
谢谢
【问题讨论】:
我有一个英语单词列表(大约 10000 个),我想按照它们在文学、报纸、博客等中出现的用法对它们进行排序。我可以用 Python 或其他语言对它们进行排序吗?我听说过NLTK,这是我所知道的最近的可以提供帮助的图书馆。还是其他工具的任务?
谢谢
【问题讨论】:
您可以使用collections.Counter。代码就这么简单:
l = get_iterable_or_list_of_words() # That is up to you
c = collections.Counter(l)
print(c.most_common())
【讨论】:
我对自然语言处理知之甚少,但我认为 Python 是一种理想的语言,供您使用。
在 Google 搜索中找到“Python 自然语言”:
搜索 StackOverflow 找到了这个答案:
Python or Java for text processing (text mining, information retrieval, natural language processing)
这又与模式相关联:
http://www.clips.ua.ac.be/pages/pattern
您可能想看看 Pattern,这似乎很有希望。
祝你好运,玩得开心!
【讨论】:
Python 和 NLTK 是对单词表进行排序的完美工具,因为 NLTK 带有一些英语语料库,您可以从中提取频率信息。
以下代码将按照棕色语料库中的词频顺序打印给定的wordlist:
import nltk
from nltk.corpus import brown
wordlist = ["corpus","house","the","Peter","asdf"]
# collect frequency information from brown corpus, might take a few seconds
freqs = nltk.FreqDist([w.lower() for w in brown.words()])
# sort wordlist by word frequency
wordlist_sorted = sorted(wordlist, key=lambda x: freqs[x.lower()], reverse=True)
# print the sorted list
for w in wordlist_sorted:
print w
输出:
>>>
the
house
Peter
corpus
asdf
如果您想使用不同的语料库或获取更多信息,您应该查看chapter 2 of the nltk book。
【讨论】: