How to match all array contents and display all highest matched sentences in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to match all array contents and display all highest matched sentences in perl?
# 1  
Old 06-13-2009
How to match all array contents and display all highest matched sentences in perl?

Hi,

I have an array with 3 words in it and i have to match all the array contents and display the exact matched sentence i.e all 3 words should match with the sentence.

Here are sentences.
Code:
$arr1="Our data suggests that epithelial shape and growth control are unequally affected depending on how wt p53 function is impaired and whether partial or full tumor suppressor activity is lost";

$arr2="The growth of epithelial tissue is downregulation";

I want all there 3 words to be matched and that sentence has to printed first.

Here are the words. I tried like this:

Code:
$array="epithelial,growth,downregulation";
@split=split(",",$array);
foreach $word(@split)
{
    if($arr1=~/\b$word\b/i || $arr2=~/\b$word\b/i)
    {
         print "<br> Matched <br>";
    }

}

Just stuck up here!!

The output should be like this:

Code:
The growth of epithelial tissue is downregulation 

Our data suggests that epithelial shape and growth control are unequally affected depending on how wt p53 function is impaired and whether partial or full tumor suppressor activity is lost

How can i match all these words and print the sentence with highest match (all the words matching the sentence) in perl??

Any idea???

Regards
Vanitha
# 2  
Old 06-13-2009
Quote:
Originally Posted by vanitham
...
How can i match all these words and print the sentence with highest match (all the words matching the sentence) in perl??
...
Maybe something like this:

Code:
$
$ cat -n data.txt
     1  Our data suggests that epithelial shape and growth control are unequally affected depending on how wt p53 function is impaired and whether partial or full tumor suppressor activity is lost
     2  The growth of epithelial tissue is downregulation
     3  The quick brown fox jumps over the lazy dog.
$
$ perl -ne 'BEGIN{@x=split /,/, "epithelial,growth,downregulation"} { chomp;
>   print $_,"\n" if $_ =~ /\b$x[0]\b/ && $_ =~ /\b$x[1]\b/ && $_ =~ /\b$x[2]\b/
> }' data.txt
The growth of epithelial tissue is downregulation
$
$

Quote:

The output should be like this:

Code:
The growth of epithelial tissue is downregulation

Our data suggests that epithelial shape and growth control are unequally affected depending on how wt p53 function is impaired and whether partial or full tumor suppressor activity is lost
I don't know why you want the line "Our data suggests..." to be displayed as well. It does not have the word "downregulation" in it and as per your search criteria, it should not be displayed.

tyler_durden
# 3  
Old 06-15-2009
Hi,

Thanks for the reply.

I have words and sentences in an array i.e

Code:
@arr1=("epithelial","downregulation","growth");

@arr2=("Our data suggests that epithelial shape and growth control are unequally affected depending on how wt p53 function is impaired and whether partial or full tumor suppressor activity is lost","The growth of epithelial tissue is downregulation");

foreach $word(@arr1)
{
     foreach $arr(@arr2)
    {
       if($arr=~/\b$word\b/i)
       {
           print "<br>matched<br>";
       }
   }
}

In such case how can i get the maximum matched sentence???

The first sentence in the output matches all the words so that is the highest priority and it is printed first.

Next sentence matches with only 2 words so the next priority.


Output:
Code:
The growth of epithelial tissue is downregulation

Our data suggests that epithelial shape and growth control are unequally affected depending on how wt p53 function is impaired and whether partial or full tumor suppressor activity is lost

How to get the desired highest maximum sentence ??

Any solutions ???

Regards
Vanitha
# 4  
Old 06-16-2009
Quote:
Originally Posted by vanitham
...
In such case how can i get the maximum matched sentence???

The first sentence in the output matches all the words so that is the highest priority and it is printed first.

Next sentence matches with only 2 words so the next priority.


Output:
Code:
 
The growth of epithelial tissue is downregulation
 
Our data suggests that epithelial shape and growth control are unequally affected depending on how wt p53 function is impaired and whether partial or full tumor suppressor activity is lost

How to get the desired highest maximum sentence ??

...
"How to get the desired highest maximum sentence ??" - funny. Not sure if "maximum sentence" is desirable. Smilie

You may want to do something like this:

Code:
$
$ cat matches.pl
#!perl -w
@arr1=("epithelial","downregulation","growth");
@arr2=("Our data suggests that epithelial shape and growth control are unequally affected",
       "The growth of epithelial tissue is downregulation",
       "The quick brown fox jumps over the lazy dog",
       "The observed downregulation is due to elevated levels of insulin in the blood");
$x = '\\b'.join('\\b|\\b',@arr1).'\\b';
foreach (@arr2) {
  $matches{$_} = () = /$x/g;
}
foreach $i (reverse sort {$matches{$a} <=> $matches{$b}} @arr2) {
  print $i,"\n";
}
$
$ perl matches.pl
The growth of epithelial tissue is downregulation
Our data suggests that epithelial shape and growth control are unequally affected
The observed downregulation is due to elevated levels of insulin in the blood
The quick brown fox jumps over the lazy dog
$
$

HTH,
tyler_durden
# 5  
Old 06-17-2009
you may try below perl code

Code:
my $array="epithelial,growth,downregulation";
my @split=split(",",$array);
my ($reg,$str);
map {$str=sprintf("%s(?=.*%s)",$str,$_)} @split;
$reg=qr/^$str.*$/;
while(<DATA>){
	print if /$reg/;
}
__DATA__
The growth of epithelial tissue is downregulation
The downregulation growth of
The of epithelial tissue growth is downregulation
The downregulation growth of epithelial tissue is

# 6  
Old 06-18-2009
Quote:
Originally Posted by summer_cherry
you may try below perl code

Code:
my $array="epithelial,growth,downregulation";
my @split=split(",",$array);
my ($reg,$str);
map {$str=sprintf("%s(?=.*%s)",$str,$_)} @split;
$reg=qr/^$str.*$/;
while(<DATA>){
	print if /$reg/;
}
__DATA__
The growth of epithelial tissue is downregulation
The downregulation growth of
The of epithelial tissue growth is downregulation
The downregulation growth of epithelial tissue is

Hi,

Thanks a lot!!

Regards
Vanitha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract all the sentences that matched two patterns

Hi I have two lists of patterns named A and B consisting of around 200 entries in each and I want to extract all the sentences from a big text file which match atleast one pattern from both A and B. For example, pattern list A consists of : ama ani ahum mari ... ... and pattern... (1 Reply)
Discussion started by: my_Perl
1 Replies

2. Shell Programming and Scripting

Display array contents on a new line

ksh eg arrayname=(1 2 3 4 5) I'm trying to display the individual contents of an array on a new line without using a loop, using one line of code. output 1 2 3 4 5 (3 Replies)
Discussion started by: squrcles
3 Replies

3. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

4. Shell Programming and Scripting

Perl: array name tha contains contents of a variable

Hi there I have a counter called my $counter = 0; I am trying to build an array that will have a name that is for example my @array0 = ("some", "stuff"); but instead of hard coding the "0" in the array name i want to use whatever value the aforementioned $counter has in it...so ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

5. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

6. Shell Programming and Scripting

Perl help: Creating a multidimensional array of subdirectories and its contents

I'm currently working with dozens of FASTA files, and I'm tired of having to manually change the filename in my Perl script. I'm trying to write a simple Perl script that'll create a 2-dimensional array containing the name of the folders and its contents. For example, I would like the output... (6 Replies)
Discussion started by: shwang3
6 Replies

7. Shell Programming and Scripting

PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines, C:\searching4theseletters.txt a b c Read this into an array @ARRAY and then use this to compare against another file C:\inputletters.txt b o a c n a (9 Replies)
Discussion started by: bradleykins
9 Replies

8. UNIX for Dummies Questions & Answers

Display the contents of an array

Hi i have just registered So i am at university studying forensic computing and we have to learn c++ i have never done anything with c++ before and i am abit stuck i need to create a programme to display the contents of an array of characters forwards and in reverse Can anyone help me... (1 Reply)
Discussion started by: RossMc
1 Replies

9. Shell Programming and Scripting

Perl: array, assigning multi-word sentences with quotes

Just wondering if there's a better way to get these complete sentences into an array and keep the quotes intact? All the quotes make it look ugly to me but it works. I want to be able to refer to the full sentences by index. I've tried a few qw and qq/ aproaches but what I have below seems about... (4 Replies)
Discussion started by: gctaylor
4 Replies

10. Shell Programming and Scripting

How to get exact match sentences?

Hi, I have sentences like this: $sent= Protein modeling studies reveal that the RG-rich region is part of a three to four strand antiparallel beta-sheet, which in other RNA binding protein functions as a platform for nucleic acid interactions. Heterogeneous nuclear ribonucleoparticle... (19 Replies)
Discussion started by: vanitham
19 Replies
Login or Register to Ask a Question