Sun*_*ngh 4 java algorithm logic
我需要一些逻辑来找到句子中的语法模式:
[adjective]* [noun]+ [hyphen] [verb Past Participle | verb Present Participle | one of the special adjectives] [adjective]* [noun]+
Run Code Online (Sandbox Code Playgroud)
其中*表示任何数字(0或更多),?表示0或1,+表示1或更多,| 意思是.
如果我给出任何输入句子,逻辑必须搜索它是否包含上述模式.我完全不知道如何开始.如果有人能用某种逻辑建议我的话.
这是伪代码.它在输入上进行2次传递,在第一次传递中,它将输入字符串中的每个单词转换为引用其类型的字母,在第二次传递中,您将第一次传递的结果与正则表达式匹配.
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+")
}
Run Code Online (Sandbox Code Playgroud)