<commit_before>/**
 * 
 */
package edu.jhu.hlt.rebar.stages;

import java.util.Iterator;

import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.MutationsRejectedException;
import org.apache.accumulo.core.data.Mutation;
import org.apache.accumulo.core.data.Value;
import org.apache.thrift.TException;

import edu.jhu.hlt.asphalt.Stage;
import edu.jhu.hlt.concrete.Communication;
import edu.jhu.hlt.concrete.Section;
import edu.jhu.hlt.concrete.SectionSegmentation;
import edu.jhu.hlt.rebar.AnnotationException;
import edu.jhu.hlt.rebar.Constants;
import edu.jhu.hlt.rebar.RebarException;
import edu.jhu.hlt.rebar.Util;
import edu.jhu.hlt.rebar.annotations.AbstractRebarAnnotation;

/**
 * @author max
 *
 */
public class SectionStage extends AbstractStage<SectionSegmentation> {
  
  public SectionStage(Connector conn, Stage stage) throws RebarException {
    super(conn, stage);
  }

  public SectionStage(Stage stage) throws RebarException {
    this(Constants.getConnector(), stage);
  }

  public void annotate(SectionSegmentation ss, String docId) throws RebarException, AnnotationException {
    try {
      Communication c = this.reader.get(docId);
      AbstractRebarAnnotation<SectionSegmentation> rss = new AbstractRebarAnnotation<SectionSegmentation>(ss) {

        @Override
        public boolean validate(Communication c) throws RebarException {
          boolean valid = 
              this.annotation.metadata != null
              && Util.isValidUUIDString(this.annotation.uuid)
              && this.annotation.isSetSectionList();
          Iterator<Section> sects = this.annotation.getSectionListIterator();
          while (valid && sects.hasNext()) {
            Section s = sects.next();
            valid = 
                Util.isValidUUIDString(s.uuid);
          }
          
          return valid;
        }
      };
      
      if (rss.validate(c)) {
        Mutation m = new Mutation(c.uuid);
        m.put("annotations", this.stage.name, new Value(this.serializer.serialize(ss)));

        this.idxBw.addMutation(Util.generateEmptyValueMutation("stage:"+this.stage.name, c.uuid, ""));
        this.bw.addMutation(m);
      } else {
        throw new AnnotationException("Your SectionSegmentation object was invalid.");
      }
    } catch (TException | MutationsRejectedException e) {
      throw new RebarException(e);
    }
  }

  @Override
  public Iterator<Communication> getDocuments() throws RebarException {
    // TODO Auto-generated method stub
    return null;
  }
}<commit_msg>Blah - mid refactoring<commit_after>/**
 * 
 */
package edu.jhu.hlt.rebar.stages;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.accumulo.core.client.BatchScanner;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.MutationsRejectedException;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Mutation;
import org.apache.accumulo.core.data.Range;
import org.apache.accumulo.core.data.Value;
import org.apache.thrift.TException;

import edu.jhu.hlt.asphalt.Stage;
import edu.jhu.hlt.concrete.Communication;
import edu.jhu.hlt.concrete.Section;
import edu.jhu.hlt.concrete.SectionSegmentation;
import edu.jhu.hlt.rebar.AnnotationException;
import edu.jhu.hlt.rebar.Configuration;
import edu.jhu.hlt.rebar.Constants;
import edu.jhu.hlt.rebar.RebarException;
import edu.jhu.hlt.rebar.Util;
import edu.jhu.hlt.rebar.accumulo.AbstractReader;
import edu.jhu.hlt.rebar.annotations.AbstractRebarAnnotation;

/**
 * @author max
 *
 */
public class SectionStage extends AbstractStage<SectionSegmentation> {
  
  private class SectionCommunicationReader extends AbstractReader<Communication> {

    public SectionCommunicationReader(String tableName, String idxTableName) throws RebarException {
      this(Constants.getConnector(), tableName, idxTableName);
    }
    
    public SectionCommunicationReader(Connector conn, String tableName, String idxTableName) throws RebarException {
      super(conn, tableName, idxTableName);
    }
    
    public Iterator<Communication> mergedIterator(String stageName) throws RebarException {
      Range r = new Range("stage:"+stageName);
      Set<Range> ranges = this.scanIndexTableColF(r);
      return null;
    }

    @Override
    protected Iterator<Communication> accumuloIterToTIter(Iterator<Entry<Key, Value>> accIter) throws RebarException {
      // TODO Auto-generated method stub
      return null;
    }
    
  }
  
  public SectionStage(Connector conn, Stage stage) throws RebarException {
    super(conn, stage);
  }

  public SectionStage(Stage stage) throws RebarException {
    this(Constants.getConnector(), stage);
  }

  public void annotate(SectionSegmentation ss, String docId) throws RebarException, AnnotationException {
    try {
      Communication c = this.reader.get(docId);
      AbstractRebarAnnotation<SectionSegmentation> rss = new AbstractRebarAnnotation<SectionSegmentation>(ss) {

        @Override
        public boolean validate(Communication c) throws RebarException {
          boolean valid = 
              this.annotation.metadata != null
              && Util.isValidUUIDString(this.annotation.uuid)
              && this.annotation.isSetSectionList();
          Iterator<Section> sects = this.annotation.getSectionListIterator();
          while (valid && sects.hasNext()) {
            Section s = sects.next();
            valid = 
                Util.isValidUUIDString(s.uuid);
          }
          
          return valid;
        }
      };
      
      if (rss.validate(c)) {
        Mutation m = new Mutation(c.uuid);
        m.put("annotations", this.stage.name, new Value(this.serializer.serialize(ss)));

        this.idxBw.addMutation(Util.generateEmptyValueMutation("stage:"+this.stage.name, c.uuid, ""));
        this.bw.addMutation(m);
      } else {
        throw new AnnotationException("Your SectionSegmentation object was invalid.");
      }
    } catch (TException | MutationsRejectedException e) {
      throw new RebarException(e);
    }
  }

  @Override
  public Iterator<Communication> getDocuments() throws RebarException {

    // a
    // TODO Auto-generated method stub
    return null;
  }
  
  
}
{'fraction_non_alphanumeric': 0.10554421234490341, 'fraction_numerical': 0.0, 'mean_word_length': 3.337874659400545, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4262721', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1892, 'n_words': 355}