All Downloads are FREE. Search and download functionalities are using the official Maven repository.

chalk.tools.namefind.NameSampleSequenceStream Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 package chalk.tools.namefind;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import nak.core.AbstractModel;
import nak.core.Event;
import nak.core.Sequence;
import nak.data.SequenceStream;
import chalk.tools.util.ObjectStream;
import chalk.tools.util.featuregen.AdaptiveFeatureGenerator;


public class NameSampleSequenceStream implements SequenceStream {

  private NameContextGenerator pcg;
  private List samples;
  
  public NameSampleSequenceStream(ObjectStream psi) throws IOException {
    this(psi, new DefaultNameContextGenerator((AdaptiveFeatureGenerator) null));
  }
  
  public NameSampleSequenceStream(ObjectStream psi, AdaptiveFeatureGenerator featureGen) 
  throws IOException {
    this(psi, new DefaultNameContextGenerator(featureGen));
  }
  
  public NameSampleSequenceStream(ObjectStream psi, NameContextGenerator pcg)
      throws IOException {
    samples = new ArrayList();
    
    NameSample sample;
    while((sample = psi.read()) != null) {
      samples.add(sample);
    }
    
    System.err.println("Got "+samples.size()+" sequences");
    
    this.pcg = pcg;
  }
  
  
  @SuppressWarnings("unchecked")
  public Event[] updateContext(Sequence sequence, AbstractModel model) {
    Sequence pss = sequence;
    TokenNameFinder tagger = new NameFinderME(new TokenNameFinderModel("x-unspecified", model, Collections.emptyMap(), null));
    String[] sentence = pss.getSource().getSentence();
    String[] tags = NameFinderEventStream.generateOutcomes(tagger.find(sentence), null, sentence.length);
    Event[] events = new Event[sentence.length];
    
    NameFinderEventStream.generateEvents(sentence,tags,pcg).toArray(events);
    
    return events;
  }
  
  @SuppressWarnings("unchecked")
  public Iterator iterator() {
    return new NameSampleSequenceIterator(samples.iterator());
  }

}

class NameSampleSequenceIterator implements Iterator {

  private Iterator psi;
  private NameContextGenerator cg;
  
  public NameSampleSequenceIterator(Iterator psi) {
    this.psi = psi;
    cg = new DefaultNameContextGenerator((AdaptiveFeatureGenerator)null);
  }
  
  public boolean hasNext() {
    return psi.hasNext();
  }

  public Sequence next() {
    NameSample sample = psi.next();
    
    String sentence[] = sample.getSentence();
    String tags[] = NameFinderEventStream.generateOutcomes(sample.getNames(), null, sentence.length);
    Event[] events = new Event[sentence.length];
    
    for (int i=0; i < sentence.length; i++) {

      // it is safe to pass the tags as previous tags because
      // the context generator does not look for non predicted tags
      String[] context = cg.getContext(i, sentence, tags, null);

      events[i] = new Event(tags[i], context);
    }
    Sequence sequence = new Sequence(events,sample);
    return sequence;
  }

  public void remove() {
    throw new UnsupportedOperationException();
  }
  
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy