Word frequency counter in file










1














I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16... but it outputs this instead 'A':2...'a':14. Here is my code. Any help would be much appreciated.



file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word not in wordcount:
wordcount[word]=1
else:
wordcount[word]+=1
print(wordcount)









share|improve this question




























    1














    I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16... but it outputs this instead 'A':2...'a':14. Here is my code. Any help would be much appreciated.



    file=open("phrases.txt","r")
    wordCount=
    for word in file.read().split():
    if word not in wordcount:
    wordcount[word]=1
    else:
    wordcount[word]+=1
    print(wordcount)









    share|improve this question


























      1












      1








      1







      I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16... but it outputs this instead 'A':2...'a':14. Here is my code. Any help would be much appreciated.



      file=open("phrases.txt","r")
      wordCount=
      for word in file.read().split():
      if word not in wordcount:
      wordcount[word]=1
      else:
      wordcount[word]+=1
      print(wordcount)









      share|improve this question















      I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16... but it outputs this instead 'A':2...'a':14. Here is my code. Any help would be much appreciated.



      file=open("phrases.txt","r")
      wordCount=
      for word in file.read().split():
      if word not in wordcount:
      wordcount[word]=1
      else:
      wordcount[word]+=1
      print(wordcount)






      python dictionary file-io






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 6:27









      petezurich

      3,49581733




      3,49581733










      asked Nov 12 at 6:05









      gbenyu

      224




      224






















          4 Answers
          4






          active

          oldest

          votes


















          1














          Seems like in the question your saying there is a uppercase and lowercase issue, so why not:



          file=open("phrases.txt","r")
          wordCount=
          for word in file.read().split():
          if word.lower() not in wordcount:
          wordcount[word.lower()]=1
          else:
          wordcount[word.lower()]+=1
          print(wordcount)


          Or:



          file=open("phrases.txt","r")
          wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
          for word in file.read().split():
          wordcount[word.lower()]+=1
          print(wordcount)





          share|improve this answer




























            2














            You can use an inbuilt function called Counter for this as an alternative to looping through the list.



            example :



            from collections import Counter

            file = open("phrases.txt","r")
            data = file.read().lower().split() # added lower() will convert everything to lower case
            wordcount = dict(Counter(data))
            print(wordcount)





            share|improve this answer




























              0














              lower all the words when comparing.
              for word.lower() in file.read().split():






              share|improve this answer




















              • That's not valid!!!, bad syntax
                – U9-Forward
                Nov 12 at 6:09










              • convert it to string for str(word).lower() in file.read().split():
                – AmilaMGunawardana
                Nov 12 at 6:10










              • Not helping....
                – U9-Forward
                Nov 12 at 6:11










              • file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                – AmilaMGunawardana
                Nov 12 at 6:15






              • 1




                file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                – AmilaMGunawardana
                Nov 12 at 8:23



















              0














              You can convert the words to lowercase, and then count them. So, your code changes to something like this.



              file=open("phrases.txt","r")
              wordCount=
              for word in file.read().split():
              newWord = word.lower()
              if newWord not in wordcount:
              wordcount[newWord]=1
              else:
              wordcount[newWord]+=1
              print(wordcount)


              Basically, you will be storing in the dict, where keys are the lower case versions of each word.



              Do note, that you will lose "data", if you are doing operations which are case sensitive.






              share|improve this answer




















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256644%2fword-frequency-counter-in-file%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                Seems like in the question your saying there is a uppercase and lowercase issue, so why not:



                file=open("phrases.txt","r")
                wordCount=
                for word in file.read().split():
                if word.lower() not in wordcount:
                wordcount[word.lower()]=1
                else:
                wordcount[word.lower()]+=1
                print(wordcount)


                Or:



                file=open("phrases.txt","r")
                wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
                for word in file.read().split():
                wordcount[word.lower()]+=1
                print(wordcount)





                share|improve this answer

























                  1














                  Seems like in the question your saying there is a uppercase and lowercase issue, so why not:



                  file=open("phrases.txt","r")
                  wordCount=
                  for word in file.read().split():
                  if word.lower() not in wordcount:
                  wordcount[word.lower()]=1
                  else:
                  wordcount[word.lower()]+=1
                  print(wordcount)


                  Or:



                  file=open("phrases.txt","r")
                  wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
                  for word in file.read().split():
                  wordcount[word.lower()]+=1
                  print(wordcount)





                  share|improve this answer























                    1












                    1








                    1






                    Seems like in the question your saying there is a uppercase and lowercase issue, so why not:



                    file=open("phrases.txt","r")
                    wordCount=
                    for word in file.read().split():
                    if word.lower() not in wordcount:
                    wordcount[word.lower()]=1
                    else:
                    wordcount[word.lower()]+=1
                    print(wordcount)


                    Or:



                    file=open("phrases.txt","r")
                    wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
                    for word in file.read().split():
                    wordcount[word.lower()]+=1
                    print(wordcount)





                    share|improve this answer












                    Seems like in the question your saying there is a uppercase and lowercase issue, so why not:



                    file=open("phrases.txt","r")
                    wordCount=
                    for word in file.read().split():
                    if word.lower() not in wordcount:
                    wordcount[word.lower()]=1
                    else:
                    wordcount[word.lower()]+=1
                    print(wordcount)


                    Or:



                    file=open("phrases.txt","r")
                    wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
                    for word in file.read().split():
                    wordcount[word.lower()]+=1
                    print(wordcount)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 at 6:07









                    U9-Forward

                    11.8k21136




                    11.8k21136























                        2














                        You can use an inbuilt function called Counter for this as an alternative to looping through the list.



                        example :



                        from collections import Counter

                        file = open("phrases.txt","r")
                        data = file.read().lower().split() # added lower() will convert everything to lower case
                        wordcount = dict(Counter(data))
                        print(wordcount)





                        share|improve this answer

























                          2














                          You can use an inbuilt function called Counter for this as an alternative to looping through the list.



                          example :



                          from collections import Counter

                          file = open("phrases.txt","r")
                          data = file.read().lower().split() # added lower() will convert everything to lower case
                          wordcount = dict(Counter(data))
                          print(wordcount)





                          share|improve this answer























                            2












                            2








                            2






                            You can use an inbuilt function called Counter for this as an alternative to looping through the list.



                            example :



                            from collections import Counter

                            file = open("phrases.txt","r")
                            data = file.read().lower().split() # added lower() will convert everything to lower case
                            wordcount = dict(Counter(data))
                            print(wordcount)





                            share|improve this answer












                            You can use an inbuilt function called Counter for this as an alternative to looping through the list.



                            example :



                            from collections import Counter

                            file = open("phrases.txt","r")
                            data = file.read().lower().split() # added lower() will convert everything to lower case
                            wordcount = dict(Counter(data))
                            print(wordcount)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 6:25









                            LonelyCpp

                            366111




                            366111





















                                0














                                lower all the words when comparing.
                                for word.lower() in file.read().split():






                                share|improve this answer




















                                • That's not valid!!!, bad syntax
                                  – U9-Forward
                                  Nov 12 at 6:09










                                • convert it to string for str(word).lower() in file.read().split():
                                  – AmilaMGunawardana
                                  Nov 12 at 6:10










                                • Not helping....
                                  – U9-Forward
                                  Nov 12 at 6:11










                                • file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 6:15






                                • 1




                                  file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 8:23
















                                0














                                lower all the words when comparing.
                                for word.lower() in file.read().split():






                                share|improve this answer




















                                • That's not valid!!!, bad syntax
                                  – U9-Forward
                                  Nov 12 at 6:09










                                • convert it to string for str(word).lower() in file.read().split():
                                  – AmilaMGunawardana
                                  Nov 12 at 6:10










                                • Not helping....
                                  – U9-Forward
                                  Nov 12 at 6:11










                                • file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 6:15






                                • 1




                                  file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 8:23














                                0












                                0








                                0






                                lower all the words when comparing.
                                for word.lower() in file.read().split():






                                share|improve this answer












                                lower all the words when comparing.
                                for word.lower() in file.read().split():







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 12 at 6:08









                                AmilaMGunawardana

                                645




                                645











                                • That's not valid!!!, bad syntax
                                  – U9-Forward
                                  Nov 12 at 6:09










                                • convert it to string for str(word).lower() in file.read().split():
                                  – AmilaMGunawardana
                                  Nov 12 at 6:10










                                • Not helping....
                                  – U9-Forward
                                  Nov 12 at 6:11










                                • file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 6:15






                                • 1




                                  file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 8:23

















                                • That's not valid!!!, bad syntax
                                  – U9-Forward
                                  Nov 12 at 6:09










                                • convert it to string for str(word).lower() in file.read().split():
                                  – AmilaMGunawardana
                                  Nov 12 at 6:10










                                • Not helping....
                                  – U9-Forward
                                  Nov 12 at 6:11










                                • file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 6:15






                                • 1




                                  file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                  – AmilaMGunawardana
                                  Nov 12 at 8:23
















                                That's not valid!!!, bad syntax
                                – U9-Forward
                                Nov 12 at 6:09




                                That's not valid!!!, bad syntax
                                – U9-Forward
                                Nov 12 at 6:09












                                convert it to string for str(word).lower() in file.read().split():
                                – AmilaMGunawardana
                                Nov 12 at 6:10




                                convert it to string for str(word).lower() in file.read().split():
                                – AmilaMGunawardana
                                Nov 12 at 6:10












                                Not helping....
                                – U9-Forward
                                Nov 12 at 6:11




                                Not helping....
                                – U9-Forward
                                Nov 12 at 6:11












                                file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                – AmilaMGunawardana
                                Nov 12 at 6:15




                                file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                – AmilaMGunawardana
                                Nov 12 at 6:15




                                1




                                1




                                file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                – AmilaMGunawardana
                                Nov 12 at 8:23





                                file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
                                – AmilaMGunawardana
                                Nov 12 at 8:23












                                0














                                You can convert the words to lowercase, and then count them. So, your code changes to something like this.



                                file=open("phrases.txt","r")
                                wordCount=
                                for word in file.read().split():
                                newWord = word.lower()
                                if newWord not in wordcount:
                                wordcount[newWord]=1
                                else:
                                wordcount[newWord]+=1
                                print(wordcount)


                                Basically, you will be storing in the dict, where keys are the lower case versions of each word.



                                Do note, that you will lose "data", if you are doing operations which are case sensitive.






                                share|improve this answer

























                                  0














                                  You can convert the words to lowercase, and then count them. So, your code changes to something like this.



                                  file=open("phrases.txt","r")
                                  wordCount=
                                  for word in file.read().split():
                                  newWord = word.lower()
                                  if newWord not in wordcount:
                                  wordcount[newWord]=1
                                  else:
                                  wordcount[newWord]+=1
                                  print(wordcount)


                                  Basically, you will be storing in the dict, where keys are the lower case versions of each word.



                                  Do note, that you will lose "data", if you are doing operations which are case sensitive.






                                  share|improve this answer























                                    0












                                    0








                                    0






                                    You can convert the words to lowercase, and then count them. So, your code changes to something like this.



                                    file=open("phrases.txt","r")
                                    wordCount=
                                    for word in file.read().split():
                                    newWord = word.lower()
                                    if newWord not in wordcount:
                                    wordcount[newWord]=1
                                    else:
                                    wordcount[newWord]+=1
                                    print(wordcount)


                                    Basically, you will be storing in the dict, where keys are the lower case versions of each word.



                                    Do note, that you will lose "data", if you are doing operations which are case sensitive.






                                    share|improve this answer












                                    You can convert the words to lowercase, and then count them. So, your code changes to something like this.



                                    file=open("phrases.txt","r")
                                    wordCount=
                                    for word in file.read().split():
                                    newWord = word.lower()
                                    if newWord not in wordcount:
                                    wordcount[newWord]=1
                                    else:
                                    wordcount[newWord]+=1
                                    print(wordcount)


                                    Basically, you will be storing in the dict, where keys are the lower case versions of each word.



                                    Do note, that you will lose "data", if you are doing operations which are case sensitive.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 12 at 6:10









                                    MaJoR

                                    408111




                                    408111



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256644%2fword-frequency-counter-in-file%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        這個網誌中的熱門文章

                                        How to read a connectionString WITH PROVIDER in .NET Core?

                                        In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                        Museum of Modern and Contemporary Art of Trento and Rovereto