大家好,我有一个代码可以从解析结果中获取句子的第一个名词。我写了以下代码。但似乎有些问题。if 语句不会中断 for 循环。任何人都可以帮我解决它吗?
提前致谢。
public static String Find_Noun(Parse p)
{
label:
for(Parse k:p.getChildren())
{
if((k.getType()).equals("NN")||(k.getType()).equals("NNP")||
(k.getType()).equals("NNS")||(k.getType()).equals("NNPS"))
{
noun=k.toString();
System.out.println(noun);
break label; // I am aware that label is not needed,
// but it doesn't work either way.
}
else
{
System.out.println("else "+k);
Find_Noun(k);
}
}
return noun;
}
输入:
成为\VBD a\DT常规\JJ客户\NN of\IN a\DT郊区\JJ花园\NN
输出是:
else became
else became
else a regular customer of a suburban garden
else a regular customer
else a
else a
else regular
else regular
customer \\This is the string to be extracted
else of a suburban garden
else of
else of
else a suburban garden
else a
else a
else suburban
else suburban
garden
garden