python counts the number of words

Updated on educate 2024-03-07
4 answers
  1. Anonymous users2024-02-06

    The upstairs program has many problems, such as not handling punctuation, wrong way to read files, etc.

    Is the building mainly case-sensitive? If you want to make a distinction, let's follow the following:

    import re

    def get_word_frequencies(file_name):

    dic = {}

    txt = open(filename, 'r').read().splitlines()

    The following sentence replaces except'-'outside all punctuation, since'-'May be present in words.

    txt = '[^\u4e00-\u94a5\w\d\-]', ' ', txt)

    Replace the separate'-'

    txt = ' - ', ' ', txt)for line in :

    for word in :

    If it is not case-sensitive, it will be treated as lowercase, and the following sentence will be changed to 0), 0).

    dic[word] += 1

    print dic

    if __name__ = '__main__':

    get_word_frequencies(''If you have any questions, keep asking.

  2. Anonymous users2024-02-05

    The idea goes something like this:

    Read the content of the file into a linked list, use the count() function of the linked list to count the number of spaces n, and then loop n times, mark the spaces before and after each word, take out the word, put it in an empty linked list of word[], and then use the count() function to count the number of occurrences, put it in another number【] empty linked list, after the loop is executed, the result output=dict(zip(word,number)) is the final dictionary output;

    Or you can use the re regular expression to directly find the word in the middle of the space, output it to a new linked list, and then directly count each word.

  3. Anonymous users2024-02-04

    dic = {}

    for line in open(filename):

    for word in :

    dic[word] += 1

    dic about this, dic is what you want, just deal with this kind of thing by yourself;

    Get a ['-other don't'].

  4. Anonymous users2024-02-03

    Summary. How to count the total number of words and sentences in an English article in python.

    Ask about custom messages].

Related questions
4 answers2024-03-07

Arrows and songs. henry wadsworth longfellow(1807-1882)

I shoot an arrow into the sky, and I don't know where it lands. >>>More

15 answers2024-03-07

I memorized the right number of words every day, I memorized it for a year last year, so let's share my experience: The "music words" I use plan to memorize about 40 to 45 words a day, and the review plan will slowly become 85 to 100. I usually skip the review task in the morning, and add it to the dictionary without any impression of it becoming a new word, and read more example sentences when I have time. >>>More

3 answers2024-03-07

Clap clapp, chop chop, chip chips, chap guys, camp camping, clip paper clip, crap crap, crop crops, coup coup. >>>More

5 answers2024-03-07

Personally, I think that "polysemy" is an inevitable phenomenon in any language, which is caused by "different usages", but "different etymologies" is only seen in "synonyms". >>>More

2 answers2024-03-07

Abase is lowered.

abate lightened. >>>More