我正在尝试用Java创建一个摘要生成器。我正在使用Stanford Log-linear Part-Of-Speech Tagger标记单词,然后,对于某些标签,我正在对句子进行评分,最后在摘要中,我正在打印具有高分值的句子。 这是代码:
MaxentTagger tagger = new MaxentTagger("taggers/bidirectional-distsim-wsj-0-18.tagger");
BufferedReader reader = new BufferedReader( new FileReader ("C:\\Summarizer\\src\\summarizer\\testing\\testingtext.txt"));
String line = null;
int score = 0;
StringBuilder stringBuilder = new StringBuilder();
File tempFile = new File("C:\\Summarizer\\src\\summarizer\\testing\\tempFile.txt");
Writer writerForTempFile = new BufferedWriter(new FileWriter(tempFile));
String ls = System.getProperty("line.separator");
while( ( line = reader.readLine() ) != null )
{
stringBuilder.append( line );
stringBuilder.append( ls );
String tagged = tagger.tagString(line);
Pattern pattern = Pattern.compile("[.?!]"); //Find new line
Matcher matcher = pattern.matcher(tagged);
while(matcher.find())
{
Pattern tagFinder = Pattern.compile("/JJ"); // find adjective tag
Matcher tagMatcher = tagFinder.matcher(matcher.group());
while(tagMatcher.find())
{
score++; // increase score of sentence for every occurence of adjective tag
}
if(score > 1)
writerForTempFile.write(stringBuilder.toString());
score = 0;
stringBuilder.setLength(0);
}
}
reader.close();
writerForTempFile.close();
以上代码无效。虽然,如果我削减我的工作并为每一行(而不是句子)生成分数,它就有效。但摘要不是那样产生的,不是吗? 这是代码:(所有声明与上面相同)
while( ( line = reader.readLine() ) != null )
{
stringBuilder.append( line );
stringBuilder.append( ls );
String tagged = tagger.tagString(line);
Pattern tagFinder = Pattern.compile("/JJ"); // find adjective tag
Matcher tagMatcher = tagFinder.matcher(tagged);
while(tagMatcher.find())
{
score++; //increase score of line for every occurence of adjective tag
}
if(score > 1)
writerForTempFile.write(stringBuilder.toString());
score = 0;
stringBuilder.setLength(0);
}
编辑1:
有关MaxentTagger的功能的信息。显示其功能的示例代码:
import java.io.IOException;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;
public class TagText {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
// Initialize the tagger
MaxentTagger tagger = new MaxentTagger(
"taggers/bidirectional-distsim-wsj-0-18.tagger");
// The sample string
String sample = "This is a sample text";
// The tagged string
String tagged = tagger.tagString(sample);
// Output the result
System.out.println(tagged);
}
}
输出:
This/DT is/VBZ a/DT sample/NN sentence/NN
编辑2:
使用BreakIterator修改代码以查找句子中断。然而问题仍然存在。
while( ( line = reader.readLine() ) != null )
{
stringBuilder.append( line );
stringBuilder.append( ls );
String tagged = tagger.tagString(line);
BreakIterator bi = BreakIterator.getSentenceInstance();
bi.setText(tagged);
int end, start = bi.first();
while ((end = bi.next()) != BreakIterator.DONE)
{
String sentence = tagged.substring(start, end);
Pattern tagFinder = Pattern.compile("/JJ");
Matcher tagMatcher = tagFinder.matcher(sentence);
while(tagMatcher.find())
{
score++;
}
scoreTracker.add(score);
if(score > 1)
writerForTempFile.write(stringBuilder.toString());
score = 0;
stringBuilder.setLength(0);
start = end;
}
答案 0 :(得分:3)
找到句子中断可能比查找[。?!]更复杂,考虑使用BreakIterator。getSentenceInstance()
它的性能实际上与LingPipe(更复杂)的实现非常相似,并且优于OpenNLP(至少来自我自己的测试)。
BreakIterator bi = BreakIterator.getSentenceInstance();
bi.setText(text);
int end, start = bi.first();
while ((end = bi.next()) != BreakIterator.DONE) {
String sentence = text.substring(start, end);
start = end;
}
我认为这就是你要找的东西:
Pattern tagFinder = Pattern.compile("/JJ");
BufferedReader reader = getMyReader();
String line = null;
while ((line = reader.readLine()) != null) {
BreakIterator bi = BreakIterator.getSentenceInstance();
bi.setText(line);
int end, start = bi.first();
while ((end = bi.next()) != BreakIterator.DONE) {
String sentence = line.substring(start, end);
String tagged = tagger.tagString(sentence);
int score = 0;
Matcher tag = tagFinder.matcher(tagged);
while (tag.find())
score++;
if (score > 1)
writerForTempFile.println(sentence);
start = end;
}
}
答案 1 :(得分:2)
如果不理解这一切,我的猜测就是你的代码应该更像这样:
int lastMatch = 0;// Added
Pattern pattern = Pattern.compile("[.?!]"); //Find new line
Matcher matcher = pattern.matcher(tagged);
while(matcher.find())
{
Pattern tagFinder = Pattern.compile("/JJ"); // find adjective tag
// HERE START OF MY CHANGE
String sentence = tagged.substring(lastMatch, matcher.end());
lastMatch = matcher.end();
Matcher tagMatcher = tagFinder.matcher(sentence);
// HERE END OF MY CHANGE
while(tagMatcher.find())
{
score++; // increase score of sentence for every occurence of adjective tag
}
if(score > 1)
writerForTempFile.write(sentence);
score = 0;
}