package rake4j.core.index;

import io.deepreader.java.commons.util.Displayer;
import rake4j.core.model.Document;
import rake4j.core.model.Term;

import java.io.Serializable;
import java.util.*;

/**
 * User: Danyang
 * Date: 1/6/2015
 * Time: 20:15
 */
public class Index implements Serializable {
    Map<String, PostingsArray> invertedIndex = new HashMap<>();
    int numDocs = 0;

    public void processDoc(Document doc, float topPercentage) {
        numDocs++;

        TreeMap<Integer, Term> termMap = doc.getTermMap();
        Set<String> repeated = new HashSet<>();

        float upper = termMap.size()*topPercentage;
        Iterator itr = termMap.entrySet().iterator();
        for(int cnt=0; itr.hasNext() && cnt<=upper; cnt++) {
            Map.Entry e = (Map.Entry) itr.next();
            Term t = (Term) e.getValue();
            String s = t.getTermText();
            if(!invertedIndex.containsKey(s)) {
                invertedIndex.put(s, new PostingsArray());
            }
            if(!repeated.contains(s)) {
                invertedIndex.get(s).df += 1;
                repeated.add(s);
            }
            invertedIndex.get(s).tf += 1;
        }
    }

    @Override
    public String toString() {
        return Displayer.display(this.invertedIndex);
    }

    public Integer docFreq(String term) {
        if(invertedIndex.containsKey(term)) {
            return invertedIndex.get(term).df;
        }
        return 0;
    }

    public Integer totalTermFreq(String term) {
        if(invertedIndex.containsKey(term)) {
            return invertedIndex.get(term).tf;
        }
        return 0;
    }

    public Integer numDocs() {
        return numDocs;
    }
}

@@ -375,16 +375,69 @@
 ocs = 0;
+%0A    int totalTermFreq = 0;%0A    int totalDocFreq = 0;
 %0A%0A    pu
@@ -1120,32 +1120,67 @@
 get(s).df += 1;%0A
+                totalDocFreq += 1;%0A
                 
@@ -1244,32 +1244,64 @@
 get(s).tf += 1;%0A
+            totalTermFreq += 1;%0A
         %7D%0A    %7D%0A
@@ -1780,24 +1780,24 @@
 numDocs() %7B%0A
-
         retu
@@ -1813,11 +1813,245 @@
 s;%0A    %7D
+%0A%0A    public int totalTermFreq() %7B%0A        return totalTermFreq;%0A    %7D%0A%0A    public int totalDocFreq() %7B%0A        return totalDocFreq;%0A    %7D%0A%0A    public Map%3CString, PostingsArray%3E getInvertedIndex() %7B%0A        return invertedIndex;%0A    %7D
 %0A%7D%0A

add overall index information