White scenery @showyou, hatena

If you have any comments, you may also send twitter @shsub or @showyou.

分かち書きした単語列から、上位n個を得るプログラム。

#!/usr/bin/python
# -*- coding: sjis -*-

#現在のログから、注目の単語topN個を抽出

#辞書型の中のものをソートします。
def sortWordCnt(wordcnt):
	# まず、辞書をリストのリストに変換かなぁ。
	words = wordcnt.keys()
	words.sort(key=lambda x: wordcnt[x], reverse=True)
	return words
	
#引数 string配列
#既にmecabで分かち書きされてること
def topN(words,n):
	wordcnt = {}
	for s in words:
		if wordcnt.has_key(s)== False :
			wordcnt[s] = 1
		else:
			wordcnt[s] += 1
		
	wordRank = sortWordCnt(wordcnt)
	i = 0
	lst = []
	for w in wordRank:
		print w + "x" + str(wordcnt[w])
		lst.append(w)
		i+=1
		if i >= n : break
	print ""
	return lst