Guest User

Untitled

a guest
Apr 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. package org.eda.practica02.ejercicio03;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. import org.eda.estructurasdatos.AVLTree;
  9. import org.eda.estructurasdatos.BinarySearchTree;
  10.  
  11. public class CorrectorOrtografico {
  12.  
  13. private AVLTree<String>tree;
  14.  
  15. public CorrectorOrtografico(){
  16. tree = new AVLTree<String>();
  17. }
  18.  
  19. public int computeLevenshteinDistance(String s, String t) {
  20. if (s == null || t == null) {
  21. throw new IllegalArgumentException("Strings must not be null");
  22. }
  23.  
  24. /*
  25. * The difference between this impl. and the previous is that, rather
  26. * than creating and retaining a matrix of size s.length()+1 by
  27. * t.length()+1, we maintain two single-dimensional arrays of length
  28. * s.length()+1. The first, d, is the 'current working' distance array
  29. * that maintains the newest distance cost counts as we iterate through
  30. * the characters of String s. Each time we increment the index of
  31. * String t we are comparing, d is copied to p, the second int[]. Doing
  32. * so allows us to retain the previous cost counts as required by the
  33. * algorithm (taking the minimum of the cost count to the left, up one,
  34. * and diagonally up and to the left of the current cost count being
  35. * calculated). (Note that the arrays aren't really copied anymore, just
  36. * switched...this is clearly much better than cloning an array or doing
  37. * a System.arraycopy() each time through the outer loop.)
  38. *
  39. * Effectively, the difference between the two implementations is this
  40. * one does not cause an out of memory condition when calculating the LD
  41. * over two very large strings.
  42. */
  43.  
  44. int n = s.length(); // length of s
  45. int m = t.length(); // length of t
  46.  
  47. if (n == 0) {
  48. return m;
  49. } else if (m == 0) {
  50. return n;
  51. }
  52.  
  53. int p[] = new int[n + 1]; // 'previous' cost array, horizontally
  54. int d[] = new int[n + 1]; // cost array, horizontally
  55. int _d[]; // placeholder to assist in swapping p and d
  56.  
  57. // indexes into strings s and t
  58. int i; // iterates through s
  59. int j; // iterates through t
  60.  
  61. char t_j; // jth character of t
  62.  
  63. int cost; // cost
  64.  
  65. for (i = 0; i <= n; i++) {
  66. p[i] = i;
  67. }
  68.  
  69. for (j = 1; j <= m; j++) {
  70. t_j = t.charAt(j - 1);
  71. d[0] = j;
  72.  
  73. for (i = 1; i <= n; i++) {
  74. cost = s.charAt(i - 1) == t_j ? 0 : 1;
  75. // minimum of cell to the left+1, to the top+1, diagonally left
  76. // and up +cost
  77. d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1]
  78. + cost);
  79. }
  80.  
  81. // copy current distance counts to 'previous row' distance counts
  82. _d = p;
  83. p = d;
  84. d = _d;
  85. }
  86.  
  87. // our last action in the above loop was to switch d and p, so p now
  88. // actually has the most recent cost counts
  89. return p[n];
  90. }
  91.  
  92. public void cargarDiccionario(String archivoEntrada) {
  93. // TODO Auto-generated method stub
  94. try{
  95. File f = new File(archivoEntrada);
  96. FileReader fr = new FileReader(f);
  97. BufferedReader br = new BufferedReader(fr);
  98. String entrada = br.readLine();
  99.  
  100. while(entrada!=null){
  101. tree.add(entrada);
  102. entrada = br.readLine();
  103. }
  104.  
  105. br.close();
  106. }catch(Exception e){
  107. e.printStackTrace();
  108. }
  109. }
  110.  
  111. public ArrayList<String> listaSugerencias(int num, String word) {
  112. ArrayList<String> lista = new ArrayList<String>();
  113. String palabraNodo,palabra = "";
  114. PalabraPrioridad pP, top;
  115. int distancia;
  116. PriorityQueue<PalabraPrioridad> pQ = new PriorityQueue<PalabraPrioridad>(num);
  117. for (Iterator<String> iterator = tree.iterator(); iterator.hasNext();) {
  118. palabraNodo = iterator.next();
  119. distancia = this.computeLevenshteinDistance(word,palabraNodo);
  120. pP = new PalabraPrioridad(palabraNodo, distancia);
  121. if (pQ.size()==num){
  122. top = pQ.peek();
  123. if(pP.getDistancia() < top.getDistancia()){
  124. pQ.poll();
  125. pQ.add(pP);
  126. }
  127. }
  128. else
  129. pQ.add(pP);
  130. }
  131.  
  132. for (int i = 0; i<num;i++)
  133. lista.add(pQ.poll().getPalabra());
  134. for (Iterator<String> iterator = lista.iterator(); iterator.hasNext();)
  135. palabra = iterator.next();
  136. lista.remove(palabra);
  137. lista.add(palabra);
  138.  
  139. return lista;
  140. }
  141.  
  142. public void addPalabra(String string) {
  143. if(tree.contains(string)==false){
  144. tree.add(string);
  145. }
  146. }
  147.  
  148. }
Add Comment
Please, Sign In to add comment