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

ai.preferred.regression.pe.data.Vocabulary Maven / Gradle / Ivy

The newest version!
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package ai.preferred.regression.pe.data;

import java.util.*;

public class Vocabulary {

  private final ArrayList vocabularyList;
  private final Map vocabularyMap;

  public Vocabulary(Collection vocabulary) {
    vocabularyList = new ArrayList<>(vocabulary);
    Collections.sort(vocabularyList);
    vocabularyMap = new HashMap<>(vocabularyList.size());
    for (final String w : vocabularyList) {
      vocabularyMap.put(w, vocabularyMap.size());
    }
  }

  public List getVocabularyList() {
    return Collections.unmodifiableList(vocabularyList);
  }

  public String[] getVocabularyArray() {
    return vocabularyList.toArray(new String[0]);
  }

  public int getIndex(String w) {
    final Integer index = vocabularyMap.get(w);
    if (index == null) {
      return 0;
    }
    return index;
  }

  public String getWord(int index) {
    if (index >= 0 && index < vocabularyList.size()) {
      return vocabularyList.get(index);
    }
    throw new IllegalArgumentException("No such index in the vocabulary: " + index);
  }

  public int size() {
    return vocabularyList.size();
  }

  @Override
  public String toString() {
    return "Vocabulary{" + vocabularyList + '}';
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy