content: /*  PseudosyllableFeatureExtractor.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;
  
  import edu.cuny.qc.speech.AuToBI.core.Region;
  import edu.cuny.qc.speech.AuToBI.core.FeatureExtractor;
  import edu.cuny.qc.speech.AuToBI.core.WavData;
  import edu.cuny.qc.speech.AuToBI.core.syllabifier.Syllabifier;
  import edu.cuny.qc.speech.AuToBI.core.syllabifier.VillingSyllabifier;
  import edu.cuny.qc.speech.AuToBI.util.SubregionUtils;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  
  /**
   * PseudosyllableFeatureExtractor hypothesizes syllables for a given signal and aligns the loudest contained syllable to
   * each region.
   * <p/>
   * These pseudosyllables are generated by Syllabifier, and are stored as SubWord features on the regions.
   *
   * @see Syllabifier
   * @see edu.cuny.qc.speech.AuToBI.core.SubWord
   */
  @SuppressWarnings("unchecked")
  public class PseudosyllableFeatureExtractor extends FeatureExtractor {
    public static final String moniker = "psyl";
    private String subregion_name;  // the feature name to store the subregion
  
    /**
     * Constructs a new PseudosyllableFeatureExtractor.
     *
     * @param subregion_name the feature name to store the subregion
     */
    @Deprecated
    public PseudosyllableFeatureExtractor(String subregion_name) {
      super();
      this.subregion_name = subregion_name;
      this.required_features.add("wav");
      this.extracted_features.add(subregion_name);
    }
  
    public PseudosyllableFeatureExtractor() {
      super();
      this.subregion_name = moniker;
      this.required_features.add("wav");
      this.extracted_features.add(subregion_name);
    }
  
    /**
     * Identifies the loudest pseudosyllable for a given region, constructs a SubWord object, and stores it with each
     * region.
     *
     * @param regions The regions to extract features from.
     * @throws edu.cuny.qc.speech.AuToBI.featureextractor.FeatureExtractorException if anything goes wrong.
     */
    public void extractFeatures(List regions) throws FeatureExtractorException {
      // Identify all regions which are associated with each wav data.
      HashMap<WavData, List<Region>> wave_region_map = new HashMap<WavData, List<Region>>();
      for (Region r : (List<Region>) regions) {
        WavData wav = (WavData) r.getAttribute("wav");
        if (wav != null) {
          if (!wave_region_map.containsKey(wav)) {
            wave_region_map.put(wav, new ArrayList<Region>());
          }
          wave_region_map.get(wav).add(r);
        }
      }
  
      for (WavData wav : wave_region_map.keySet()) {
        Syllabifier syllabifier = new VillingSyllabifier();
        List<Region> pseudosyllables = syllabifier.generatePseudosyllableRegions(wav);
  
        SubregionUtils.alignLongestSubregionsToWords(regions, pseudosyllables, subregion_name);
      }
  
    }
  }
  