MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Count Ouccrence of words in a long text

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118999] Re: Count Ouccrence of words in a long text
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 19 May 2011 07:42:47 -0400 (EDT)

>        unique=Union[words];
>        counts=Count[words,#]&/@unique;

... is the same as

{unique,counts} = Transpose@Sort@Tally@words

Bobby

On Wed, 18 May 2011 06:18:26 -0500, Murray Eisenberg  
<murray at math.umass.edu> wrote:

> Here's one approach, which I've encapsulated in a Module for convenience:
>
>    wordCounts[txt_] :=
>      Module[{words,unique,counts},
>        words=StringCases[ToLowerCase[txt],WordCharacter..];
>        unique=Union[words];
>        counts=Count[words,#]&/@unique;
>        Reverse@SortBy[Transpose[{unique,counts}],Last]
>    ]
>
>    (* example *)
>    story = ExampleData[{"Text", "AliceInWonderland"}];
>    wordCounts[story]
>
> {{"the", 632}, {"and", 338}, {"a", 278}, {"to", 252}, {"she",
>    242}, {"of", 199},...
>
> If you want a nice table printout, just use TableForm:
>
>     wordCounts[story] // TableForm
>
> There's at least one anomaly: the "s" at the end of possessives is split
> off as a separate word.
>
> On 5/17/2011 7:47 AM, Yako wrote:
>> Hello,
>>
>> First of all I am pretty new to Mathematica, so excuse me if this has
>> a simple answer.
>>
>> What I need is to be able to count the occurrence of each word of a
>> text and count the times each word appears on it. I know how to do
>> this on other languages but I am trying to achieve it with
>> mathematica.
>>
>> Can someone hint me the way to go?
>>
>> Thanks!
>>
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: TeXForm: slight change in behaviour from version 7 to version 8
  • Next by Date: Re: Count Ouccrence of words in a long text
  • Previous by thread: Re: Count Ouccrence of words in a long text
  • Next by thread: Re: Count Ouccrence of words in a long text