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.

Recommended Answers

All 2 Replies

I don't want to sound ignorant, or pretend that I know everything, but does this code make any sense to you all?
Because if the purpose of the code is reading a specific file then I don't think this is the right way.

What exactly are you trying to accomplish?
What is it that it doesn't work?
What do you get and what do you expect?

commented: You mean you don't know everything ;) +9

I don't want to sound ignorant, or pretend that I know everything, but does this code make any sense to you all?
Because if the purpose of the code is reading a specific file then I don't think this is the right way.

What exactly are you trying to accomplish?
What is it that it doesn't work?
What do you get and what do you expect?

Sorry, I guess I was a little vague. I'm trying to read text from given files by using a scanner object and store them into a set, which is a value within a hashmap. From there, I want to be able check to see if the value is already within the set and then create a key for this using the hashmap.

I then want to be able to return all values of the set within the hashmap and then somehow reference these within the main method. But it is not actually working.

Any help pls? Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.