0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Day5. テキストの単語頻度ランキング - 勝手にChatGPTチャレンジ (Python)

Last updated at Posted at 2025-12-05

前提

本日のお題


5. テキストの単語頻度ランキング

何を作る?
テキストファイルを読み込んで、出現頻度トップ N の単語を表示するツール。

学べること

  • collections.Counter
  • 前処理(小文字化・句読点除去など)
  • ソートとスライス

面白いところ

  • 小説・ブログ・自分のコードコメントなどを分析して遊べる
  • 「自分はどんな言葉を多用しているか」が見えてちょっと恥ずかしい

準備

今回は以下のlorem ipsumという割と有名そうなダミーのテキストを使用してみます。

05_lorem_ipsum.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

日本語のテキストでも良いのですが、単語の分割にmecab等の形態素解析ライブラリが必要そうな気がしたので今回はやめました。

回答

コード

05_text_count.py
from collections import Counter

def load_lorem_ipsum():
    path = "05_lorem_ipsum.txt"

    with open(path, "r") as f:
        lines = f.readlines()
    return lines
    

def count_words(lines):
    counter = Counter()
    
    for line in lines:
        line_removed = line.replace(",", "").replace(".", "")
        line_splitted = line_removed.split()
        counter.update(line_splitted)
    return counter


def main():
    lines = load_lorem_ipsum()
    counter = count_words(lines)
    print("Top3:", counter.most_common(3))
    print("All:", counter.items())


if __name__ == "__main__":
    main()

実行例

$python 05_text_count.py
Top3: [('in', 3), ('dolor', 2), ('ut', 2)]
All: dict_items([('Lorem', 1), ('ipsum', 1), ('dolor', 2), ('sit', 1), ('amet', 1), ('consectetur', 1), ('adipiscing', 1), ('elit', 1), ('sed', 1), ('do', 1), ('eiusmod', 1), ('tempor', 1), ('incididunt', 1), ('ut', 2), ('labore', 1), ('et', 1), ('dolore', 2), ('magna', 1), ('aliqua', 1), ('Ut', 1), ('enim', 1), ('ad', 1), ('minim', 1), ('veniam', 1), ('quis', 1), ('nostrud', 1), ('exercitation', 1), ('ullamco', 1), ('laboris', 1), ('nisi', 1), ('aliquip', 1), ('ex', 1), ('ea', 1), ('commodo', 1), ('consequat', 1), ('Duis', 1), ('aute', 1), ('irure', 1), ('in', 3), ('reprehenderit', 1), ('voluptate', 1), ('velit', 1), ('esse', 1), ('cillum', 1), ('eu', 1), ('fugiat', 1), ('nulla', 1), ('pariatur', 1), ('Excepteur', 1), ('sint', 1), ('occaecat', 1), ('cupidatat', 1), ('non', 1), ('proident', 1), ('sunt', 1), ('culpa', 1), ('qui', 1), ('officia', 1), ('deserunt', 1), ('mollit', 1), ('anim', 1), ('id', 1), ('est', 1), ('laborum', 1)])

感想

  • collections.Counterはちゃんと使うと便利だと感じた。これからも積極的に使っていきたい。
  • Counterが優秀だったので学べることにあるソートとスライスを明示的に使っていないがまぁいいか…。

Coutner知れてよかった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?