package annotators;

import java.util.TreeMap;

import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.descriptor.ExternalResource;
import org.apache.uima.jcas.JCas;

import resources.Candidate;
import resources.CandidateList;

public class CandidateConsumer extends JCasAnnotator_ImplBase {
  public final static String CANDIDATE_KEY = "candidateKey";

  @ExternalResource(key = CANDIDATE_KEY)
  private CandidateList collection;

  public static final String PARAM_FILENAME = "filename_";

  @ConfigurationParameter(name = PARAM_FILENAME, mandatory = true, defaultValue = "target/candidates.txt")
  private String filename_;

  @Override
  public void collectionProcessComplete() throws AnalysisEngineProcessException {
    // TODO Auto-generated method stub
    super.collectionProcessComplete();
    System.out.println("Collection Process Complete --> saving candidates...");
    // compute DF and IDF
    for (TreeMap<String, Candidate> ac : collection.getAllCandidates()) {
      for (Candidate c : ac.values()) {
        int df = 0;
        for (TreeMap<String, Candidate> ac2 : collection.getAllCandidates()) {
          if (ac2.containsKey(c.getName())) {
            df++;
          }
        }
        c.setDocument_frequency(df);
        c.setInverse_document_frequency(-Math.log((c.getDocument_frequency() + 1.0)
                / (collection.getAllCandidates().size() + 1.0)));
      }
    }
    collection.save(filename_);
    System.out.println("Candidates successfully saved to " + filename_);
  }

  @Override
  public void process(JCas arg0) throws AnalysisEngineProcessException {

  }
}

@@ -25,27 +25,148 @@
 ort 
-java.util.TreeMap;%0A
+static org.apache.uima.fit.util.JCasUtil.select;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.TreeMap;%0A%0Aimport org.apache.uima.UimaContext;
 %0Aimp
@@ -450,67 +450,205 @@
 as;%0A
-%0Aimport resources.Candidate;%0Aimport resources.CandidateList
+import org.apache.uima.resource.ResourceInitializationException;%0A%0Aimport resources.Candidate;%0Aimport resources.CandidateList;%0Aimport de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData
 ;%0A%0Ap
@@ -1044,16 +1044,55 @@
 lename_;
+%0A  %0A  private ArrayList%3CString%3E files_;
 %0A%0A  @Ove
@@ -1888,24 +1888,32 @@
 ve(filename_
+, files_
 );%0A    Syste
@@ -2024,12 +2024,12 @@
 Cas 
-arg0
+jcas
 ) th
@@ -2066,15 +2066,346 @@
 ption %7B%0A
+    for (DocumentMetaData dmd : select(jcas, DocumentMetaData.class)) %7B%0A      files_.add(dmd.getDocumentTitle());%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void initialize(UimaContext context) throws ResourceInitializationException %7B%0A    // TODO Auto-generated method stub%0A    super.initialize(context);%0A    files_ = new ArrayList%3CString%3E();
 %0A  %7D%0A%7D%0A

add filename to the resource