Find the answer to your Linux question:
Results 1 to 4 of 4
hi all, I want to compare two sentences and find the highest co-occurrence of words. For instance the result for the: Code: This phrase has five words. There are more ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    46

    Perl - Count the number of subwords occurrence


    hi all,

    I want to compare two sentences and find the highest co-occurrence of words. For instance the result for the:
    Code:
    This phrase has five words.
    There are more words in this phrase.
    should be 2 (this phrase) Please note that there are words like my:home. In that case this count as one word

    Thank you in advance.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    okay, this sounds like homework...yes?

    if not, show your work! what have you tried?

  3. #3
    Just Joined!
    Join Date
    Apr 2012
    Posts
    46
    hi,
    No, this is not a homework. I have already prepare the following script, but this counts the max co-occurrence of strings. How can I count the words?
    Thanks in advance.

    Code:
    #! perl -slw
    use strict;
    
    sub lcs{
        my $strings = join "\0", @_;
        my $lcs;
        for my $n ( 1 .. length $strings ) {
            my $re = "(.{$n})" . '.*\0.*\1' x ( @_ - 1 );
            last unless $strings =~ $re;
            $lcs = $1
        }
        return $lcs;
    }
    
    my @a = <DATA>;
    chomp @a;
    
    print "lcs: ", lcs( @a );
    
    __DATA__
    This phrase has five words.
    There are more words in this phrase.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    try this:
    Code:
    for(@a){
      my $cnt = () = $_ =~ /\W/gi;
      print "LINE: $_ ($cnt)\n";
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •