Solved

Counting diagraphs and trigraphs in Perl

Posted on 2004-08-21
2
427 Views
Last Modified: 2010-07-27
Hi,
   I have a string in perl say    "hh bbb. rt irr"

   I'm looking for some way of going through that string and counting how many three letter words there are and how many two letter words. punctuation should be ignored....


 Any takers? :)


0
Comment
Question by:petepalmer
2 Comments
 
LVL 10

Expert Comment

by:rj2
Comment Utility
$text="hh bbb. rt irr";
@words = split(/[\s.,;:\'\"\\\/\`\~\!\@\#\$\%\^\& \*\(\)\_\- \+\+\[\]\{\}\?]+/,$text);
foreach(@words) {      
      $twocharwords++ if length($_)==2;
      $threecharwords++ if length($_)==3;
}
print "two char word count: $twocharwords, three char word count: $threecharwords\n";
0
 
LVL 20

Accepted Solution

by:
jmcg earned 250 total points
Comment Utility
(spelling error in title corrected)

Actually, the words digraph and trigraph are usually used with a different meaning: they refer to two-character and three-character sequences regardless of whether they appear as independent words or as part of larger words. The statistics of digraphs and trigraphs can be used in compression algorithms and in code breaking.

If we take from your question that you want to count the number of two- and three-character sequences (sequences, that is, containing alphabetic characters only), the approach that rj2 has shown is pretty good. If I were doing it, I'd probably do it more like:

my @words = $text =~ m{\b([a-zA-Z]+)\b}g;
my @histogram;
$histogram[length]++ foreach @words;
print "length\tcount\n";
printf "%5d\t%5d\n" $_, $histogram[$_] foreach (2..3);

or, change that last line to get the entire histogram,

printf "%5d\t%5d\n" $_, $histogram[$_] foreach (1 .. $#histogram);

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now