Solved

keyword Matching

Posted on 2000-05-08
7
251 Views
Last Modified: 2010-03-05
I want to know how to match each word  a user types in against each keyword in an array.  Then to output the amount of times a word was successfully matched to one of the keywords!
0
Comment
Question by:shels
  • 4
  • 3
7 Comments
 

Author Comment

by:shels
ID: 2789041
This is part of the code I'm already using but it does not produce the desired output.  I want it just to output one line saying the amount of times keywords were found, but this code outputs a line for each keyword matched.
Any ideas on how I should ammend the code or maybe a better way to do it??

while ($string =~ /(\S+)/g) {  # groups of non-whitespace characters
    if (exists $word{$1}) {      # $1 is that group of characters
      $word{$1}++;               # increment the times it was found
    }
  }
print "$word{$_} asthma keyword(s) were found in the heading.\n";
for (@aswordlist) {
    if ($word{$_}) {  # if there was at least 1 match for the word
    print "$word{$_} asthma keyword(s) were found in the heading.\n";
    }
  }
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2789132
".....but this code outputs a line for each keyword matched. "

as per your code this is how it is supposed to work.

i think the @aswordlist array has the list of your keywords. Now you are using the foreach loop (viz. for (@aswordlist) { )and going thru EVERY element of that array!!

if you want to print specific keyword, then you will have to first check if the keyword you are looking for is indeed the one that is being processed currently in the loop.

here are 2 examples.

the first shows all elements of the array.

@aswordlist=("allergy","nasal","flu","flonase");
foreach(@aswordlist){
  print $_,"\n";
}


The foll shows ONLY what you asked for.
$what="nasal";

@aswordlist=("allergy","nasal","flu","flonase");
foreach(@aswordlist){
  print $_,"\n" if /^$what$/;
}

So the above will ONLY print nasal, since that is string you are looking for.

So basically in the code you have posted above, you need to use a variable similar to $what and print the results ONLY when a match is found.

Hope that helps.
0
 

Author Comment

by:shels
ID: 2789191
Thanks a mil. that does help me a bit, but what I actually want to output is the number of times keywords were found in the text, not any one keyword.  
At the moment my output for text containing three different keywords, looks like this:
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.

but I want the output to look like the foll:

3 asthma keyword(s) were found in the heading

Thanks again for your help!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2789232
from the looks of it, the fix might be very simple.

try this...

## variable that keeps a total of all the keywords found!!
$total_count=0;
for (@aswordlist) {
  if ($word{$_}) {  # if there was at least 1 match for the word
    $total_count++;
  }
}

## NOW, print the results..
print "$total_count asthma keyword(s) were found in the heading.\n";
0
 

Author Comment

by:shels
ID: 2789243
Thanks a mil. that does help me a bit, but what I actually want to output is the number of times keywords were found in the text, not any one keyword.  
At the moment my output for text containing three different keywords, looks like this:
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.

but I want the output to look like the foll:

3 asthma keyword(s) were found in the heading

Thanks again for your help!
0
 

Author Comment

by:shels
ID: 2789267
exactly what I was looking for, thanks man!!!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2789300
most welcome :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Web Database 5 111
SIMPLE Perl Regex 1 153
Perl strange behaviour 5 69
perl split by | 2 90
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

16 Experts available now in Live!

Get 1:1 Help Now