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

com.joliciel.talismane.machineLearning.maxent.custom.TwoPassDataIndexer Maven / Gradle / Ivy

There is a newer version: 6.1.8
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 com.joliciel.talismane.machineLearning.maxent.custom;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.joliciel.talismane.utils.LogUtils;

import opennlp.model.AbstractDataIndexer;
import opennlp.model.ComparableEvent;
import opennlp.model.Event;
import opennlp.model.EventStream;
import opennlp.model.FileEventStream;

/**
 * Collecting event and context counts by making two passes over the events. The
 * first pass determines which contexts will be used by the model, and the
 * second pass creates the events in memory containing only the contexts which
 * will be used. This greatly reduces the amount of memory required for storing
 * the events. During the first pass a temporary event file is created which is
 * read during the second pass.
 */
public class TwoPassDataIndexer extends AbstractDataIndexer {
  private static final Logger LOG = LoggerFactory.getLogger(TwoPassDataIndexer.class);

  /**
   * One argument constructor for DataIndexer which calls the two argument
   * constructor assuming no cutoff.
   *
   * @param eventStream
   *          An Event[] which contains the a list of all the Events seen in the
   *          training data.
   */
  public TwoPassDataIndexer(EventStream eventStream) throws IOException {
    this(eventStream, 0);
  }

  public TwoPassDataIndexer(EventStream eventStream, int cutoff) throws IOException {
    this(eventStream, cutoff, true);
  }

  /**
   * Two argument constructor for DataIndexer.
   *
   * @param eventStream
   *          An Event[] which contains the a list of all the Events seen in the
   *          training data.
   * @param cutoff
   *          The minimum number of times a predicate must have been observed in
   *          order to be included in the model.
   */
  @SuppressWarnings("unchecked")
  public TwoPassDataIndexer(EventStream eventStream, int cutoff, boolean sort) throws IOException {
    Map predicateIndex = new HashMap();
    @SuppressWarnings("rawtypes")
    List eventsToCompare;

    LOG.info("Indexing events using cutoff of " + cutoff);

    LOG.info("Computing event counts...  ");
    try {
      File tmp = File.createTempFile("events", null);
      tmp.deleteOnExit();
      Writer osw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmp), "UTF8"));
      int numEvents = computeEventCounts(eventStream, osw, predicateIndex, cutoff);
      LOG.info("done. " + numEvents + " events");

      LOG.info("Indexing...  ");

      eventsToCompare = index(numEvents, this.getFileEventStream(tmp), predicateIndex);
      // done with predicates
      predicateIndex = null;
      tmp.delete();
      LOG.info("done.");

      if (sort) {
        System.out.print("Sorting and merging events... ");
      } else {
        System.out.print("Collecting events... ");
      }
      sortAndMerge(eventsToCompare, sort);
      LOG.info("Done indexing.");
    } catch (IOException e) {
      LogUtils.logError(LOG, e);
    }
  }

  /**
   * Reads events from eventStream into a linked list. The predicates
   * associated with each event are counted and any which occur at least
   * cutoff times are added to the predicatesInOut map along
   * with a unique integer index.
   *
   * @param eventStream
   *          an EventStream value
   * @param eventStore
   *          a writer to which the events are written to for later processing.
   * @param predicatesInOut
   *          a TObjectIntHashMap value
   * @param cutoff
   *          an int value
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  private int computeEventCounts(EventStream eventStream, Writer eventStore, Map predicatesInOut, int cutoff) throws IOException {
    Map counter = new HashMap();
    int eventCount = 0;
    Set predicateSet = new HashSet();
    while (eventStream.hasNext()) {
      Event ev = eventStream.next();
      eventCount++;
      eventStore.write(this.toLine(ev));
      String[] ec = ev.getContext();
      update(ec, predicateSet, counter, cutoff);
    }
    predCounts = new int[predicateSet.size()];
    int index = 0;
    for (Iterator pi = predicateSet.iterator(); pi.hasNext(); index++) {
      String predicate = (String) pi.next();
      predCounts[index] = counter.get(predicate);
      predicatesInOut.put(predicate, index);
    }
    eventStore.close();
    return eventCount;
  }

  @SuppressWarnings({ "rawtypes", "unchecked" })
  protected List index(int numEvents, EventStream es, Map predicateIndex) throws IOException {
    Map omap = new HashMap();
    int outcomeCount = 0;
    List eventsToCompare = new ArrayList(numEvents);
    List indexedContext = new ArrayList();
    while (es.hasNext()) {
      Event ev = es.next();
      String[] econtext = ev.getContext();
      ComparableEvent ce;

      int ocID;
      String oc = ev.getOutcome();

      if (omap.containsKey(oc)) {
        ocID = omap.get(oc);
      } else {
        ocID = outcomeCount++;
        omap.put(oc, ocID);
      }

      for (int i = 0; i < econtext.length; i++) {
        String pred = econtext[i];
        if (predicateIndex.containsKey(pred)) {
          indexedContext.add(predicateIndex.get(pred));
        }
      }

      // drop events with no active features
      if (indexedContext.size() > 0) {
        int[] cons = new int[indexedContext.size()];
        for (int ci = 0; ci < cons.length; ci++) {
          cons[ci] = indexedContext.get(ci);
        }
        ce = new ComparableEvent(ocID, cons);
        eventsToCompare.add(ce);
      } else {
        LOG.debug("Dropped event " + ev.getOutcome() + ":" + Arrays.asList(ev.getContext()));
      }
      // recycle the TIntArrayList
      indexedContext.clear();
    }
    outcomeLabels = toIndexedStringArray(omap);
    predLabels = toIndexedStringArray(predicateIndex);
    return eventsToCompare;
  }

  protected EventStream getFileEventStream(File file) throws IOException {
    return new FileEventStream(file);
  }

  protected String toLine(Event ev) {
    return FileEventStream.toLine(ev);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy