Mckan/D-A — pull request #1 by LoLip0op (2012-03-18 22:15:12+00:00)
Title: wordlist

import java.util.*;
import java.io.*;
// Author:	Alexander Kurkimäki
// Email:	alekur@student.chalmers.se
// Date:	2012-03-14

public class WordLists {
FileInputStream fis;
DataInputStream dis;
BufferedReader in;
TreeMap tm;
TreeSet reverseString;
FileWriter fw;
BufferedWriter bw;
String s;

public WordLists(String inputFileName) throws IOException {
fis = new FileInputStream(inputFileName);
dis = new DataInputStream(fis);
in = new BufferedReader(new InputStreamReader(dis));
tm = new TreeMap();

int x = 1,v;

while((s=getWord()) != null){

if(tm.containsKey(s)){
v = tm.get(s);
v++;
tm.put(s,v);
}
else
tm.put(s,x);
}
}

private boolean isPunctuationChar(char c) {
final String punctChars = ",.:;?!";
return punctChars.indexOf(c) != -1;
}

private String getWord() throws IOException {
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) {
char[] tmp = new char[s.length()];
int temp = s.length()-1;

for(int i = 0; i < s.length(); i++){
tmp[i]= s.charAt(temp-i);
}

String s2 = new String(tmp);
return s2;

}

private void computeWordFrequencies() {
try{
fw = new FileWriter("alfaSorted.txt");
bw = new BufferedWriter(fw);
TreeMap temp = (TreeMap)tm.clone();
while(true){
if(temp.firstEntry()==null)
break;

bw.write("" + temp.pollFirstEntry() + "\n");
}
bw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}

}

private void computeFrequencyMap() {
TreeMap tm2 = (TreeMap)tm.clone();
TreeMap> freq = new TreeMap>();
TreeSet test = new TreeSet();

try{
fw = new FileWriter("frequencySorted.txt");
bw = new BufferedWriter(fw);
String word;
for(String key : tm2.keySet()){

test.add(key);

if(freq.containsKey(tm2.get(key))){
TreeSet temp = freq.get(tm2.get(key));
temp.add(key);
}
else{
TreeSet temp = new TreeSet();
temp.add(key);
freq.put(tm2.get(key), temp);

}
//test.remove(key);

}

while(true){
if(freq.firstEntry()==null)
break;

bw.write("\n" + freq.pollFirstEntry() + "\n");
}
bw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}

}

private void computeBackwardsOrder() {
TreeSet tmp = new TreeSet();

String s;
try{
while( (s=getWord()) != null){
tmp.add(reverse(s));
}

}catch (IOException e){}

try{
// Create file
fw = new FileWriter("backwardsSorted.txt");
bw = new BufferedWriter(fw);
System.out.println("hej!");
//Write every word to file
Iterator i = tmp.iterator();
while(i.hasNext()){
bw.write(reverse(i.next()) + "\n");
}

//Close the output stream
bw.close();

}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}

public static void main(String[] args) throws IOException {
WordLists wl = new WordLists(args[0]); // args[0] contains the input file name
wl.computeWordFrequencies();
wl.computeFrequencyMap();
wl.computeBackwardsOrder();

System.out.println("Finished!");
}
}