0

Example:

d1 = "the sky is blue"

d2 = "the car is blue"

Key          Value
the          [<d1,1>,<d2,1>]
sky          [<d1,1>]
is           [<d1,1>,<d2,1>]
blue         [<d1,1>,<d2,1>]
car          [<d2,1>]

Where: key = String

ex:

<d1,1>

d1 = Document id

1 = How many times the word apear on file

I created a document type object with the docid variables and frequency.

public class Documento {

    private final int docid;
    private final int frequencia;

    public Documento(int docid, int frequencia) {
        this.docid = docid;
        this.frequencia = frequencia;
    }

    public int getDocid() {
        return docid;
    }

    public int getFrequencia() {
        return frequencia;
    }

    @Override
    public boolean equals(Object o) {
        if ((o instanceof Documento) && docid == ((Documento) o).docid && frequencia == ((Documento) o).frequencia) {
            return true;
        }
        return false;
    }

And the dictionary class that is a hashmap with

public class Dicionario {

    public Map<String, Documento> indice = new HashMap<>();

    public void InsereDicionario(String palavra, int docid) {
        int cont = indice.containsKey(palavra) ? indice.get(palavra).getFrequencia() : 0;
        indice.put(palavra, new Documento(docid, cont + 1));
    }

    public int frequencia(String palavra) {
        return indice.get(palavra).getFrequencia();
    }

    public void criaDicionario(String entrada) {
        String[] palavras = entrada.split("\\s+");
        for (int i = 0; i < palavras.length; i++) {
            InsereDicionario(palavras[i], 1);
        }

    }
    public void ListaPalavras(){
        for(String key:indice.keySet()){
            System.out.println("");
        }

}

But what I really need the dictionary is a list of documents , and I do not know how to do this , someone could help me ?

or is there an easier way to do this ?

2 Answers 2

2

If you need a list of documents, why not create one? With Java8 this becomes even more convenient:

For example:

public Map<String, List<Documento>> indice = new HashMap<>();

//register new word
indice.putIfAbsent(palavra, new ArrayList<>());
//add additional occurence
indice.get(palavra).add(documento);
//get frequency
int frequencia = indice.get(palavra)
                       .stream()
                       .map(d -> d.getFrequencia())
                       .reduce(0, (s, i) -> s + i);

An alternative would be to use Guava's Multimap, see here

Sign up to request clarification or add additional context in comments.

1 Comment

.map(d -> d.getFrequencia()) returning: method map in interface Stream<T> cannot be applied to given types; required: Function<? super Documento,? extends R> found: <none> reason: cannot infer type-variable(s) R (argument mismatch; <none> cannot be converted to Function<? super Documento,? extends R>) where R,T are type-variables: R extends Object declared in method <R>map(Function<? super T,? extends R>) T extends Object declared in interface Stream
1
Map<String, List<Documento>>

Obviously you need to adapt the rest of the code.

For example, when you need to add something to the dictionary, if it's the first time you need to create the List with that single document, next time you need to take the already created list and add documents there.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.