我想从句子中提取名词并从POS标签中取回原始句子
//Extract the words before _NNP & _NN from below and also how to get back the original sentence from the Pos TAG.
Original Sentence:Hi. How are you? This is Mike·
POSTag: Hi._NNP How_WRB are_VBP you?_JJ This_DT is_VBZ Mike._NN
我试过这样的事情
String txt = "Hi._NNP How_WRB are_VBP you?_JJ This_DT is_VBZ Mike._NN";
String re1 = "((?:[a-z][a-z0-9_]*))"; // Variable Name 1
String re2 = ".*?"; // Non-greedy match on filler
String re3 = "(_)"; // Any Single Character 1
String re4 = "(NNP)"; // Word 1
Pattern p = Pattern.compile(re1 + re2 + re3 + re4, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find()) {
String var1 = m.group(1);
System.out.print( var1.toString() );
}
}
输出:嗨 但我需要一个句子中所有名词的清单。
答案 0 :(得分:5)
要提取名词,您可以这样做:
public static String[] extractNouns(String sentenceWithTags) {
// Split String into array of Strings whenever there is a tag that starts with "._NN"
// followed by zero, one or two more letters (like "_NNP", "_NNPS", or "_NNS")
String[] nouns = sentenceWithTags.split("_NN\\w?\\w?\\b");
// remove all but last word (which is the noun) in every String in the array
for(int index = 0; index < nouns.length; index++) {
nouns[index] = nouns[index].substring(nouns[index].lastIndexOf(" ") + 1)
// Remove all non-word characters from extracted Nouns
.replaceAll("[^\\p{L}\\p{Nd}]", "");
}
return nouns;
}
要提取原始句子,您可以这样做:
public static String extractOriginal(String sentenceWithTags) {
return sentenceWithTags.replaceAll("_([A-Z]*)\\b", "");
}
证明它有效:
public static void main(String[] args) {
String sentence = "Hi._NNP How_WRB are_VBP you?_JJ This_DT is_VBZ Mike._NN";
System.out.println(java.util.Arrays.toString(extractNouns(sentence)));
System.out.println(extractOriginal(sentence));
}
输出:
[Hi, Mike]
Hi. How are you? This is Mike.
注意:对于从提取的名词中删除所有非单词字符(如标点符号)的正则表达式,我使用 this Stack Overflow question/answer。
答案 1 :(得分:1)
使用while (m.find())
代替if (m.find())
来迭代所有匹配。
此外,你的正则表达式可以真正简化:
((?:...))
,这很奇怪:直接嵌套在捕获组中的非捕获组没有任何意义。.*?
部分是否符合预期。如果您想匹配一个点,请改用[.]
。因此,请尝试([a-z][a-z0-9_]*)[.]_NNP
。
甚至使用积极前瞻:[a-z][a-z0-9_]*(?=[.]_NNP)
。使用m.group()
访问捕获的数据。
答案 2 :(得分:1)
这个应该有效
import java.util.ArrayList;
public class Test {
public static final String NOUN_REGEX = "[a-zA-Z]*_NN\\w?\\w?\\b";
public static ArrayList<String> extractNounsByRegex(String sentenceWithTags) {
ArrayList<String> nouns = new ArrayList<String>();
String[] words = sentenceWithTags.split("\\s+");
for (int i = 0; i < words.length; i++) {
if(words[i].matches(NOUN_REGEX)) {
System.out.println(" Matched ");
//remove the suffix _NN* and retain [a-zA-Z]*
nouns.add(words[i].replaceAll("_NN\\w?\\w?\\b", ""));
}
}
return nouns;
}
public static String extractOriginal(String word) {
return word.replaceAll("_NN\\w?\\w?\\b", "");
}
public static void main(String[] args) {
// String sentence = "Hi._NNP How_WRB are_VBP you?_JJ This_DT is_VBZ Mike._NN";
String sentence = "Eiffel_NNP tower_NN is_VBZ in_IN paris_NN Hi_NNP How_WRB are_VBP you_PRP This_DT is_VBZ Mike_NNP Barrack_NNP Obama_NNP is_VBZ a_DT president_NN this_VBZ";
System.out.println(extractNounsByRegex(sentence).toString());
System.out.println(sentence);
}
}