Count frequency of prepositions from input text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WuJianWei
    New Member
    • Mar 2007
    • 10

    Count frequency of prepositions from input text

    here' s the description:
    =============== =============== =============== ============
    You are asked to develop a C++ program that reads a text ¯le given by the user (e.g. test.txt), and output
    the frequency of the prepositions contained in the text ¯le on the screen. Your program shall provide a simple
    interface for the user to input the name of a text ¯le and exit the program.
    =============== =============== =============== ============
    I don't expect you to write the code for me...
    Just tell me what is the idea of the uqestion,OK?
  • Sieira
    New Member
    • May 2007
    • 31

    #2
    Originally posted by WuJianWei
    here' s the description:
    =============== =============== =============== ============
    You are asked to develop a C++ program that reads a text ¯le given by the user (e.g. test.txt), and output
    the frequency of the prepositions contained in the text ¯le on the screen. Your program shall provide a simple
    interface for the user to input the name of a text ¯le and exit the program.
    =============== =============== =============== ============
    I don't expect you to write the code for me...
    Just tell me what is the idea of the uqestion,OK?
    For instance, you can take each word, one by one from the text file, and compare it with a list of english prepositions, you have to count how many have been found, think about something that serves as a list and a counter of each member...



    (I DIDN'T WANT TO REPORT, I MISSED THE LINK FOR THE REPLY, SO SORRY)

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by Sieira
      (I DIDN'T WANT TO REPORT, I MISSED THE LINK FOR THE REPLY, SO SORRY)
      ??? As long as you didn't hit the 'send report' button, you didn't actually report, so relax. ;)


      Adrian

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by WuJianWei
        here' s the description:
        =============== =============== =============== ============
        You are asked to develop a C++ program that reads a text ¯le given by the user (e.g. test.txt), and output
        the frequency of the prepositions contained in the text ¯le on the screen. Your program shall provide a simple
        interface for the user to input the name of a text ¯le and exit the program.
        =============== =============== =============== ============
        I don't expect you to write the code for me...
        Just tell me what is the idea of the uqestion,OK?
        What Sieira said sounds right. You may wish to look up the STL map class to help you do this fairly easily.


        Adrian

        Comment

        • tomPee
          New Member
          • Jun 2007
          • 21

          #5
          Originally posted by WuJianWei
          here' s the description:
          =============== =============== =============== ============
          You are asked to develop a C++ program that reads a text ¯le given by the user (e.g. test.txt), and output
          the frequency of the prepositions contained in the text ¯le on the screen. Your program shall provide a simple
          interface for the user to input the name of a text ¯le and exit the program.
          =============== =============== =============== ============
          I don't expect you to write the code for me...
          Just tell me what is the idea of the uqestion,OK?
          Well, what i would do is, but it might be a bit to much of topic, i don't know that for sure, so you be the judge ^^.
          Thinking about reusability and a nice design i'd try something like this.

          You can read in two files: one file of prepositions and another one containing the text you have to search.
          Read all the prepositions and put them in a list of records ( or structs, or classes with the instructions to increment occurences, setters and getters etc. ) containing for example, a string and a int counter
          Every 'node' you insert in the 'list' has a string specified and that counter. Every time you find a word that's in your list you increment the counter. of that specific word.

          Offcourse you could also use an array to store the prepositions but then you'd have to you'd either have to:
          -make a constant for the array size
          -run the preposition file two times, one time to count the number of prepositions and one time to actually add them in the (dynamic) array.
          -Put at the top of the prepositions file the number of prepositions specified in the file.

          Offcourse how you want to implement it is totally our choice. But i do believe this has some nice potential. I like the object oriënted design and the classes, and it has some nice potential for reusability ! :D
          I believe, depending on the number of prepositions it might be better to use arrays ( as they the number of propositions becomes considerably larger ) though i don't think it'll matter anything for such few elements.
          Offcourse if you really want to top it you could even hold an extra, small array that holds the last 10 occurences of words also immediately specifying their index in the big array/list of prepositions so that more common prepositions won't require the 'long' search of the big array/list anymore.
          But the last bit is just over the top ^^

          Well i hope this gives you an idea of how you could implement such a program, it's allways good to give it some thought before you actually implement it. :)

          Hope this helps.
          Cheers,
          -Tom

          Comment

          • Sieira
            New Member
            • May 2007
            • 31

            #6
            Originally posted by tomPee
            [...]I believe, depending on the number of prepositions it might be better to use arrays ( as they the number of propositions becomes considerably larger ) though i don't think it'll matter anything for such few elements.[..]
            -Tom

            one list of prepositions cannot become large, Anyway, Tom's solution looks much more elegant

            Comment

            • tomPee
              New Member
              • Jun 2007
              • 21

              #7
              Originally posted by Sieira
              one list of prepositions cannot become large, Anyway, Tom's solution looks much more elegant
              Thanks ! :D

              And yes, the list of prepositions indeed can't become larger, but when you start using the file to recognize other paterns than only prepositions it could become bigger, but i shouldn't have used the word prepositions there :D.

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                I would still use a map to store your prepositions (key) and your count (value). That way it is fast and you don't have to reinvent some search mechinism.


                Adrian

                Comment

                • tomPee
                  New Member
                  • Jun 2007
                  • 21

                  #9
                  Originally posted by AdrianH
                  I would still use a map to store your prepositions (key) and your count (value). That way it is fast and you don't have to reinvent some search mechinism.


                  Adrian
                  [hijack_post]
                  Hmm, what's the map ? Is it built in in C++ ? :)
                  Because, i have programming experience in Oberon, but i'm quite new to C++ so i'm not all that familiar with all it's possibilities yet.
                  Could you explain how to use a map please ? :D
                  Thanks in advance :)
                  [/hijack_post]

                  -Tom

                  PS: Hijack is spelled like that right ?

                  Comment

                  Working...