/*
 * Copyright (C) 2012 Fabian Hirschmann <fabian@hirschm.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.github.fhirschmann.clozegen.lib.annotators;

import com.github.fhirschmann.clozegen.lib.type.GapAnnotation;
import java.util.Iterator;

import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.NonEmptyStringList;
import org.uimafit.component.JCasAnnotator_ImplBase;
import org.apache.uima.jcas.tcas.Annotation;
import org.uimafit.util.FSCollectionFactory;

/**
 * Base class for all cloze item annotations.
 *
 * @author Fabian Hirschmann <fabian@hirschm.net>
 */
public abstract class GapAnnotator extends JCasAnnotator_ImplBase {
    /**
     * Returns the type of the word class an extending class is looking for.
     *
     * This should be implemented by all inheriting classes.
     *
     * @see de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos
     * @return word class type
     */
    public abstract int getType();

    /**
     * Generates cloze tests item from a given subject (a word in a word class).
     *
     * This method should generate a number of valid and invalid answers
     * for a given subject. For example, the subject "a" in the "articles" class
     * might have "a" as only valid answers and {"an","the"} as invalid answers.
     *
     * @param subject the word to generate a cloze test item for
     * @return valid and invalid answers for a gap
     */
    public abstract Gap generate(Annotation subject);

    /**
     * Process the annotator.
     *
     * This method will set up the annotation for words in a word class
     * (as defined by the extending classes) and call generate() in the
     * extending class for each word in this class.
     *
     * @param jcas the CAS to work on
     * @throws AnalysisEngineProcessException
     */
    @Override
    public final void process(final JCas jcas) throws AnalysisEngineProcessException {
        for (Iterator<Annotation> i = jcas.getAnnotationIndex(
                getType()).iterator(); i.hasNext();) {
            Annotation subject = i.next();
            GapAnnotation annotation = new GapAnnotation(jcas);

            annotation.setBegin(subject.getBegin());
            annotation.setEnd(subject.getEnd());

            Gap pair = generate(subject);
            NonEmptyStringList d = (NonEmptyStringList) FSCollectionFactory.
                    createStringList(jcas, pair.getInvalidAnswers());
            annotation.setInvalidAnswers(d);
            NonEmptyStringList a = (NonEmptyStringList) FSCollectionFactory.
                    createStringList(jcas, pair.getValidAnswers());
            annotation.setValidAnswers(a);
            annotation.addToIndexes();
        }
    }
}


@@ -895,24 +895,113 @@
 Annotation;%0A
+import de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS;%0Aimport java.util.Arrays;%0A
 import java.
@@ -1492,16 +1492,17 @@
 lBase %7B%0A
+%0A
     /**%0A
@@ -1524,20 +1524,8 @@
 the 
-type of the 
 word
@@ -1534,42 +1534,37 @@
 lass
+es
  an 
-extending class
+annotator
  is 
-loo
+wor
 king 
-for
+on
 .%0A  
@@ -1763,19 +1763,31 @@
 act 
-int getType
+String%5B%5D getWordClasses
 ();%0A
@@ -1955,23 +1955,16 @@
  answers
-%0A     *
  for a g
@@ -1959,32 +1959,39 @@
 wers for a given
+%0A     *
  subject. For ex
@@ -2036,23 +2036,16 @@
 s%22 class
-%0A     *
  might h
@@ -2054,16 +2054,23 @@
 e %22a%22 as
+%0A     *
  only va
@@ -2420,23 +2420,16 @@
 rd class
-%0A     *
  (as def
@@ -2439,16 +2439,23 @@
 d by the
+%0A     *
  extendi
@@ -2488,31 +2488,24 @@
 ate() in the
-%0A     *
  extending c
@@ -2530,16 +2530,23 @@
  in this
+%0A     *
  class.%0A
@@ -2828,17 +2828,16 @@
     
-getT
+POS.t
 ype
-()
 ).it
@@ -2901,24 +2901,146 @@
 = i.next();%0A
+            POS pos = (POS) subject;%0A%0A            if (Arrays.asList(getWordClasses()).contains(pos.getPosValue())) %7B%0A%0A    
             
@@ -3088,24 +3088,28 @@
 ion(jcas);%0A%0A
+    
             
@@ -3153,32 +3153,36 @@
 ));%0A            
+    
 annotation.setEn
@@ -3215,16 +3215,20 @@
         
+    
 Gap pair
@@ -3249,16 +3249,105 @@
 bject);%0A
+%0A                if (pair == null) %7B%0A                    continue;%0A                %7D%0A    
         
@@ -3427,32 +3427,36 @@
                 
+    
 createStringList
@@ -3493,32 +3493,36 @@
 ));%0A            
+ 
+   
 annotation.setIn
@@ -3538,16 +3538,20 @@
 ers(d);%0A
+    
         
@@ -3631,32 +3631,36 @@
                 
+  
+  
 createStringList
@@ -3695,32 +3695,36 @@
 ));%0A            
+    
 annotation.setVa
@@ -3742,32 +3742,36 @@
 a);%0A            
+  
+  
 annotation.addTo
@@ -3777,24 +3777,38 @@
 oIndexes();%0A
+            %7D%0A
         %7D%0A  
@@ -3813,9 +3813,8 @@
     %7D%0A%7D%0A
-%0A

use the POS Annotator model instead of multiple POS annotators