Creating Frequency of words from a file by accessing a corpus


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating Frequency of words from a file by accessing a corpus
# 1  
Old 07-23-2013
Creating Frequency of words from a file by accessing a corpus

Hello,
I have a large file of syllables /strings in Urdu. Each word is on a separate line.
Example in English:
Code:
be
at
for
if
being
attract

I need to identify the frequency of each of these strings from a large corpus (which I cannot attach unfortunately because of size limitations) and identify the frequency of each string.
Is there a perl or awk script which can do the job.
Many thanks for your help
# 2  
Old 07-23-2013
Code:
$ awk '{txt[$0]++} END{for (i in txt) { printf "%-8d %s\n"  ,txt[i],i }}' list
1        attract
1        if
1        at
1        being
1        for
1        be

# 3  
Old 07-24-2013
You could also use sort and uniq like this:

Code:
$ sort corpus | uniq -c
      1 at
      1 attract
      1 be
      1 being
      1 for
      1 if

# 4  
Old 07-24-2013
Quote:
Originally Posted by gimley
...I need to identify the frequency of each of these strings from a large corpus (which I cannot attach unfortunately because of size limitations) and identify the frequency of each string.
...
Code:
$ 
$ # "wordlist.txt" is a list of words that we have to check
$ cat wordlist.txt
be
at
for
if
being
attract
$ 
$ # "poe_the_gold_bug.txt" is a text file against which we have to
$ # check the words. This file contains the story "The Gold Bug" by
$ # Edgar Allen Poe from the Project Gutenberg website.
$ wc poe_the_gold_bug.txt
 1460 13462 76460 poe_the_gold_bug.txt
$ 
$ # A Perl program to check the frequency of words from "wordlist.txt"
$ # in the file "poe_the_gold_bug.txt"
$ cat -n word_occurrences.pl
     1    #!/usr/bin/perl -w
     2    use strict;
     3    my $wordfile = $ARGV[0];
     4    my $testfile = $ARGV[1];
     5    my %occurrences;
     6    open(WF, "<", $wordfile) or die "Can't open $wordfile: $!";
     7    while (<WF>) {
     8      chomp;
     9      $occurrences{$_} = 0
    10    }
    11    close(WF) or die "Can't close $wordfile: $!";
    12    open(TF, "<", $testfile) or die "Can't open $testfile: $!";
    13    while (<TF>) {
    14      chomp;
    15      while (/(\w+)/g) {
    16        $occurrences{$1}++ if defined $occurrences{$1};
    17      }
    18    }
    19    close(TF) or die "Can't close $testfile: $!";
    20    while (my ($k, $v) = each %occurrences) {
    21      printf("%-10s occurs %5d times\n", $k, $v);
    22    }
$ 
$ # Execution of the Perl program
$ perl word_occurrences.pl wordlist.txt poe_the_gold_bug.txt
attract    occurs     0 times
for        occurs   109 times
be         occurs    72 times
at         occurs    96 times
being      occurs    13 times
if         occurs    24 times
$ 
$

# 5  
Old 07-24-2013
Hello,
I tried the awk script but it does not work.
I created a file called txt which is the source file for which the frequencies have to be found
Code:
eng
book
shop
writ

and a large file of English words which I am appending as a zip for testing.
The idea is that the script should find the strings provided in the input file and spew out all words containing their frequency.
Thus in the corpus 1134 instances of eng were detected (did this in Ultraedit) and a sample output desired is provided below:
Code:
eng=1134
engine
strength
revenge
engaged
challenge
passengers
engineer
engagement
engines
messenger
length
vengeance
passenger
engage
avenge
engineering
engine
engineers
Deng
challenged
challenging
penguin

Many thanks for the help. Please note that I cannot use Unix tools since I work in Windows/DOS.Smilie
# 6  
Old 07-24-2013
Looks like "grep" returns a different count for "eng" than UltraEdit. But the counts determined by grep and Perl are consistent.

Code:
$ 
$ cat words.txt
eng
book
shop
writ
$ 
$ 
$ grep eng en.txt | wc -l
1123
$ 
$ grep book en.txt | wc -l
220
$ 
$ grep shop en.txt | wc -l
147
$ 
$ grep writ en.txt | wc -l
176
$ 
$ # The Perl program
$ cat -n word_frequency.pl
     1    #!/usr/bin/perl -w
     2    use strict;
     3    my $wordfile = $ARGV[0];
     4    my $testfile = $ARGV[1];
     5    my %occurrences;
     6    open(WF, "<", $wordfile) or die "Can't open $wordfile: $!";
     7    while (<WF>) {
     8      chomp;
     9      $occurrences{$_} = 0
    10    }
    11    close(WF) or die "Can't close $wordfile: $!";
    12    open(TF, "<", $testfile) or die "Can't open $testfile: $!";
    13    while (<TF>) {
    14      chomp(my $word = $_);
    15      foreach my $k (keys %occurrences) {
    16        $occurrences{$k}++ if $word =~ /$k/
    17      }
    18    }
    19    close(TF) or die "Can't close $testfile: $!";
    20    while (my ($k, $v) = each %occurrences) {
    21      printf("%-10s occurs %5d times\n", $k, $v);
    22    }
$ 
$ # "en.txt" is the file you attached in your post
$ perl word_frequency.pl words.txt en.txt
shop       occurs   147 times
book       occurs   220 times
writ       occurs   176 times
eng        occurs  1123 times
$ 
$

This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 07-24-2013
In awk, this might be long-winded

Code:
bash-3.2$ cat list
be
at
for
if
being
attract
bash-3.2$ cat input
at
be
bash-3.2$ 
bash-3.2$ 
bash-3.2$ awk 'BEGIN { while((getline line < "input") > 0) { pat[line] = 0 } }  { for(x in pat) { if($0 ~ x) { pat[x]++; matched[x,pat[x]]=$0; } } }  END { for (x in pat) { print x"="pat[x]; for (c=1; c<=pat[x]; c++) { print matched[x,c] } }}' list
be=2
be
being
at=2
at
attract

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. HP-UX

Problems creating and accessing with user

Hi, I have created the user 'mastersa' in several servers. I need to change the user ID to '0'. However, after doing this, I am not able to login (Access denied). Even after I change the password, I still get this error. Why is this? Also, when I attempt to delete the user account, I get... (5 Replies)
Discussion started by: anaigini45
5 Replies

3. Shell Programming and Scripting

Frequency of Words in a File, sed script from 1980

tr -cs A-Za-z\' '\n' | tr A-Z a-z | sort | uniq -c | sort -k1,1nr -k2 | sed ${1:-25} < book7.txt This is not my script, it can be found way back from 1980 but once it worked fine to give me the most used words in a text file. Now the shell is complaining about an error in sed sed: -e... (5 Replies)
Discussion started by: 1in10
5 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

6. Shell Programming and Scripting

Assigning the same frequency to more than one words in a file

I have a file of names with the following structure NAME FREQUENCY NAME NAME FREQUENCY NAME NAME NAME FREQUENCY i.e. more than one name is assigned the same frequency. An example will make this clear SANDHYA DAS 6901 ARATI DAS 6201 KALPANA DAS 4714 GITA DAS 4550 BISWANATH DAS 3949... (4 Replies)
Discussion started by: gimley
4 Replies

7. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

8. Shell Programming and Scripting

count frequency of words in a file

I need to write a shell script "cmn" that, given an integer k, print the k most common words in descending order of frequency. Example Usage: user@ubuntu:/$ cmn 4 < example.txt :b: (3 Replies)
Discussion started by: mohit_iitk
3 Replies

9. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

10. Shell Programming and Scripting

Creating String from words in a file

Hi i have a file called search.txt Which contains text like Car Bus Cat Dog Now i have to create a string from the file which should look like Car,Bus,Cat,Dog ( appending , is essential part) String must be stored in some variable so i can pass it as argument to some other... (5 Replies)
Discussion started by: deepakthaman
5 Replies
Login or Register to Ask a Question