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


댓글 없음:

댓글 쓰기

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

쿵푸를 하는 옵티머스 - 2030년에 100만대 대량 생산을 시작한다고 합니다.

 에이전틱한 로봇으로 계속 발전하고 있습니다. ㅎㅎ  https://www.youtube.com/watch?v=E1oi10TRqgk