package corpus;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import java.util.Collection;
import java.util.TreeSet;
public class FileReader {
private static final String[] FILES ={"a_christmas_carol_capter1.txt",
"a_christmas_carol_chapter4.txt",
"emma_chapter1.txt",
"emma_chapter2.txt",
"pride_and_prejudice_chapter3.txt",};
private Map<String, Set<String>> corpusCV;
public FileReader(String textFile) throws FileNotFoundException
{
Set<String> texts = new HashSet<String>();
corpusCV = new HashMap<String, Set<String>>();
try {
Scanner s = new Scanner(new File(textFile), "utf8");
s.useDelimiter(".");
while(s.hasNextLine()){
String text = s.nextLine();
if(text.length() > 0 && !texts.contains(text)) {
texts.add(text);
}
}
s.close();
findText(texts);
}
catch (FileNotFoundException fnfe) {
System.out.println("File Not Found");
}
}
private void findText(Set<String> texts)
{
for(String text : texts)
{
String word = text;
Set<String> foundText = corpusCV.get(word);
if(!corpusCV.containsKey(word)) {
foundText = new HashSet<String>();
corpusCV.put(word, foundText);
}
foundText.add(text);
}
}
public Set<String> getText()
{
return new TreeSet<String>(corpusCV.keySet());
}
public Set<String> showText(String word)
{
return corpusCV.get(word);
}
public static void main (String[] args)
{
String textFile ;
if(FILES.length > 0) {
textFile = args[0];
}
try {
FileReader fr = new FileReader(textFile);
printWords(fr.corpusCV.keySet());
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
private static void printWords(Collection<String> words)
{
for(String p : words) {
System.out.println(p);
}
System.out.println();
}
}
Basically, i've tried to write code in order to lookup given specified files, but I cant't seem to get any output as the files are stored in a string array. This conflicts when trying to reference this variable within the main method as it also uses an array of strings.
Would someone please be able to provide me with some help. Thank you.