Solved

keyword Matching

Posted on 2000-05-08
7
250 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl to mysql 5 128
perl match and sort unique result 2 122
SIMPLE Perl Regex 1 144
Perl tar error 8 31
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.…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

21 Experts available now in Live!

Get 1:1 Help Now