Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将文本的所有段落读入列表?_Java_Text Processing_Text Parsing - Fatal编程技术网

Java 如何将文本的所有段落读入列表?

Java 如何将文本的所有段落读入列表?,java,text-processing,text-parsing,Java,Text Processing,Text Parsing,我试图把一篇课文分成不同的段落。我确实发现了一个又一个问题。但是,我已经找到了如何检测段落的方法。我救不了他们 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 c

我试图把一篇课文分成不同的段落。我确实发现了一个又一个问题。但是,我已经找到了如何检测段落的方法。我救不了他们

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 List findParagraph(List originalBook)
{
列表段落=新建链接列表();
列出句子=新建链接列表();
对于(int i=0;i2)
{
添加(原书.删除(i));
//如果你前面的那一行是空行,那么你已经到了段落的末尾
如果(i
这个类定义了我的段落

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;
    }

}
公共类段落
{
私人列表句子;
公开段()
{
超级();
}
公共列表{
返回句子;
}
公共无效条款(列出句子){
这个句子=句子;
}
}
我能够很好地识别段落,但是我正在清除所有的句子,并且我得到一个只包含最后一段的列表。我一直在想一个解决办法,但我没能想出一个。有人能提供一些建议吗


我已尽可能详细地解释了。如有必要,我可以添加更多详细信息。

问题在本栏中:

Paragraph paragraph = new Paragraph();
List<String> strings = sentences; // <-- !!!!!
paragraph.setSentences(strings);
paragraphs.add(paragraph);
sentences.clear();
段落=新段落();

列表字符串=句子;// 您可以将代码更改为更加高效和干净,而不是计算其索引和创建多个if语句

示例:

Scanner scan = new Scanner(new File("text.txt"));
String parag = "";

while(scan.hasNextLine())
{
    String s = scan.nextLine();
    if(s.trim().length() != 0)
        parag += s + "\n"; //new sentence
    else
    {
        System.out.println(parag); //new paragraph
        parag = "";
    }
}

System.out.println(parag); //last paraggraph

我不知道为什么我没有想到这一点。有时候你只需要一双新鲜的眼睛。谢谢