0

I am using the Lingua::EN::Tagger Perl module in order to tag parts of speech from a user's input. That portion of my code works perfect. However, the problem is that I only want to keep the input that has the noun tags which are "NN, NNS, NNP, NNPS", and store these words in a separate array @nounArray. The user will be inputting a question such as "what is your name?" Each element of the question will be tagged: What/WP is/is your/PN name/NN

my @UserInput = $readable_text;
my @nounArray;
foreach my $UserInput (@UserInput){

    if ($UserInput =~ m/NN|NNS$|NNP$|NNPS$/){

              $UserInput = @nounArray;
    }
print @nounArray;
}

However, nothing occurs when I run the code. The goal is to have the nouns of the user's input be placed in a separate array after separating them from the original array. I do not want to print the array, but i do this in order to see if the code was working.

3
  • Why you added $ in your regex? Commented Jun 30, 2014 at 16:20
  • @AvinashRaj, the reason i added the $ at the end is because the tags occur at the end of the words. Commented Jun 30, 2014 at 16:24
  • An example of the input would be "Can you turn off the lights?" It will be some form of a question. @mpapec Commented Jun 30, 2014 at 16:25

3 Answers 3

1

Since you want to iterate over the words in $readable_text you can split them first into array,

my $readable_text = "What/WP is/is your/PN name/NN";
my @UserInput = split ' ', $readable_text;
my @nounArray;
foreach my $UserInput (@UserInput) {

  if ($UserInput =~ m/NN|NNS$|NNP$|NNPS$/) {
    # print "$UserInput\n";
    push @nounArray, $UserInput;
  }
}

print @nounArray;
Sign up to request clarification or add additional context in comments.

4 Comments

I don't just want to print the user input, I need to store the words with the noun tags in a separate array.
I tried the code with the update, it still is not giving me anything.There may be a problem with the regex, however I am not sure because i am getting no error.
@brilliantB check again. btw, which words do you want to catch in What/WP is/is your/PN name/NN?
the nouns.. for example name has the tag /NN therefore this would be considered a noun because they have the tags /NN /NNS /NNP /NNPS each stands for noun, nouns, proper noun, and proper nouns respectively.
0

$ matches at the end of the string. I suppose your strings have at least a \n at the end, which would prevent them from matching.

But as you point out in your comment, it looks like you're trying to match word boundaries here, so just replace all $ in your expression with \b.

1 Comment

Yes I tried this and with a manipulation of my code it helped to only get the words that had /NN taggers. I removed all other taggers but kept only that one in my regex.
0

First, split your words by whitespace:

my @UserInput = split /\s+/, $UserInput;

Then grep for the nouns:

my @nouns = grep { m%/N% } @UserInput;   # only noun tags include /N

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.