word count

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jwvai316

    #1

    word count

    I'm having problem in this case. I need to count how many time each
    word appears in one sentence. and I need it to be case insensitive

    for example when the input is

    The quick brown fox jumps over the lazy dog.

    and the output is

    the 2
    quick 1
    brown 1
    fox 1
    jumps 1
    over 1
    lazy 1
    dog 1

    anyone can help me to solve this problem?
    an example may help me a lot.

    thanks.

  • Emmanuel Delahaye

    #2
    Re: word count

    jwvai316 wrote on 15/08/05 :[color=blue]
    > I'm having problem in this case. I need to count how many time each
    > word appears in one sentence. and I need it to be case insensitive[/color]

    What exactly is your question about the C-language ? We don't do
    homeworks here.

    Do your best and post your code. If you know nothing about C, start
    from the beginning with a good C-book. The 'Kernighan and Ritchie' is a
    reference.

    http://cm.bell-labs.com/cm/cs/cbook/


    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"


    Comment

    • Kenny McCormack

      #3
      Re: word count

      In article <1124098890.824 952.24810@z14g2 000cwz.googlegr oups.com>,
      jwvai316 <jwvai316@gmail .com> wrote:[color=blue]
      >I'm having problem in this case. I need to count how many time each
      >word appears in one sentence. and I need it to be case insensitive
      >
      >for example when the input is
      >
      >The quick brown fox jumps over the lazy dog.
      >
      >and the output is
      >
      >the 2
      >quick 1
      >brown 1
      >fox 1
      >jumps 1
      >over 1
      >lazy 1
      >dog 1
      >
      >anyone can help me to solve this problem?
      >an example may help me a lot.
      >
      >thanks.
      >[/color]

      #!gawk
      { for (i=1; i<=NF; i++) x[$i]++ }
      END { for (i in x) print i,x[i] }

      (followups set)

      Comment

      • osmium

        #4
        Re: word count

        "jwvai316" writes:
        [color=blue]
        > I'm having problem in this case. I need to count how many time each
        > word appears in one sentence. and I need it to be case insensitive
        >
        > for example when the input is
        >
        > The quick brown fox jumps over the lazy dog.
        >
        > and the output is
        >
        > the 2
        > quick 1
        > brown 1
        > fox 1
        > jumps 1
        > over 1
        > lazy 1
        > dog 1
        >
        > anyone can help me to solve this problem?
        > an example may help me a lot.[/color]

        The question cries out for a tree. Trees are discussed in , AFAIK, all
        books on data structures.


        Comment

        • David Resnick

          #5
          Re: word count


          osmium wrote:[color=blue]
          > "jwvai316" writes:
          >[color=green]
          > > I'm having problem in this case. I need to count how many time each
          > > word appears in one sentence. and I need it to be case insensitive
          > >
          > > for example when the input is
          > >
          > > The quick brown fox jumps over the lazy dog.
          > >
          > > and the output is
          > >
          > > the 2
          > > quick 1
          > > brown 1
          > > fox 1
          > > jumps 1
          > > over 1
          > > lazy 1
          > > dog 1
          > >
          > > anyone can help me to solve this problem?
          > > an example may help me a lot.[/color]
          >
          > The question cries out for a tree. Trees are discussed in , AFAIK, all
          > books on data structures.[/color]

          I'd have said it calls out for a hashtable mapping strings to counts.
          But hey, There's More Than One Way To Do It, to borrow from another
          language.

          That said, since the homework question said "one sentence", any
          algorithm, even a dynamic array of structs each having a string
          and count would be just fine...

          To the OP, look at "tolower", "ispunct", and perhaps "strtok".
          Figure out some way to store your lower cased punctation
          stripped words and an associated count, and to search through
          it when adding a word to see if it is a duplicate. Post what
          you come up with, and you will no doubt get some help if you
          have made an effort.

          -David

          Comment

          • akarl

            #6
            Re: word count

            David Resnick wrote:[color=blue]
            > osmium wrote:
            >[color=green]
            >>"jwvai316" writes:
            >>
            >>[color=darkred]
            >>>I'm having problem in this case. I need to count how many time each
            >>>word appears in one sentence. and I need it to be case insensitive
            >>>
            >>>for example when the input is
            >>>
            >>>The quick brown fox jumps over the lazy dog.
            >>>
            >>>and the output is
            >>>
            >>>the 2
            >>>quick 1
            >>>brown 1
            >>>fox 1
            >>>jumps 1
            >>>over 1
            >>>lazy 1
            >>>dog 1
            >>>
            >>>anyone can help me to solve this problem?
            >>>an example may help me a lot.[/color]
            >>
            >>The question cries out for a tree. Trees are discussed in , AFAIK, all
            >>books on data structures.[/color]
            >
            >
            > I'd have said it calls out for a hashtable mapping strings to counts.
            > But hey, There's More Than One Way To Do It, to borrow from another
            > language.[/color]

            OK, this is already off topic, but...how do you traverse the hashtable
            to display the result? (I guess the OP would actually want a sorted output.)

            August

            Comment

            • Randy Howard

              #7
              Re: word count

              akarl wrote
              (in article <CU2Me.144162$d P1.498707@newsc .telia.net>):
              [color=blue]
              > David Resnick wrote:[color=green]
              >> osmium wrote:
              >>[color=darkred]
              >>> "jwvai316" writes:
              >>>
              >>>
              >>>> I'm having problem in this case. I need to count how many time each
              >>>> word appears in one sentence. and I need it to be case insensitive
              >>>>
              >>>> for example when the input is
              >>>>
              >>>> The quick brown fox jumps over the lazy dog.
              >>>>
              >>>> and the output is
              >>>>
              >>>> the 2
              >>>> quick 1
              >>>> brown 1
              >>>> fox 1
              >>>> jumps 1
              >>>> over 1
              >>>> lazy 1
              >>>> dog 1
              >>>>
              >>>> anyone can help me to solve this problem?
              >>>> an example may help me a lot.
              >>>[/color][/color][/color]
              [color=blue]
              > OK, this is already off topic, but...how do you traverse the hashtable
              > to display the result? (I guess the OP would actually want a sorted output.)[/color]

              His example didn't show the output in sorted form, so why would
              you guess that?

              --
              Randy Howard (2reply remove FOOBAR)

              Comment

              • CBFalconer

                #8
                Re: word count

                akarl wrote:[color=blue]
                > David Resnick wrote:[color=green]
                >> osmium wrote:[color=darkred]
                >>> "jwvai316" writes:
                >>>
                >>>> I'm having problem in this case. I need to count how many time
                >>>> each word appears in one sentence. and I need it to be case
                >>>> insensitive
                >>>>
                >>>> for example when the input is
                >>>>
                >>>> The quick brown fox jumps over the lazy dog.
                >>>>
                >>>> and the output is
                >>>>
                >>>> the 2
                >>>> quick 1
                >>>> brown 1
                >>>> fox 1
                >>>> jumps 1
                >>>> over 1
                >>>> lazy 1
                >>>> dog 1
                >>>>
                >>>> anyone can help me to solve this problem?
                >>>> an example may help me a lot.
                >>>
                >>> The question cries out for a tree. Trees are discussed in ,
                >>> AFAIK, all books on data structures.[/color]
                >>
                >> I'd have said it calls out for a hashtable mapping strings to
                >> counts. But hey, There's More Than One Way To Do It, to borrow
                >> from another language.[/color]
                >
                > OK, this is already off topic, but...how do you traverse the
                > hashtable to display the result? (I guess the OP would actually
                > want a sorted output.)[/color]

                You download my portable hashlib and compile and run the demo
                wdfreq program.

                <http://cbfalconer.home .att.net/download/hashlib.zip>

                [1] c:\c\hashlib>wd freq
                Usage: wdfreq < inputfile > outputfile
                collects all words in inputfile and outputs a
                sorted (by frequency) list of words and the
                frequency of their occurences, ignores case.

                Signal EOF to terminate (^D or ^Z usually)
                Now is the time for all good men to come to the aid of the party.
                The quick brown fox jumped over the lazy hound dogs.
                ^Z
                26 words, 21 entries, 59 probes, 18 misses
                5 the
                2 to
                1 aid
                1 all
                1 brown
                1 come
                1 dogs
                1 for
                1 fox
                1 good
                1 hound
                1 is
                1 jumped
                1 lazy
                1 men
                1 now
                1 of
                1 over
                1 party
                1 quick
                1 time

                --
                "If you want to post a followup via groups.google.c om, don't use
                the broken "Reply" link at the bottom of the article. Click on
                "show options" at the top of the article, then click on the
                "Reply" at the bottom of the article headers." - Keith Thompson


                Comment

                • akarl

                  #9
                  Re: word count

                  CBFalconer wrote:[color=blue]
                  > akarl wrote:[color=green]
                  >>OK, this is already off topic, but...how do you traverse the
                  >>hashtable to display the result? (I guess the OP would actually
                  >>want a sorted output.)[/color]
                  >
                  > You download my portable hashlib and compile and run the demo
                  > wdfreq program.
                  >
                  > <http://cbfalconer.home .att.net/download/hashlib.zip>
                  >
                  > [1] c:\c\hashlib>wd freq
                  > Usage: wdfreq < inputfile > outputfile
                  > collects all words in inputfile and outputs a
                  > sorted (by frequency) list of words and the
                  > frequency of their occurences, ignores case.
                  >
                  > Signal EOF to terminate (^D or ^Z usually)
                  > Now is the time for all good men to come to the aid of the party.
                  > The quick brown fox jumped over the lazy hound dogs.
                  > ^Z
                  > 26 words, 21 entries, 59 probes, 18 misses
                  > 5 the
                  > 2 to
                  > 1 aid
                  > 1 all
                  > 1 brown
                  > 1 come
                  > 1 dogs
                  > 1 for
                  > 1 fox
                  > 1 good
                  > 1 hound
                  > 1 is
                  > 1 jumped
                  > 1 lazy
                  > 1 men
                  > 1 now
                  > 1 of
                  > 1 over
                  > 1 party
                  > 1 quick
                  > 1 time[/color]

                  OK, so you sort the items in a separate phase. With a binary search tree
                  (BST) you get the output sorted by word for free with an in-order
                  traversal. If the items doesn't need to be sorted or are to be sorted
                  by frequency (and you must implement the sorting) the BST approach is
                  slower however (though simpler).

                  August

                  Comment

                  • CBFalconer

                    #10
                    Re: word count

                    akarl wrote:[color=blue]
                    > CBFalconer wrote:[color=green]
                    >> akarl wrote:[/color]
                    >[color=green][color=darkred]
                    >>> OK, this is already off topic, but...how do you traverse the
                    >>> hashtable to display the result? (I guess the OP would actually
                    >>> want a sorted output.)[/color]
                    >>
                    >> You download my portable hashlib and compile and run the demo
                    >> wdfreq program.
                    >>
                    >> <http://cbfalconer.home .att.net/download/hashlib.zip>
                    >>
                    >> [1] c:\c\hashlib>wd freq
                    >> Usage: wdfreq < inputfile > outputfile
                    >> collects all words in inputfile and outputs a
                    >> sorted (by frequency) list of words and the
                    >> frequency of their occurences, ignores case.
                    >>[/color][/color]
                    .... snip usage example ...[color=blue]
                    >
                    > OK, so you sort the items in a separate phase. With a binary search
                    > tree (BST) you get the output sorted by word for free with an
                    > in-order traversal. If the items doesn't need to be sorted or are
                    > to be sorted by frequency (and you must implement the sorting) the
                    > BST approach is slower however (though simpler).[/color]

                    Try your simple binary tree with sorted input. O(n*n).

                    --
                    "If you want to post a followup via groups.google.c om, don't use
                    the broken "Reply" link at the bottom of the article. Click on
                    "show options" at the top of the article, then click on the
                    "Reply" at the bottom of the article headers." - Keith Thompson


                    Comment

                    • Richard Heathfield

                      #11
                      Re: word count

                      CBFalconer wrote:
                      [color=blue]
                      > akarl wrote:[color=green]
                      >>
                      >> OK, so you sort the items in a separate phase. With a binary search
                      >> tree (BST) you get the output sorted by word for free with an
                      >> in-order traversal. If the items doesn't need to be sorted or are
                      >> to be sorted by frequency (and you must implement the sorting) the
                      >> BST approach is slower however (though simpler).[/color]
                      >
                      > Try your simple binary tree with sorted input. O(n*n).[/color]


                      How a do in often order? sentence sorted write you


                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      mail: rjh at above domain

                      Comment

                      Working...