Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module src;
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- behavior HelloWorld {
- Map documents = new HashMap();
- Map AllWords = new HashMap();
- Map AllWordsStartingWithCharacter = new HashMap();
- Map readFile(int n, String wordFile){
- Map words = new HashMap();
- try {
- BufferedReader in = new BufferedReader(new FileReader(wordFile));
- String line;
- while ((line = in.readLine())!= null){
- String[] splitted = line.split(" ");
- for(int i = 0; i < splitted.length; i++){
- String word = splitted[i];
- char FirstCharacter = word.charAt(0);
- double vals[];
- vals = new double[] {1.0d, 0.0d};
- if(words.containsKey(word)){
- double v[] = (double[])words.get(word);
- vals[0] = v[0]+1.0d;
- }
- vals[1] = 1 + java.lang.Math.log10(vals[0]);
- Map map = ((Map)AllWordsStartingWithCharacter.get(FirstCharacter));
- String key = "Doc " + n + " | " + word;
- map.put(key, vals);
- words.put(word, vals);
- }
- }
- in.close();
- } catch (IOException ioe){
- standardOutput<-println("[error] Can't open the file "+wordFile+" for reading.");
- }
- return words;
- }
- void act( String[] argv ){
- char[] L = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
- for(int i = 0; i < 26; i++){
- Map map = new HashMap();
- AllWordsStartingWithCharacter.put(L[i], map);
- }
- for(int i = 0; i < 1052; i++){
- standardOutput<-println("Next file: " + i);
- documents.put(i, readFile(i, "/Users/ethanbond/Classes/ProgLang/Homework3/src/wordfile/"+i+".txt"));
- Map doc = new HashMap();
- doc = (Map)documents.get(i);
- Iterator it = ((Map)documents.get(i)).entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = (Map.Entry)it.next();
- Object key = entry.getKey();
- }
- standardOutput<-println(i);
- }
- for(int i = 0; i < 26; i++){
- Writer writer = null;
- try {
- writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("words-" + L[i] + ".txt"), "utf-8"));
- writer.write("Document lfkadjsf for letter " + L[i]);
- Iterator it = ((Map)AllWordsStartingWithCharacter.get(L[i])).entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = (Map.Entry)it.next();
- Object key = entry.getKey();
- double value[] = new double[2];
- value = (double[])entry.getValue();
- writer.write(key + " | " + value[0] + " | " + value[1] + "\n");
- }
- } catch (IOException ex) {
- standardOutput<-println("exception here");
- standardOutput<-println(ex);
- // report
- } finally {
- try {writer.close();} catch (Exception ex) {}
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement