counting words

I know how to count words and its frequency in a text file.
However, I don not know how to count the words and its frequency in different paragraphs.

Any suggestions greatly appreciated.
Thanks
sdesarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

monasCommented:
perl -00 -n -e '@w = /\w+/g;print $#w."\n";' list_of_files

will print number of words in each paragraph. WOrk on array @w and you can get frequency of words in the paragraph.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sdesarAuthor Commented:
I tried this but I am receiving an error message as follow-
Unrecognized file test: -n at line 3

I made some assumptions-
I typed
#!/usr/bin/perl

per -00 -n -e '@.........."\n"list_of_files

I am assuming that the
list_of_files is a file that cointains the text.

I thougt that there was a semicolon missing after list_of_files;

However, that didn't solve it either.

Any other suggestions?
0
monasCommented:
NOOOOOOO!

If you like to write contens in file, then write to cnt.pl

#!/usr/bin/perl -00 -n
@w = /\w+/g;print $#w."\n";


and from command line call

cnt.pl file_1 file_2 file_3

where file_X is name of file with text where you want to count words in paragraph.

      Good look
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

sdesarAuthor Commented:
This seems to count the words only and not the words and its frequency in the individual parahs.

Anything else that I should do?

PS. Thanks for your time on these suggestions.
0
monasCommented:
Well, you have all the words in @w array. If you want frequency then add

map { $wc{$_}++; } @w;
foreach $wd(keys %wc){print $wd.':'.$wc{$wd}."\n";}
print "---------------\n";

This will additionaly print you every word and number of occurences of this word in the paragraph
0
ozoCommented:
What's a word?
0
sdesarAuthor Commented:
I test this and it works.
How do I list the paragraph numbers-
ie Parah1
   word      freq
   Parah2
   word      freq
0
monasCommented:
#!/usr/bin/perl -00 -n
print "Parah ".$..":";
@w = /\w+/g;print $#w."\n";
map { $wc{$_}++; } @w;
foreach $wd(keys %wc){print  "\t".$wd."\t".$wc{$wd}."\n";}
0
sdesarAuthor Commented:
Thanks monas!!
I gave U excellent points.  
Have they been recorded?
0
monasCommented:
Yes, TNX
0
sdesarAuthor Commented:
How can I use perl for
word recognition?

Example - If there are a bunch of words in a text file like -

this text is derived from the book and to see from information on deriving check out the textbook.

Since derived and deriving stem from the root - derive.  How can I use perl to parse the text and recognize DERIVE.
0
ozoCommented:
use Lingua::Stem qw(:all);
set_locale('en');
#add_exceptions({derived=>'DERIVE', deriving=>'DERIVE'});
#print "@{stem(qw(Since derived and deriving stem from the root - derive.  How can I use perl to parse the text and recognize DERIVE'))}\n";
print "@{stem(map{/(\w+)/g}<>)}\n";
0
sdesarAuthor Commented:
Thanks -oza !!  
It works as expected.  

OZO or MONAS-
The parah & word counting program - I am implementing it in a web application.
I wanted to know how will this routine handle
multiple files.
That is if I have one text file_1.in that I want the
word and freq. count on and save it in file_1.out
And then if I want to generate a similar count on another file_2.in and store the results in file_2.out.
What's the efficient way to be able to handle a freq. count on multiple files?
Also, is map() a function in perl and is it using a LIST Data Structure for perform word counts?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.