Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grammar rule implementation in java

I need some logic to find a grammatical pattern like in a sentence:

[adjective]* [noun]+ [hyphen] [verb Past Participle | verb Present Participle | one of the special adjectives] [adjective]* [noun]+

where * means any number (0 or more), ? means 0 or 1, and + means 1 or more, | means or.

If i give any input sentence the logic has to search if it contains the above pattern or not. I completely have no idea how to begin. Please if anyone could suggest me with some logic.

like image 944
Suneeta Singh Avatar asked Jan 15 '23 21:01

Suneeta Singh


2 Answers

This is pseudo code. It makes 2 passes on the input, in the first pass it converts each word in the input string to a letter which refers to its type, and on the second pass you match the result of the first pass with your regular expression.

method(input) {
    typed_input = '';
    for (word in input) {
        if (word is noun) {
            typed_input += 'n'
        else if (word is adjective)
            typed_input += 'a'
        else if (word is hyphen)
            typed_input += 'h'
        else if (word is verb Past Participle)
            typed_input += 'v'
        else if (word is verb Present Participle)
            typed_input += 'p'
        else if (word is one of the special adjectives)
            typed_input += 's'
        else
           throw exception("invalid input")
    }
    return typed_input.match("a*n+h[v|p|s]a*n+")
}
like image 149
Isaac Avatar answered Jan 23 '23 08:01

Isaac


This may help you. Link to standford parser. Also You can download the code in java.

like image 31
Kasun Kariyawasam Avatar answered Jan 23 '23 08:01

Kasun Kariyawasam