統計シーケンスで最も多く発生する要素
テキスト内のホットワードをどのように取得するかという大きなテキストに遭遇した場合、まずテキストをシーケンス化し(シーケンス化プロセスは言わない)、統計を取ります.
一般的な考え方.
出力結果は次のとおりです.
利用するCounterクラス実装
出力結果は次のとおりです.
Counterインスタンスは、次のような数学的演算操作と容易に結合されます.
出力結果は次のとおりです.
この方法の使用がもたらす便利さは明らかである.
一般的な考え方.
words = [
'look','into','my','eyes','look','into','my','eyes','the',
'eyes','the','eyes','the','eyes','not','around','the','eyes',
"don't",'look','around','the','eyes','look','into','my','eyes',
"you're",'under'
]
# ,
words_set = set(words)
words_dict = {}
# , ,
for i in words_set:
words_dict[i] = words.count(i)
print(words_dict)
#
sorted(words_dict.items(),key = lambda x:x[1],reverse=True)
出力結果は次のとおりです.
{'eyes': 8, 'not': 1, 'look': 4, 'into': 3, "you're": 1, "don't": 1, 'around': 2, 'the': 5, 'under': 1, 'my': 3}
[('eyes', 8), ('the', 5), ('look', 4),('into', 3), ('my', 3),
('around', 2), ('not', 1), ("you're", 1), ("don't", 1), ('under', 1)]
利用するCounterクラス実装
words = [
'look','into','my','eyes','look','into','my','eyes','the',
'eyes','the','eyes','the','eyes','not','around','the','eyes',
"don't",'look','around','the','eyes','look','into','my','eyes',
"you're",'under'
]
from collections import Counter
# , Counterl ( ),
word_counts = Counter(words)
print(word_counts)
# Counter most_common 4
top_four = word_counts.most_common(4)
print(top_four)
出力結果は次のとおりです.
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, 'not': 1, "don't": 1, "you're": 1, 'under': 1})
[('eyes', 8), ('the', 5), ('look', 4), ('into', 3)]
Counterインスタンスは、次のような数学的演算操作と容易に結合されます.
from collections import Counter
words = [
'look','into','my','eyes','look','into','my','eyes','the',
'eyes','the','eyes','the','eyes','not','around','the','eyes',
"don't",'look','around','the','eyes','look','into','my','eyes',
"you're",'under'
]
words1 = ['why','are','you','not','looking','in','my','eyes']
a = Counter(words)
print('a:',a)
b = Counter(words1)
print('b:',b)
print("a+b:",a+b)
出力結果は次のとおりです.
a: Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, 'not': 1, "don't": 1, "you're": 1, 'under': 1})
b: Counter({'why': 1, 'are': 1, 'you': 1, 'not': 1, 'looking': 1, 'in': 1, 'my': 1, 'eyes': 1})
a+b: Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2, 'around': 2, "don't": 1, "you're": 1, 'under': 1, 'why': 1, 'are': 1, 'you': 1, 'looking': 1, 'in': 1})
この方法の使用がもたらす便利さは明らかである.