目次
説明
文字列の中から単語を抜き出す。その単語が何回出てきたかも知ることが出来る。
実装
# -*- coding: utf-8 -*- # ライブラリの読み込み import re from collections import Counter # 00 テキストの取得 target_text = """ Python is a great lang lang """ # 01 文章を単語に分ける # 複数の区切り文字を指定するため re.split を使う words = re.split(r'\s|\,|\.|\(|\)', target_text.lower()) # 02 集計する counter = Counter(words) # 03 表示する # Counter#most_common を使って出現頻度の降順に csv 形式で表示する for word, count in counter.most_common(): if len(word) > 0: print("%s,%d" % (word, count)) # => csv 形式の単語出現回数