content: /*  QuantizedContourModel.java
  
      Copyright (c) 2009-2014 Andrew Rosenberg
  
    This file is part of the AuToBI prosodic analysis package.
  
    AuToBI is free software: you can redistribute it and/or modify
    it under the terms of the Apache License (see boilerplate below)
  
   ***********************************************************************************************************************
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
   * the License. You should have received a copy of the Apache 2.0 License along with AuToBI.
   * 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 edu.cuny.qc.speech.AuToBI.featureextractor.shapemodeling;
  
  import edu.cuny.qc.speech.AuToBI.core.ConditionalDistribution;
  import edu.cuny.qc.speech.AuToBI.core.Contour;
  
  /**
   * QuantizedContourModel is a sequential multinomial model of a quantized contour.
   */
  public class QuantizedContourModel {
    public ContourQuantizer cq;  // A quantizer.
    public ConditionalDistribution[] time_models;  // The component time aligned models.
  
    /**
     * Constructs a new QuantizedContourModel.
     *
     * @param cq          a quantizer
     * @param time_models the time aligned models
     */
    public QuantizedContourModel(ContourQuantizer cq, ConditionalDistribution[] time_models) {
      this.cq = cq;
      this.time_models = time_models;
    }
  
    /**
     * Generates the log likelihood that a contour was generated by this QuantizedContourModel.
     *
     * @param c the contour
     * @return the log likelihood
     * @throws ContourQuantizerException if the contour cannot be quantized.
     */
    public double evaluateContour(Contour c) throws ContourQuantizerException {
      int[] quantized = cq.quantize(c);
      double log_p = 0;
  
      String prev_value = "";
      for (int i = 0; i < quantized.length; ++i) {
        String value = Integer.toString(quantized[i]);
        if (time_models[i].containsKey(prev_value) && time_models[i].get(prev_value).containsKey(value)) {
          log_p += Math.log(time_models[i].get(prev_value).get(value));
        } else {
          log_p = -Double.MAX_VALUE;
        }
        prev_value = value;
      }
  
      return log_p;
    }
  }
  