Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

keyword Matching

Posted on 2000-05-08
7
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
maneshr earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

604 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