Appendix to Python



  • There's a fragment of the code that reset the text in a spontaneous manner and counts how many times the word has been met and adds +1 to the dictionary.

    word = ""
    dic = {}
    for c in file:   
        if c not in punctuations:
            if word not in uninteresting_words:
                if c == " ":
                    dic[word] += 1
                    word = ""
                    continue
                word += c
    print(word)
    

    There's a mistake: KeyError



  • So you'll finish the mistake.

    The code you've issued does not define the variables punctuations and uninteresting_words

    In addition (mostly the variables mentioned above are all defined) You got a mistake here.

    dic[word] += 1
    

    You write in the vocabulary when the vocabulary is empty

    You've got to do this:

    dic[word] = dic.get(word, 0) + 1
    

    With regard to the logic of the programme, it's not as good as God. Here's the algorithmic errors.:

    1. The algorithm doesn't recognize these structures. "word1,word2"

    Your puncture symbols should also serve as quotes

    1. The algorithm is mistreating these structures. "word1 word2"

    last word, if he's not followed by a sectioner.

    1. You're working with some uninterested words, but the algorithm uses it in checking every symbol, not a word, so if the first word was uninterested, then the algorithm won't give the NI EL word any more.

    Your code would look like,

    file = "word1,word2 word3"
    

    punctuations = [' ', ',']
    uninteresting_words = [''] # чтобы не делать дополнительную проверку на пустое слово (возникает если несколько разделителей один за другим)

    word = ""
    dic = {}

    for i in range(len(file)):
    if file[i] not in punctuations:
    word += file[i]

    if file[i] in punctuations or i == len(file) - 1:
        if word not in uninteresting_words:
            dic[word] = dic.get(word, 0) + 1
        word = ""
    

    print(dic)


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2