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

edu.ucla.sspace.basis.AbstractBasisMapping Maven / Gradle / Ivy

Go to download

The S-Space Package is a collection of algorithms for building Semantic Spaces as well as a highly-scalable library for designing new distributional semantics algorithms. Distributional algorithms process text corpora and represent the semantic for words as high dimensional feature vectors. This package also includes matrices, vectors, and numerous clustering algorithms. These approaches are known by many names, such as word spaces, semantic spaces, or distributed semantics and rest upon the Distributional Hypothesis: words that appear in similar contexts have similar meanings.

The newest version!
/*
 * Copyright 2010 Keith Stevens 
 *
 * This file is part of the S-Space package and is covered under the terms and
 * conditions therein.
 *
 * The S-Space package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation and distributed hereunder to you.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND NO REPRESENTATIONS OR WARRANTIES,
 * EXPRESS OR IMPLIED ARE MADE.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, WE MAKE
 * NO REPRESENTATIONS OR WARRANTIES OF MERCHANT- ABILITY OR FITNESS FOR ANY
 * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE OR DOCUMENTATION
 * WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER
 * RIGHTS.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see .
 */

package edu.ucla.sspace.basis;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * An abstract base class for {@link BasisMapping}s that implements most of the
 * required functionality.  For any requested feature, the currently mapped
 * dimension is returned upon request, or a new dimension is generated if the
 * {@link BasisMapping} is not in read only mode.
 *
 * 

* * All access to this {@link BasisMapping} is thread safe. * * @author Keith Stevens */ public abstract class AbstractBasisMapping implements BasisMapping, Serializable { private static final long serialVersionUID = 1L; /** * The mapping from keys to dimension indices. */ private Map mapping; /** * A cache of the reverse {@code mapping} mapping. This field is only * updated on calls to {@link #getDimensionDescription(int)} when the * mapping has chanaged since the previous call. */ transient private List indexToKeyCache; /** * Set to {@code true} when the {@link BasisMapping} should not create new * dimensions for unseen keys. */ private boolean readOnly; /** * Creates a new {@link AbstractBasisMapping}. */ public AbstractBasisMapping() { mapping = new HashMap(); readOnly = false; indexToKeyCache = new ArrayList(); } /** * {@inheritDoc} */ public K getDimensionDescription(int dimension) { if (dimension < 0 || dimension > mapping.size()) throw new IllegalArgumentException( "invalid dimension: " + dimension); // If the basis mapping was de-serialized, the mapping will be null, so // re-set it. if (indexToKeyCache == null) indexToKeyCache = new ArrayList(); // If the cache is out of date, rebuild the reverse mapping. if (mapping.size() > indexToKeyCache.size()) { // Lock to ensure safe iteration synchronized(this) { indexToKeyCache = new ArrayList(mapping.size()); for (int i = 0; i < mapping.size(); ++i) indexToKeyCache.add(null); for (Map.Entry e : mapping.entrySet()) indexToKeyCache.set(e.getValue(), e.getKey()); } } return indexToKeyCache.get(dimension); } /** * {@inheritDoc} */ public Set keySet() { return mapping.keySet(); } /** * Returns an integer corresponding to {@code key}. If in read only mode, * -1 is returned for unseen keys. Otherwise, unseen keys are assigned a * new dimension. */ protected int getDimensionInternal(K key) { Integer index = mapping.get(key); if (readOnly) return (index == null) ? -1: index; if (index == null) { synchronized(this) { // Recheck to see if the key was added while blocking. index = mapping.get(key); // If another thread has not already added this key while the // current thread was blocking waiting on the lock, then add it. if (index == null) { int i = mapping.size(); mapping.put(key, i); return i; // avoid the auto-boxing to assign i to index } } } return index; } /** * Returns the internal mapping from keys to indices. */ protected Map getMapping() { return mapping; } /** * {@inheritDoc} */ public int numDimensions() { return mapping.size(); } /** * {@inheritDoc} */ public void setReadOnly(boolean readOnly) { this.readOnly = readOnly; } /** * {@inheritDoc} */ public boolean isReadOnly() { return readOnly; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy