我正在一个需要使用搭配的项目中工作。我创建了以下代码来提取它们。该代码接受一个字符串并返回该字符串中的搭配模式列表。我已经使用斯坦福 POS 进行标记。
我需要您对代码的建议,当我处理大量文本时,它似乎很慢。任何改进代码的建议都将受到高度赞赏。
/**
*
* A COLLOCATION is an expression consisting of two or more words that
* correspond to some conventional way of saying things.
*
* I used the seventh Part-of-speech-tag patterns for collocation filtering that
* were suggested by Justeson and Katz(1995).
* These patterns are:
*
* -----------------------------------------
* |Tag | Pattern Example |
* -----------------------------------------
* |AN | linear function |
* |NN | regression coefficients |
* |AAN | Gaussian random variable |
* |ANN | cumulative distribution function |
* |NAN | mean squared error |
* |NNN | class probability function |
* |NPN | degrees of freedom |
* -----------------------------------------
* Where A=adjective, P=preposition, & N=noun.
*
* Stanford POS have been used for the extraction process.
* see: http://nlp.stanford.edu/software/tagger.shtml#Download
*
* more on collocation: http://nlp.stanford.edu/fsnlp/promo/colloc.pdf
* more on POS: http://acl.ldc.upenn.edu/J/J93/J93-2004.pdf
*
*/
public class GetCollocations {
public static ArrayList<String> GetCollocations(String text) throws IOException, ClassNotFoundException{
MaxentTagger tagger = new MaxentTagger("taggers/wsj-0-18-left3words.tagger");
String[] tagged = tagger.tagString(text).split("\\s+");
ArrayList<String> collocations = new ArrayList();
for (int i = 0; i < tagged.length; i++) {
String pot = tagged[i].substring(tagged[i].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
pot = tagged[i + 1].substring(tagged[i + 1].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
collocations.add(GetWordWithoutTag(tagged[i]) + " " + GetWordWithoutTag(tagged[i + 1]));
pot = tagged[i + 2].substring(tagged[i + 2].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
collocations.add(GetWordWithoutTag(tagged[i]) + " " + GetWordWithoutTag(tagged[i + 1]) + " " + GetWordWithoutTag(tagged[i + 2]));
}
} else if (pot.equals("JJ") || pot.equals("JJR") || pot.equals("JJS")) {
pot = tagged[i + 2].substring(tagged[i + 2].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
collocations.add(GetWordWithoutTag(tagged[i]) + " " + GetWordWithoutTag(tagged[i + 1]) + " " + GetWordWithoutTag(tagged[i + 2]));
}
} else if (pot.equals("IN")) {
pot = tagged[i + 2].substring(tagged[i + 2].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
collocations.add(GetWordWithoutTag(tagged[i]) + " " + GetWordWithoutTag(tagged[i + 1]) + " " + GetWordWithoutTag(tagged[i + 2]));
}
}
} else if (pot.equals("JJ") || pot.equals("JJR") || pot.equals("JJS")) {
pot = tagged[i + 1].substring(tagged[i + 1].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
collocations.add(GetWordWithoutTag(tagged[i]) + " " + GetWordWithoutTag(tagged[i + 1]));
pot = tagged[i + 2].substring(tagged[i + 2].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
collocations.add(GetWordWithoutTag(tagged[i]) + " " + GetWordWithoutTag(tagged[i + 1]) + " " + GetWordWithoutTag(tagged[i + 2]));
}
} else if (pot.equals("JJ") || pot.equals("JJR") || pot.equals("JJS")) {
pot = tagged[i + 2].substring(tagged[i + 2].indexOf("_") + 1);
if (pot.equals("NN") || pot.equals("NNS") || pot.equals("NNP") || pot.equals("NNPS")) {
collocations.add(GetWordWithoutTag(tagged[i]) + " " + GetWordWithoutTag(tagged[i + 1]) + " " + GetWordWithoutTag(tagged[i + 2]));
}
}
}
}
return collocations;
}
public static String GetWordWithoutTag(String wordWithTag){
String wordWithoutTag = wordWithTag.substring(0,wordWithTag.indexOf("_"));
return wordWithoutTag;
}
}