Advertisement
adamferg123

Untitled

Feb 10th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package spellchecker;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16. import java.util.Map;
  17. import java.util.Set;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20.  
  21. class SpellSuggest {
  22.  
  23. public static final HashMap<String, Integer> TagalogDatabaseWords = new HashMap<String, Integer>();
  24.  
  25. /**
  26. * This initializes the content of the Tagalog Words Database.
  27. *
  28. * @param file the dictionary text file
  29. * @throws IOException
  30. */
  31. public SpellSuggest(String file) throws IOException {
  32. try {
  33. BufferedReader in = new BufferedReader(new FileReader(file));
  34. Pattern p = Pattern.compile("\\w+");
  35. for (String temp = ""; temp != null; temp = in.readLine()) {
  36. Matcher m = p.matcher(temp.toLowerCase());
  37. while (m.find()) {
  38.  
  39. TagalogDatabaseWords.put((temp = m.group()), TagalogDatabaseWords.containsKey(temp) ? TagalogDatabaseWords.get(temp) + 1 : 1);
  40. }
  41. }
  42. in.close();
  43.  
  44. } catch (FileNotFoundException e) {
  45. e.printStackTrace();
  46.  
  47. }
  48. }
  49.  
  50. private final ArrayList<String> edits(String word) {
  51. ArrayList<String> result = new ArrayList<String>();
  52. for (int i = 0; i < word.length(); ++i) {
  53. result.add(word.substring(0, i) + word.substring(i + 1));
  54. }
  55. for (int i = 0; i < word.length() - 1; ++i) {
  56. result.add(word.substring(0, i) + word.substring(i + 1, i + 2) + word.substring(i, i + 1) + word.substring(i + 2));
  57. }
  58. for (int i = 0; i < word.length(); ++i) {
  59. for (char c = 'a'; c <= 'z'; ++c) {
  60. result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i + 1));
  61.  
  62. }
  63. }
  64. for (int i = 0; i < word.length(); ++i) {
  65. for (char c = 'a'; c <= 'z'; ++c) {
  66. result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
  67. }
  68. }
  69.  
  70. return result;
  71. }
  72.  
  73. public final String correct(String word) {
  74.  
  75. String test = "lmao";
  76. String[] words = new String[3];
  77. int[] val = new int[3];
  78.  
  79. if (TagalogDatabaseWords.containsKey(word)) {
  80. words[0] = word;
  81. return test;
  82. }
  83.  
  84. ArrayList<String> list_edits = edits(word);
  85. HashMap<Integer, String> candidates = new HashMap<Integer, String>();
  86.  
  87. for (String s : list_edits) {
  88. if (TagalogDatabaseWords.containsKey(s)) {
  89. if (words[0] == null) {
  90. words[0] = s;
  91. val[0] = TagalogDatabaseWords.get(s);
  92. } else if (words[1] == null) {
  93. if (TagalogDatabaseWords.get(s) >= val[0]) {
  94. words[1] = words[0];
  95. val[1] = val[0];
  96. words[0] = s;
  97. val[0] = TagalogDatabaseWords.get(s);
  98. } else {
  99. words[1] = s;
  100. val[1] = TagalogDatabaseWords.get(s);
  101. }
  102. } else if (words[2] == null) {
  103. if (TagalogDatabaseWords.get(s) >= val[0]) {
  104. words[2] = words[1];
  105. val[2] = val[1];
  106.  
  107. words[1] = words[0];
  108. val[1] = val[0];
  109.  
  110. words[0] = s;
  111. val[0] = TagalogDatabaseWords.get(s);
  112. } else if (TagalogDatabaseWords.get(s) >= val[1]) {
  113. words[2] = words[1];
  114. val[2] = val[1];
  115.  
  116. words[1] = s;
  117. val[1] = TagalogDatabaseWords.get(s);
  118. } else {
  119. words[2] = s;
  120. val[2] = TagalogDatabaseWords.get(s);
  121. }
  122. } else if (TagalogDatabaseWords.get(s) >= val[0]) {
  123. words[2] = words[1];
  124. val[2] = val[1];
  125.  
  126. words[1] = words[0];
  127. val[1] = val[0];
  128.  
  129. words[0] = s;
  130. val[0] = TagalogDatabaseWords.get(s);
  131. } else if (TagalogDatabaseWords.get(s) >= val[1]) {
  132. words[2] = words[1];
  133. val[2] = val[1];
  134.  
  135. words[1] = s;
  136. val[1] = TagalogDatabaseWords.get(s);
  137. } else if (TagalogDatabaseWords.get(s) >= val[2]) {
  138. words[2] = s;
  139. val[2] = TagalogDatabaseWords.get(s);
  140. }
  141.  
  142. }
  143. }
  144.  
  145. int ctr = 0;
  146. for (int i = 0; i < 3; i++) {
  147. System.out.println(words[i] + " : " + val[i]);
  148. }
  149.  
  150.  
  151. if (words[2] != null) {
  152.  
  153. return test;
  154.  
  155. }
  156.  
  157. for (String s : list_edits) {
  158. for (String w : edits(s)) {
  159. if (TagalogDatabaseWords.containsKey(w)) {
  160. //candidates.put(TagalogDatabaseWords.get(w), w);
  161. if(words[0] == null){
  162. words[0] = w;
  163. val[0] = TagalogDatabaseWords.get(w);
  164. }else if(words[1] == null){
  165.  
  166. }else if(words[2] == null){
  167.  
  168. }
  169.  
  170. }
  171. }
  172. }
  173. //return candidates.size() > 0 ? candidates.get(Collections.max(candidates.keySet())) : "Sorry but no possible soluction found";
  174. test = "kek";
  175. return test;
  176. }
  177.  
  178. public static void main(String[] args) throws IOException {
  179. if (args.length > 0) {
  180. System.out.println((new SpellSuggest("src/tl.txt")).correct(args[0]));
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement