Counting word frequency and making a dictionary from it

前端 未结 10 956
南旧
南旧 2020-12-05 21:08

I want to take every word from a text file, and count the word frequency in a dictionary.

Example: \'this is the textfile, and it is used to take words and co

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 21:41

    If you don't want to use collections.Counter, you can write your own function:

    import sys
    
    filename = sys.argv[1]
    fp = open(filename)
    data = fp.read()
    words = data.split()
    fp.close()
    
    unwanted_chars = ".,-_ (and so on)"
    wordfreq = {}
    for raw_word in words:
        word = raw_word.strip(unwanted_chars)
        if word not in wordfreq:
            wordfreq[word] = 0 
        wordfreq[word] += 1
    

    for finer things, look at regular expressions.

提交回复
热议问题