Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package WinterIsComing;
- import TDAMapeo.*;
- import java.io.*;
- import TDAPriorityQueue.*;
- public class jhjh {
- //Atributos
- private Map<String,Integer> mapeo;
- private String archivo;
- private int cont;
- //Constructor
- public jhjh(String archivo){
- this.archivo=archivo;
- cont=0;
- mapeo= new OpenHashMap<String,Integer>();
- }
- //Metodos
- public float porcentaje(String palabra){
- try{
- return (mapeo.get(palabra)*100)/cont;
- }
- catch(TDAMapeo.InvalidKeyException e){
- return -10;
- }
- }
- public void procesarArchivo(){
- try{
- BufferedReader in = new BufferedReader(new FileReader(archivo));
- String str;
- while ((str = in.readLine())!= null){
- str.toLowerCase();
- int i=0;
- String palabra="";
- while(i<str.length()){
- char aux=str.charAt(i);
- if(esLetra(aux)){
- palabra+=str.charAt(i);
- //i++;
- }
- else{
- if(palabra.length()>1){
- System.out.println(palabra);
- if(estaPalabra(palabra))
- {int ct=mapeo.get(palabra)+1;
- mapeo.put(palabra, ct); }
- else
- mapeo.put(palabra, 1);
- cont++;
- palabra="";
- }
- }
- i++;
- }
- }
- in.close();
- }
- catch(TDAMapeo.InvalidKeyException e){
- System.out.println(e.getMessage());
- }
- catch (IOException err) {
- System.out.println("Error al leer el archivo");
- }
- }
- private boolean estaPalabra(String p)
- {
- for(TDAMapeo.Entry<String,Integer> e: mapeo.entries())
- if(e.getKey().equals(p))
- return true;
- return false;
- }
- private boolean esSeparador(char c){
- boolean salida= false;
- if (c=='.' || c==',' || c==';' || c==' ' || c=='?' || c=='!' || (c>= '0' && c<='9') || c=='(' || c==')');
- salida= true;
- return salida;
- }
- private boolean esLetra(char c){
- return c>='a' && c<='z';
- }
- public TDAMapeo.Entry<String,Integer>[] palabraMasUsada()
- {
- HeapPriorityQueue<String,Integer> cp=new HeapPriorityQueue <String ,Integer>();
- try{
- for(TDAMapeo.Entry<String,Integer> e: mapeo.entries())
- cp.insert(e.getKey(), e.getValue());
- TDAMapeo.Entry<String,Integer> [] arreg= (TDAMapeo.Entry<String,Integer> []) new Object [5];
- for(int i=0; i<5;i++)
- arreg[i]= (TDAMapeo.Entry<String,Integer>) cp.removeMin();
- return arreg;
- }
- catch(EmptyPriorityQueueException e){System.out.println(e.getMessage());}
- catch(TDAPriorityQueue.InvalidKeyException e){System.out.println(e.getMessage());}
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement