引言
最近对自然语言处理特别感兴趣,之前上了一个研修课就想尝试着文本分析来着,但是由于时间关系和能力关系没实施,这学期又要提交一个大作业,要求是让计算机读入一段文本并自动的生成文本中叙述的场景,感觉很难,而且还在初步尝试中。
大体的流程应该是上述,不过目前我还在尝试用现有的NLP技术理解文本。
最近,我浏览网页发现呼声比较高的就是OpenNLP这个开源的工具包,下面我来简单描述一下我最近的尝试。
OpenNLP
OpenNLP 是一个机器学习工具包,用于处理自然语言文本。支持大多数常用的 NLP 任务,例如:标识化、句子切分、部分词性标注、名称抽取、组块、解析等。
这是它的主页,如果你对自然语言处理感兴趣,你可以通过访问OpenNLP去访问它,并下载Apache OpenNLP的工具包去亲身体验。
网页 上还能找到好多已经训练好的语言模型,也有十分详细的操作手册,但是由于我是这方面的小白所以不是很能理解。
Eclipse配置
由于最近一直在搞java所以想着实际编程去实现一下利用现有的工具包去实现分词分句等基础的NLP操作。
解压缩之后你会看到如上情景,你可以尝试着将你的lib文件安排到你的eclipse插件中,或者如下
找到你lib下的这两个.jar文件,放置到你的工程文件的lib文件中,然后在你的程序中
右键project->build path->configure build path,
然后在打开的文件中单击“add library”将这两个包添加进去,然后Apply and Close就好了,就能像用你的jre中提供好的API一样利用OpenNLP中提供的API进行自然语言处理了。
实操
添加了这两个包之后,就是编程利用现有的API,不过我只初步尝试了分词分句技术,通过阅读OpenNLP的网站提供的手册可以了解到网站上提供了诸多的训练好的语言模型,(当然有能力也可以自己训练,网站上提供的毕竟不是普适的)
try(InputStream modelIn = new FileInputStream(“en-sent.bin”)){
SentenceModel model = new SentenceModel(modelIn);
}
其中“en-sent.bin”就是网站上面提供的已经训练好的英语分句的语言模型,你可以下载并加载入程序中应用。
接下来,你可以实例化SentenceDetectorME,使用sentDetect 进行分句。
/* Instantiation */
SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
/* sentence segmentation */
String sentences[] = sentenceDetector.sentDetect(str);
测试结果如下
input :
This isn't the greatest example sentence in the world because I've seen better. Neither is this one. This one's not bad, though.
output:
-----------
This isn't the greatest example sentence in the world because I've seen better.
Neither is this one.
This one's not bad, though.
效果显著,同样语句处理分词结果(详细见下面详细代码)如下:
output:
This
is
n't
the
greatest
example
sentence
in
the
world
because
I
've
seen
better
.
Neither
is
this
one
.
This
one
's
not
bad
,
though
.
感受:
API真的好用,能用就用,能写当然也最好
代码:
public class NaturalLanguageProcess {
/**
* Main
* @param args -Commend Line.
*/
public static void main(String[] args) {
String testString = "This isn't the greatest example sentence in the world because I've seen better. Neither is this one. This one's not bad, though.";
String tokens[] = Token(testString);
String sentences[] = sentenceSegmentation(testString);
for (String string : tokens) {
System.out.println(string);
}
System.out.println("-----------");
for (String string : sentences) {
System.out.println(string);
}
}
/**
* Sentence Segmentation.
* @param str - a paragraph
* @return -the divided sentences of the input as a result
*/
public static String[] sentenceSegmentation(String str) {
try {
InputStream modelIn = new FileInputStream("src//file//en-sent.bin");
SentenceModel model = null;
try {
model = new SentenceModel(modelIn);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (modelIn != null) {
try {
modelIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* Instantiation */
SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);
/* sentence segmentation */
String sentences[] = sentenceDetector.sentDetect(str);
return sentences;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return null;
}
}
/**
* Break sentences into words and return.
* @param str - the sentences which need to be divide.
* @return - All the words as a result
*/
public static String[] Token(String str) {
try {
InputStream modelIn = new FileInputStream("src//file//en-token.bin");
TokenizerModel model = null;
try {
model = new TokenizerModel(modelIn);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (modelIn != null) {
try {
modelIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* Instantiation */
TokenizerME tokenizer = new TokenizerME(model);
/* word segmentation */
String tokens[] = tokenizer.tokenize(str);
return tokens;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}