skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Tuesday, October 9, 2012

Converting a Paragraph into Sentences

Posted by Raju Gupta at 1:20 PM – 0 comments
 
The given paragraph is taken as a input from a file. Using regular expresssions and pattern matching techniques, as . as delimiter for a sentence, the given paragraph is converted into sentences and stored in an output file.
// Converting a Paragraph into Sentence using Java Regular Expressions
// Takes input Paragraph from a file and places the Sentences into another file

import java.io.*;
import java.util.regex.*;

class Conversion{
    public static void main(String args[]) throws Exception 
 {
 //Input File Reader
 FileReader fr = new FileReader("input.txt");
 BufferedReader br = new BufferedReader(fr);
 String s;
 //Output gets stored in output.txt
 OutputStream out = new FileOutputStream("output.txt");
                      String token[];
 
 while((s = br.readLine()) != null)
    {
     //Delimiter considered is . and compared using pattern matching technique
      Pattern pat1 = Pattern.compile("[.]");
      token = pat1.split(s);
      for(int i=0;i<token.length;i++)
       {
         byte buf[]=token[i].getBytes();
 
                             // Writing into a file with a new line character indicating beginning of a new line after every sentence
          for(int j=0;j<buf.length;j=j+1)
         {
                                out.write(buf[j]);
                 if(j==buf.length-1)
                    out.write('n');
            }
         }
      }
       fr.close();
  }
}

Labels: Regex Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger