1
我正在使用openNLP標記POS(詞性)。在Java中使用POSTagger在不同的文件中保存不同的POS(詞類)?
InputStream inputStream = new
FileInputStream("C:/en-pos-maxent.bin");
POSModel model = new POSModel(inputStream);
POSTaggerME tagger = new POSTaggerME(model);
String sentence = "This is not a song for the broken-hearted" +
" No silent prayer for the faith-departed " +
" I am not gonna be just a face in the crowd " +
" You are gonna hear my voice " +
" When I shout it out loud";
String simple = "[.?!-]";
String[] splitString = (sentence.split(simple));
SimpleTokenizer simpleTokenizer = SimpleTokenizer.INSTANCE;
//String tokens[] = simpleTokenizer.tokenize();
for(int i = 0;i<splitString.length;i++)
{
String tokens[] = simpleTokenizer.tokenize(splitString[i]);
String[] tags = tagger.tag(tokens);
//POSSample sample = new POSSample(tokens, tags);
/*for(String token : tokens) {
System.out.println(token);
}*/
for(int j= 0;j < tags.length;j++)
{
if(tags[j].equals("DT"))
{
//System.out.println(tokens[j]);
File file = new File("DT.txt");
try {
PrintWriter output = new PrintWriter(file);
output.println(tokens[j]);
output.close();
} catch (Exception e) {
// TODO: handle exception
}
當我在loop.It使用的println告訴我需要value.But當我將它保存在一個文件名DT.txt。它只是在文本文件中保存一個值。 Text file output Printed Output in console