Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package edu.stanford.nlp.stats;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import edu.stanford.nlp.util.ErasureUtils;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.MutableDouble;
/**
* A class for keeping double counts of {@link List}s of a
* prespecified length. A depth n GeneralizedCounter can be
* thought of as a conditionalized count over n classes of
* objects, in a prespecified order. Also offers a read-only view as
* a Counter.
*
* This class is serializable but no guarantees are
* made about compatibility version to version.
*
*
* This is the worst class. Use TwoDimensionalCounter. If you need a third,
* write ThreeDimensionalCounter, but don't use this.
*
* @author Roger Levy
*/
public class GeneralizedCounter implements Serializable {
private static final long serialVersionUID = 1;
private static final Object[] zeroKey = new Object[0];
private Map map = Generics.newHashMap();
private int depth;
private double total;
/**
* GeneralizedCounter must be constructed with a depth parameter
*/
private GeneralizedCounter() {
}
/**
* Constructs a new GeneralizedCounter of a specified depth
*
* @param depth the depth of the GeneralizedCounter
*/
public GeneralizedCounter(int depth) {
this.depth = depth;
}
/**
* Returns the set of entries in the GeneralizedCounter.
* Here, each key is a read-only {@link
* List} of size equal to the depth of the GeneralizedCounter, and
* each value is a {@link Double}. Each entry is a {@link java.util.Map.Entry} object,
* but these objects
* do not support the {@link java.util.Map.Entry#setValue} method; attempts to call
* that method with result
* in an {@link UnsupportedOperationException} being thrown.
*/
public Set,Double>> entrySet() {
return ErasureUtils.,Double>>>uncheckedCast(entrySet(new HashSet<>(), zeroKey, true));
}
/* this is (non-tail) recursive right now, haven't figured out a way
* to speed it up */
private Set> entrySet(Set> s, Object[] key, boolean useLists) {
if (depth == 1) {
//System.out.println("key is long enough to add to set");
Set keys = map.keySet();
for (K finalKey: keys) {
// array doesn't escape
K[] newKey = ErasureUtils.mkTArray(Object.class,key.length + 1);
if (key.length > 0) {
System.arraycopy(key, 0, newKey, 0, key.length);
}
newKey[key.length] = finalKey;
MutableDouble value = (MutableDouble) map.get(finalKey);
Double value1 = new Double(value.doubleValue());
if (useLists) {
s.add(new Entry<>(Arrays.asList(newKey), value1));
} else {
s.add(new Entry<>(newKey[0], value1));
}
}
} else {
Set keys = map.keySet();
//System.out.println("key length " + key.length);
//System.out.println("keyset level " + depth + " " + keys);
for (K o: keys) {
Object[] newKey = new Object[key.length + 1];
if (key.length > 0) {
System.arraycopy(key, 0, newKey, 0, key.length);
}
newKey[key.length] = o;
//System.out.println("level " + key.length + " current key " + Arrays.asList(newKey));
conditionalizeHelper(o).entrySet(s, newKey, true);
}
}
//System.out.println("leaving key length " + key.length);
return s;
}
/**
* Returns a set of entries, where each key is a read-only {@link
* List} of size one less than the depth of the GeneralizedCounter, and
* each value is a {@link ClassicCounter}. Each entry is a {@link java.util.Map.Entry} object, but these objects
* do not support the {@link java.util.Map.Entry#setValue} method; attempts to call that method with result
* in an {@link UnsupportedOperationException} being thrown.
*/
public Set, ClassicCounter>> lowestLevelCounterEntrySet() {
return ErasureUtils., ClassicCounter>>>uncheckedCast(lowestLevelCounterEntrySet(new HashSet<>(), zeroKey, true));
}
/* this is (non-tail) recursive right now, haven't figured out a way
* to speed it up */
private Set>> lowestLevelCounterEntrySet(Set>> s, Object[] key, boolean useLists) {
Set keys = map.keySet();
if (depth == 2) {
// add these counters to set
for (K finalKey: keys) {
K[] newKey = ErasureUtils.mkTArray(Object.class,key.length + 1);
if (key.length > 0) {
System.arraycopy(key, 0, newKey, 0, key.length);
}
newKey[key.length] = finalKey;
ClassicCounter c = conditionalizeHelper(finalKey).oneDimensionalCounterView();
if (useLists) {
s.add(new Entry<>(Arrays.asList(newKey), c));
} else {
s.add(new Entry<>(newKey[0], c));
}
}
} else {
//System.out.println("key length " + key.length);
//System.out.println("keyset level " + depth + " " + keys);
for (K o: keys) {
Object[] newKey = new Object[key.length + 1];
if (key.length > 0) {
System.arraycopy(key, 0, newKey, 0, key.length);
}
newKey[key.length] = o;
//System.out.println("level " + key.length + " current key " + Arrays.asList(newKey));
conditionalizeHelper(o).lowestLevelCounterEntrySet(s, newKey, true);
}
}
//System.out.println("leaving key length " + key.length);
return s;
}
private static class Entry implements Map.Entry {
private K key;
private V value;
Entry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public V setValue(V value) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Entry)) {
return false;
}
Entry e = ErasureUtils.>uncheckedCast(o);
Object key1 = e.getKey();
if (!(key != null && key.equals(key1))) {
return false;
}
Object value1 = e.getValue();
if (!(value != null && value.equals(value1))) {
return false;
}
return true;
}
@Override
public int hashCode() {
if (key == null || value == null) {
return 0;
}
return key.hashCode() ^ value.hashCode();
}
@Override
public String toString() {
return key.toString() + "=" + value.toString();
}
} // end static class Entry
/**
* returns the total count of objects in the GeneralizedCounter.
*/
public double totalCount() {
if (depth() == 1) {
return total; // I think this one is always OK. Not very principled here, though.
} else {
double result = 0.0;
for (K o: topLevelKeySet()) {
result += conditionalizeOnce(o).totalCount();
}
return result;
}
}
/**
* Returns the set of elements that occur in the 0th position of a
* {@link List} key in the GeneralizedCounter.
*
* @see #conditionalize(List)
* @see #getCount
*/
public Set topLevelKeySet() {
return map.keySet();
}
/**
* Returns the set of keys, as read-only {@link List}s of size
* equal to the depth of the GeneralizedCounter.
*/
public Set> keySet() {
return ErasureUtils.>>uncheckedCast(keySet(Generics.newHashSet(), zeroKey, true));
}
/* this is (non-tail) recursive right now, haven't figured out a way
* to speed it up */
private Set