gov.sandia.cognition.text.term.DefaultTermIndex Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cognitive-foundry Show documentation
Show all versions of cognitive-foundry Show documentation
A single jar with all the Cognitive Foundry components.
/*
* File: DefaultTermIndex.java
* Authors: Justin Basilico
* Company: Sandia National Laboratories
* Project: Cognitive Foundry
*
* Copyright February 19, 2009, Sandia Corporation.
* Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
* license for use of this work by or on behalf of the U.S. Government. Export
* of this program may require a license from the United States Government.
* See CopyrightHistory.txt for complete details.
*
*/
package gov.sandia.cognition.text.term;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A default implementation of the {@code TermIndex} interface.
*
* @author Justin Basilico
* @since 3.0
*/
public class DefaultTermIndex
extends AbstractTermIndex
{
/** The mapping of terms to their corresponding (unique) indices. */
protected Map termMap;
/** The list of terms ordered by indices. */
protected List termList;
/**
* Creates a new, empty {@code DefaultTermIndex}.
*/
public DefaultTermIndex()
{
super();
this.setTermMap(new HashMap());
this.setTermList(new ArrayList());
}
@Override
public DefaultTermIndex clone()
{
final DefaultTermIndex clone = (DefaultTermIndex) super.clone();
clone.termMap = new HashMap(this.termMap);
clone.termList = new ArrayList(this.termList);
return clone;
}
public IndexedTerm add(
final Term term)
{
if (term == null)
{
// Don't add a null term.
return null;
}
// Get the current index of the term.
DefaultIndexedTerm indexedTerm = this.getIndexedTerm(term);
if (indexedTerm == null)
{
// The term is not in the index, so add it.
int index = termList.size();
indexedTerm = new DefaultIndexedTerm(index, term);
this.termMap.put(term, indexedTerm);
this.termList.add(indexedTerm);
}
// Return the index for the term.
return indexedTerm;
}
public int getTermCount()
{
return this.termList.size();
}
public List getTerms()
{
return Collections.unmodifiableList(this.getTermList());
}
public DefaultIndexedTerm getIndexedTerm(
final Term term)
{
return this.termMap.get(term);
}
public DefaultIndexedTerm getIndexedTerm(
final int index)
{
if (index < 0 || index >= this.getTermCount())
{
// Bad index.
return null;
}
else
{
// Get the index of the term.
return this.termList.get(index);
}
}
/**
* Gets the mapping of terms to their indices.
*
* @return
* The mapping of terms to their indices.
*/
protected Map getTermMap()
{
return this.termMap;
}
/**
* Sets the mapping of terms to their indices.
*
* @param termMap
* The mapping of terms to their indices.
*/
protected void setTermMap(
final Map termMap)
{
this.termMap = termMap;
}
/**
* Sets the list of terms, ordered by index.
*
* @return
* The list of terms, ordered by index.
*/
protected List getTermList()
{
return this.termList;
}
/**
* Gets the list of terms, ordered by index.
*
* @param termList
* The list of terms, ordered by index.
*/
protected void setTermList(
final List termList)
{
this.termList = termList;
}
}