Java implementation text is sliced according to natural paragraphs

Source: Internet
Author: User

 PackageCn.edu.shu.web.test;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.IOException;ImportJava.util.ArrayList;ImportJava.util.Iterator;/** * * <p> * ClassName getparagraph * </p> * <p> * Description use Java to complete the segmentation of a natural paragraph of text, where five text formats are given as example, in either format, the program can be precisely divided.         * </p> * * @author tkpad [email protected] * <p> * Date February 11, 2015 1:33:03 * </p> * @version V1.0.0 * */ Public  class getparagraph {     Public Static void Main(string[] args)throwsIOException {arraylist<string> res =NewArraylist<string> ();//Segmentation resultsStringBuilder SB =NewStringBuilder ();//splicing Read contentString temp =NULL;//Temp variable, store SB to remove the contents of the space        //BufferedReader reader = new BufferedReader (new FileReader (New File ("C:\\users\\tkpad\\desktop\\a.txt" ));        //BufferedReader reader= new BufferedReader (New FileReader (New File ("C:\\users\\tkpad\\desktop\\b.txt" ));        //BufferedReader reader= new BufferedReader (New FileReader (New File ("C:\\users\\tkpad\\desktop\\c.txt" ));        //BufferedReader reader = new BufferedReader (new FileReader (New File ("C:\\users\\tkpad\\desktop\\d.txt" ));BufferedReader reader =NewBufferedReader (NewFileReader (NewFile ("C:\\users\\tkpad\\desktop\\e.txt")));intCH =0; while(ch = reader.read ())! =-1) {temp = sb.tostring (). Trim (). ReplaceAll ("\\s*","");//Remove the space before and after removing the middle space            if((Char) ch = =' \ r ') {//Determine if it is a blank line                if(!"". Equals (temp)) {//Description to the end of the paragraph, add it to the list, and empty SBRes.add (temp); } sb.delete (0, Sb.length ()); }Else{//Description not at the end of paragraph, the result is stagedSb.append ((Char) ch); }        }if(Reader.read () = =-1) {System.out.println ("Haha, you read to the end!" "); }//Last paragraph if not null, the last paragraph will be added, otherwise not processed        if(!"". Equals (temp)) {Res.add (temp); } iterator<string> Iterator = Res.iterator (); while(Iterator.hasnext ())            {String next = Iterator.next (); System.out.println ("Paragraph start:");        System.out.println (next); } System.out.println ("The number of paragraphs is:"+ res.size ()); }}

The test text has been packaged and uploaded, please download it.
Address:
http://download.csdn.net/detail/shijiebei2009/8440133

Java implementation text is sliced according to natural paragraphs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.