Grammar rule implementation in java
algorithm, java, logic
Solution
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+")
}
Problem
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.