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