Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. module src;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.lang.Math;
  6.  
  7.  
  8. behavior HelloWorld {
  9. Map documents = new HashMap();
  10. Map AllWords = new HashMap();
  11. Map AllWordsStartingWithCharacter = new HashMap();
  12.  
  13.  
  14. Map readFile(int n, String wordFile){
  15. Map words = new HashMap();
  16.  
  17. try {
  18.  
  19. BufferedReader in = new BufferedReader(new FileReader(wordFile));
  20.  
  21. String line;
  22.  
  23.  
  24. while ((line = in.readLine())!= null){
  25.  
  26.  
  27. String[] splitted = line.split(" ");
  28.  
  29. for(int i = 0; i < splitted.length; i++){
  30.  
  31. String word = splitted[i];
  32.  
  33. char FirstCharacter = word.charAt(0);
  34.  
  35.  
  36. double vals[];
  37. vals = new double[] {1.0d, 0.0d};
  38.  
  39.  
  40. if(words.containsKey(word)){
  41.  
  42. double v[] = (double[])words.get(word);
  43. vals[0] = v[0]+1.0d;
  44.  
  45.  
  46. }
  47.  
  48. vals[1] = 1 + java.lang.Math.log10(vals[0]);
  49.  
  50.  
  51.  
  52. Map map = ((Map)AllWordsStartingWithCharacter.get(FirstCharacter));
  53. String key = "Doc " + n + " | " + word;
  54. map.put(key, vals);
  55. words.put(word, vals);
  56.  
  57.  
  58. }
  59.  
  60. }
  61. in.close();
  62. } catch (IOException ioe){
  63. standardOutput<-println("[error] Can't open the file "+wordFile+" for reading.");
  64. }
  65.  
  66.  
  67. return words;
  68. }
  69.  
  70.  
  71. void act( String[] argv ){
  72. 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'};
  73.  
  74. for(int i = 0; i < 26; i++){
  75. Map map = new HashMap();
  76. AllWordsStartingWithCharacter.put(L[i], map);
  77. }
  78.  
  79. for(int i = 0; i < 1052; i++){
  80. standardOutput<-println("Next file: " + i);
  81. documents.put(i, readFile(i, "/Users/ethanbond/Classes/ProgLang/Homework3/src/wordfile/"+i+".txt"));
  82.  
  83.  
  84.  
  85. Map doc = new HashMap();
  86. doc = (Map)documents.get(i);
  87.  
  88. Iterator it = ((Map)documents.get(i)).entrySet().iterator();
  89. while (it.hasNext()) {
  90. Map.Entry entry = (Map.Entry)it.next();
  91. Object key = entry.getKey();
  92. }
  93. standardOutput<-println(i);
  94. }
  95.  
  96.  
  97. for(int i = 0; i < 26; i++){
  98. Writer writer = null;
  99.  
  100. try {
  101. writer = new BufferedWriter(new OutputStreamWriter(
  102. new FileOutputStream("words-" + L[i] + ".txt"), "utf-8"));
  103. writer.write("Document lfkadjsf for letter " + L[i]);
  104. Iterator it = ((Map)AllWordsStartingWithCharacter.get(L[i])).entrySet().iterator();
  105. while (it.hasNext()) {
  106. Map.Entry entry = (Map.Entry)it.next();
  107. Object key = entry.getKey();
  108. double value[] = new double[2];
  109. value = (double[])entry.getValue();
  110. writer.write(key + " | " + value[0] + " | " + value[1] + "\n");
  111. }
  112. } catch (IOException ex) {
  113. standardOutput<-println("exception here");
  114. standardOutput<-println(ex);
  115. // report
  116. } finally {
  117. try {writer.close();} catch (Exception ex) {}
  118. }
  119.  
  120. }
  121.  
  122.  
  123.  
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement