All pastes #972479 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-05 13:31 UTC
rendered paste body
// Author:
// Email:	
// Date:
		
import java.io.*;
import java.util.TreeSet;
import java.util.TreeMap;
import java.util.HashMap;

public class WordList {
private TreeSet<String> TScontainer = new TreeSet<String>();
//private HashMap<String, Integer>m = new HashMap<String, Integer>();

	public WordList(String inputFileName) {
		StringBuffer contents = new StringBuffer();
		try {
		   BufferedReader input =  new BufferedReader(new FileReader(inputFileName));
		   try {
		      String line = null;
		      while (( line = input.readLine()) != null){
		          contents.append(line);
		          contents.append(System.getProperty("line.separator"));
		      }
		   }
		   finally {
			   input.close();
		   }
		}
		catch (IOException e){
		  e.printStackTrace();
		}
		    
		    String[] list = contents.toString().split(" ");
		    for(String x : list){
		    	String fixedstring = x.trim();
		    	System.out.println(fixedstring);
		    	int i;
		    	for(i=0;i<fixedstring.length();i++){
		    		if(isPunctuationChar(x.charAt(i))){
		    			fixedstring = removeCharAt(fixedstring, i); //substring with punctuations removed
		    			System.out.println(fixedstring);
		    		}
		    	}
		    	TScontainer.add(fixedstring.trim());
		    }
		}
		
		private boolean isPunctuationChar(char c) {
		    final String punctChars = ",.:;?!";
		    return punctChars.indexOf(c) != -1;
		}
		
		/**
		 * method for cutting out characters in a string
		 * @param s the string to be cut
		 * @param pos the position that will be cut out
		 * @return substring with character at pos removed
		 */
		private static String removeCharAt(String s, int pos){
			if(pos == s.length()-1){
				return s.substring(0,pos);
			}
			return s.substring(0,pos)+s.substring(pos+1); 
		}
		
		private String getWord() throws IOException {
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			int state = 0;
			int i;
			String word = "";
			while ( true ) {
				i = in.read();
				char c = (char)i;
				switch ( state ) {
				case 0:
					if ( i == -1 )
						return null;
					if ( Character.isLetter( c ) ) {
						word += Character.toLowerCase( c );
						state = 1;
					}
					break;
				case 1:
					if ( i == -1 || isPunctuationChar( c ) || Character.isWhitespace( c ) )
						return word;
					else if ( Character.isLetter( c ) ) 
						word += Character.toLowerCase( c );
					else {
						word = "";
						state = 0;
					}
				}
			}
		}
		
		private String reverse(String s) {
			return s;
		    // define!
		}
		
		private void computeWordFrequencies() {
			
		}
		
		//HÄR BLIR DET CP
		private void computeFrequencyMap() {
			TreeMap<String, Integer> tmap = new TreeMap<String, Integer>();
			for(String x : TScontainer){
				int amount = 1;
				if(tmap.containsKey(x)){
					amount = tmap.get(x);
					tmap.put(x, amount++);
					
				}
				else{
					tmap.put(x, amount);
				}
				
			}
				
			try{
				FileWriter fstream = new FileWriter("alfasorted.txt");
				BufferedWriter out = new BufferedWriter(fstream);
				for(String x : TScontainer)
					out.write(x.trim()+"  "+tmap.get(x)+"\n");
					out.close();
			}catch(Exception e){}
		}
		

		private void computeBackwardsOrder() {
		    // define!
		}

		public static void main(String[] args) throws IOException {
			WordList wl = new WordList("provtext.txt");  // arg[0] contains the input file name
			wl.computeWordFrequencies();
			wl.computeFrequencyMap();
			wl.computeBackwardsOrder();
			System.out.println("Finished!");
		}
}