Roughosing Roughosing - 7 days ago
59 0

Code which reads in lines from file (tweets), then stores the misspelled words and their frequency in a HashMap

Java

AssignOne.java

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

public class AssignOne {
	
	public AssignOne() throws IOException {			//read in file from path, test functions
		index("C:\\Users\\Gregs\\InfoRetrieval\\src\\tweets");
	}
	
	public static HashMap<String, Integer> misSpell;

        public static void index(String filename)  throws IOException {
		misSpell = new HashMap<String, Integer>();
		Scanner scanner = new Scanner(new FileInputStream(filename));
		try{
			while(scanner.hasNextLine()){
				String line = scanner.nextLine();	
				String[] lineArr = line.split(" ");
				for(int i=3; i<lineArr.length; i++){
					int count=1;
					lineArr[i] = lineArr[i].replaceAll("[^a-zA-Z]", ""); //only interested in words, remove any non alphabetic character
					if(!AssignTwo.isSpelledCorrectly(lineArr[i].toLowerCase())){
						String word = lineArr[i].toLowerCase();
						if(misSpell.containsKey(word)){				
							count = 1 + misSpell.get(word);				
						}
						misSpell.put(word, count);
					}
				}
			}
		} 
		finally{
			scanner.close();
		}
	}

   public static void main(String[] args) throws IOException{	

		AssignTwo spell = new AssignTwo();			//runs AssignTwo, creating spelled words list
		AssignOne start = new AssignOne();			//runs AssignOne, creating HashMap of misspelled words
		
		HashMap<String, Integer> results = AssignTwo.topTen(misSpell);
		
		for(Map.Entry<String, Integer> entry : results.entrySet()){
			System.out.println("Key: "+entry.getKey()+"  \t Value: "+entry.getValue());
		}
		System.out.println();
		for(Map.Entry<String, Integer> entry : results.entrySet()){
			String word = entry.getKey();
			if(!AssignTwo.isSpelledCorrectly(word)){
				List<String> suggested = AssignTwo.suggestions(word);
				System.out.println("Possible suggestions for '"+ word +"' are seen below:");
				for(int i=0; i<suggested.size(); i++){
					System.out.println(suggested.get(i));
				}
				System.out.println();
			}
		}
		
	}
}
Comments