2016년 6월 29일 수요일

python으로 파일에 있는 단어의 갯수 세기

킹제임스 버전의 영어 성경입니다. 코드는 아래와 같습니다.

# most_common_words.py
import sys
from collections import Counter

if __name__ == "__main__":
    try:
        num_words = int(sys.argv[1])
    except:
        print("usage: most_common_words.py num_words")
        sys.exit(1)

    counter = Counter(word.lower() \
                      for line in sys.stdin
                      for word in line.strip().split()
                      if word)

    for word, count in counter.most_common(num_words):
        sys.stdout.write(str(count))
        sys.stdout.write("\t")
        sys.stdout.write(word)
        sys.stdout.write("\n")


다음과 같이 실행하면됩니다.

C:\work>type the_bible.txt | python most_common_words.py 20


댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.

인공지능 모델의 생태계를 정리해 주신 분이 계셔서 한번 올려봅니다.

 하루가 다르게 시장이 변하고 있어서 공부하기가 벅찹니다. ㅋㅋ