【发布时间】:2014-06-18 11:09:05
【问题描述】:
我正在尝试将文本分成不同的段落。我确实找到了this 问题和this 问题。但是,我已经想出了如何检测段落。我无法保存它们。
One morning, when Gregor Samsa woke from troubled dreams, he found
himself transformed in his bed into a horrible vermin. He lay on
his armour-like back, and if he lifted his head a little he could
see his brown belly, slightly domed and divided by arches into stiff
sections. The bedding was hardly able to cover it and seemed ready
to slide off any moment. His many legs, pitifully thin compared
with the size of the rest of him, waved about helplessly as he
looked.
"What's happened to me?" he thought. It wasn't a dream. His room,
a proper human room although a little too small, lay peacefully
between its four familiar walls. A collection of textile samples
以上文字将被计为两段。下面是我用于段落检测的函数。
public List<Paragraph> findParagraph(List<String> originalBook)
{
List<Paragraph> paragraphs = new LinkedList<Paragraph>();
List<String> sentences = new LinkedList<String>();
for(int i=0;i<originalBook.size();i++)
{
//if it isn't a blank line
//don't count I,II symbols
if(!originalBook.get(i).equalsIgnoreCase("") & originalBook.get(i).length()>2)
{
sentences.add(originalBook.remove(i));
//if the line ahead of where you are is a blank line you've reach the end of the paragraph
if(i < originalBook.size()-1)
{
if(originalBook.get(i+1).equalsIgnoreCase("") )
{
Paragraph paragraph = new Paragraph();
List<String> strings = sentences;
paragraph.setSentences(strings);
paragraphs.add(paragraph);
sentences.clear();
}
}
}
}
return paragraphs;
}
这是定义我的段落的类
public class Paragraph
{
private List<String> sentences;
public Paragraph()
{
super();
}
public List<String> getSentences() {
return sentences;
}
public void setSentences(List<String> sentences) {
this.sentences = sentences;
}
}
我能够很好地检测到段落,但我正在清除所有句子并且得到一个仅包含最后一段的列表。我一直在想一个解决方案,但我一直无法想出一个解决方案。谁能给点建议?
我的解释尽可能详尽。如果需要,我可以添加更多详细信息。
【问题讨论】:
标签: java text-processing text-parsing