Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package WinterIsComing;
  2.  
  3. import TDAMapeo.*;
  4.  
  5. import java.io.*;
  6.  
  7. import TDAPriorityQueue.*;
  8.  
  9. public class jhjh {
  10. //Atributos
  11. private Map<String,Integer> mapeo;
  12. private String archivo;
  13. private int cont;
  14.  
  15. //Constructor
  16. public jhjh(String archivo){
  17. this.archivo=archivo;
  18. cont=0;
  19. mapeo= new OpenHashMap<String,Integer>();
  20. }
  21.  
  22. //Metodos
  23. public float porcentaje(String palabra){
  24. try{
  25. return (mapeo.get(palabra)*100)/cont;
  26. }
  27. catch(TDAMapeo.InvalidKeyException e){
  28. return -10;
  29. }
  30. }
  31.  
  32. public void procesarArchivo(){
  33. try{
  34. BufferedReader in = new BufferedReader(new FileReader(archivo));
  35. String str;
  36. while ((str = in.readLine())!= null){
  37. str.toLowerCase();
  38. int i=0;
  39. String palabra="";
  40. while(i<str.length()){
  41. char aux=str.charAt(i);
  42. if(esLetra(aux)){
  43. palabra+=str.charAt(i);
  44. //i++;
  45. }
  46. else{
  47. if(palabra.length()>1){
  48. System.out.println(palabra);
  49. if(estaPalabra(palabra))
  50. {int ct=mapeo.get(palabra)+1;
  51. mapeo.put(palabra, ct); }
  52. else
  53. mapeo.put(palabra, 1);
  54. cont++;
  55. palabra="";
  56. }
  57. }
  58. i++;
  59. }
  60. }
  61. in.close();
  62. }
  63. catch(TDAMapeo.InvalidKeyException e){
  64. System.out.println(e.getMessage());
  65. }
  66. catch (IOException err) {
  67. System.out.println("Error al leer el archivo");
  68. }
  69. }
  70.  
  71. private boolean estaPalabra(String p)
  72. {
  73. for(TDAMapeo.Entry<String,Integer> e: mapeo.entries())
  74. if(e.getKey().equals(p))
  75. return true;
  76. return false;
  77. }
  78. private boolean esSeparador(char c){
  79. boolean salida= false;
  80. if (c=='.' || c==',' || c==';' || c==' ' || c=='?' || c=='!' || (c>= '0' && c<='9') || c=='(' || c==')');
  81. salida= true;
  82. return salida;
  83. }
  84.  
  85. private boolean esLetra(char c){
  86. return c>='a' && c<='z';
  87. }
  88.  
  89. public TDAMapeo.Entry<String,Integer>[] palabraMasUsada()
  90. {
  91. HeapPriorityQueue<String,Integer> cp=new HeapPriorityQueue <String ,Integer>();
  92. try{
  93. for(TDAMapeo.Entry<String,Integer> e: mapeo.entries())
  94. cp.insert(e.getKey(), e.getValue());
  95. TDAMapeo.Entry<String,Integer> [] arreg= (TDAMapeo.Entry<String,Integer> []) new Object [5];
  96. for(int i=0; i<5;i++)
  97. arreg[i]= (TDAMapeo.Entry<String,Integer>) cp.removeMin();
  98.  
  99. return arreg;
  100. }
  101. catch(EmptyPriorityQueueException e){System.out.println(e.getMessage());}
  102. catch(TDAPriorityQueue.InvalidKeyException e){System.out.println(e.getMessage());}
  103. return null;
  104. }
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement