Answer:
The code is given below,
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SentenceUtilsTest {
public static void main(String[] args) {
List<SentenceUtils> sList = new ArrayList<SentenceUtils>();
try{
File file = new File(args[0]);
Scanner scanner = new Scanner(new FileInputStream(file));
while(scanner.hasNextLine()){
String sent = scanner.nextLine();
if(sent.trim().length() > 0){
SentenceUtils sUtils = new SentenceUtils(sent);
sList.add(sUtils);
}
}
System.out.println("File that was read:" + args[0]);
System.out.println("File contains " + sList.size() + " sentences.\n");
System.out.println("Sentences reports:\n");
for(int line = 0; line < sList.size(); line++){
System.out.println("Sentences " + line +";");
System.out.println(sList.get(line).getSentence());
SentenceUtils lineText = sList.get(line);
String[] tokens = lineText.getTokens();
for(int id = 0; id < tokens.length; id++){
System.out.println(id+":"+tokens[id]);
}
String[] shingles = lineText.getShingles();
for(int id = 0; id < shingles.length; id++){
if(shingles[id] != null){
System.out.print(shingles[id]+" ");
}
}
System.out.println("\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
package sentenceutils;
import java.util.ArrayList;
import java.util.List;
public class SentenceUtils {
private String sentence;
private String[] tokens;
private String[] shingles;
public SentenceUtils(String s){
this.sentence = s;
generateTokens();
generateShingles();
}
private void generateShingles() {
List<String> shinglesList = new ArrayList<String>();
for (int index=0; index < sentence.length()-1; index++) {
shinglesList.add( sentence.charAt(index) +""+sentence.charAt(index+1) );
}
shingles = new String[sentence.length()];
shinglesList.toArray(shingles);
}
private void generateTokens() {
tokens = sentence.split(" ");
}
public String getSentence() {
return sentence;
}
public String[] getTokens() {
return tokens;
}
public String[] getShingles() {
return shingles;
}
}