本文整理汇总了Java中edu.berkeley.nlp.util.Counter类的典型用法代码示例。如果您正苦于以下问题:Java Counter类的具体用法?Java Counter怎么用?Java Counter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Counter类属于edu.berkeley.nlp.util包,在下文中一共展示了Counter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLinearScoreMatrix
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public double[][] getLinearScoreMatrix(InstanceSequence<V, E, L> sequence,
int index, double[] w) {
int numLabels = encoding.getNumLabels();
double[][] M = new double[numLabels][numLabels];
Counter<F> vertexFeatures = vertexExtractor.extractFeatures(sequence
.getVertexInstance(index));
for (int vc = 0; vc < numLabels; vc++) {
double vertexScore = dotProduct(vertexFeatures, vc, w);
for (int vp = 0; vp < numLabels; vp++) {
L previousLabel = encoding.getLabel(vp);
Counter<F> edgeFeatures = edgeExtractor
.extractFeatures(sequence.getEdgeInstance(index,
previousLabel));
double edgeScore = dotProduct(edgeFeatures, vc, w);
M[vp][vc] = vertexScore + edgeScore;
}
}
return M;
}
示例2: getEmpiricalCounts
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public List<Counter<F>> getEmpiricalCounts(
List<? extends LabeledInstanceSequence<V, E, L>> sequences) {
int numLabels = encoding.getNumLabels();
List<Counter<F>> counts = new ArrayList<Counter<F>>(numLabels);
for (int l = 0; l < numLabels; l++) {
counts.add(new Counter<F>());
}
for (LabeledInstanceSequence<V, E, L> s : sequences) {
for (int i = 0; i < s.getSequenceLength(); i++) {
Counter<F> vertexFeatures = vertexExtractor.extractFeatures(s
.getVertexInstance(i));
int goldLabelIndex = encoding.getLabelIndex(s.getGoldLabel(i));
counts.get(goldLabelIndex).incrementAll(vertexFeatures);
if (i > 0) {
Counter<F> edgeFeatures = edgeExtractor.extractFeatures(s
.getEdgeInstance(i, s.getGoldLabel(i - 1)));
counts.get(goldLabelIndex).incrementAll(edgeFeatures);
}
}
}
return counts;
}
示例3: relax
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
private void relax(Counter<Edge<V>> pathCosts,
Map<Edge<V>, V> intermediateStates,
Map<V, List<Edge<V>>> closedUnaryRulesByChild,
Map<V, List<Edge<V>>> closedUnaryRulesByParent, Edge<V> unaryRule,
V intermediateState, double newScore) {
if (intermediateState != null
&& (intermediateState.equals(unaryRule.getParent()) || intermediateState
.equals(unaryRule.getChild())))
return;
boolean isNewRule = !pathCosts.containsKey(unaryRule);
double oldScore = (isNewRule ? Double.NEGATIVE_INFINITY : pathCosts
.getCount(unaryRule));
if (oldScore > newScore)
return;
if (isNewRule) {
CollectionUtils.addToValueList(closedUnaryRulesByChild,
unaryRule.getChild(), unaryRule);
CollectionUtils.addToValueList(closedUnaryRulesByParent,
unaryRule.getParent(), unaryRule);
}
pathCosts.setCount(unaryRule, newScore);
intermediateStates.put(unaryRule, intermediateState);
}
示例4: initPunctuations
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
private void initPunctuations(StateSetTreeList trainTrees) {
punctuationSignatures = new Indexer<String>();
isPunctuation = new boolean[nWords];
Counter<String> punctSigCounter = new Counter<String>();
for (int word = 0; word < nWords; word++) {
isPunctuation[word] = isPunctuation(wordIndexer.get(word));
}
for (Tree<StateSet> tree : trainTrees) {
getPunctuationSignatures(tree.getYield(), true, punctSigCounter);
}
Indexer<String> newPunctuationSignatures = new Indexer<String>();
for (String sig : punctSigCounter.keySet()) {
if (punctSigCounter.getCount(sig) >= minFeatureFrequency)
newPunctuationSignatures.add(sig);
}
punctuationSignatures = newPunctuationSignatures;
punctuationScores = new double[punctuationSignatures.size()][nClasses];
ArrayUtil.fill(punctuationScores, 1);
nFeatures += nClasses * punctuationScores.length;
}
示例5: countRuleParents
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public void countRuleParents() {
for (Tree<StateSet> tree : trees) {
addParent(tree);
}
for (BinaryRule br : binaryRuleCounter.keySet()) {
contexts[br.parentState]++;
contexts[br.leftChildState]++;
contexts[br.rightChildState]++;
}
for (UnaryRule ur : unaryRuleCounter.keySet()) {
contexts[ur.parentState]++;
contexts[ur.childState]++;
}
for (int i = 0; i < contexts.length; i++) {
Counter<String> tempC = posCounter.getCounter(i);
contexts[i] += tempC.size();
}
}
示例6: trainClassifier
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public ProbabilisticClassifier<I, L> trainClassifier(
List<LabeledInstance<I, L>> trainingData) {
CounterMap<L, F> featureProbs = new CounterMap<L, F>();
Counter<F> backoffProbs = new Counter<F>();
Counter<L> labelProbs = new Counter<L>();
for (LabeledInstance<I, L> instance : trainingData) {
L label = instance.getLabel();
labelProbs.incrementCount(label, 1.0);
I inst = instance.getInput();
Counter<F> featCounts = featureExtractor.extractFeatures(inst);
for (F feat : featCounts.keySet()) {
double count = featCounts.getCount(feat);
backoffProbs.incrementCount(feat, count);
featureProbs.incrementCount(label, feat, count);
}
}
featureProbs.normalize();
labelProbs.normalize();
backoffProbs.normalize();
return new NaiveBayesClassifier<I, F, L>(featureProbs,
backoffProbs, labelProbs, featureExtractor);
}
示例7: buildEncoding
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
private Encoding<F, L> buildEncoding(List<LabeledInstance<I, L>> data) {
Indexer<F> featureIndexer = new Indexer<F>();
Indexer<L> labelIndexer = new Indexer<L>();
for (LabeledInstance<I, L> labeledInstance : data) {
L label = labeledInstance.getLabel();
Counter<F> features = featureExtractor
.extractFeatures(labeledInstance.getInput());
LabeledFeatureVector<F, L> labeledDatum = new BasicLabeledFeatureVector<F, L>(
label, features);
labelIndexer.getIndex(labeledDatum.getLabel());
for (F feature : labeledDatum.getFeatures().keySet()) {
featureIndexer.getIndex(feature);
}
}
return new Encoding<F, L>(featureIndexer, labelIndexer);
}
示例8: calculate
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
@Override
protected Pair<Double, double[]> calculate(double[] x) {
weights = x;
double objective = 0.0;
double[] gradient = new double[dimension()];
for (Pair<I, Double> datum : trainingData) {
I input = datum.getFirst();
Counter<Feature> featCounts = getFeatures(input);
double guessResponse = getScore(featCounts);
double goldResponse = datum.getSecond();
double diff = (guessResponse - goldResponse);
objective += 0.5 * diff * diff;
for (Feature feat : featCounts.keySet()) {
double count = featCounts.getCount(feat);
gradient[feat.getIndex()] += count * diff;
}
}
// TODO Auto-generated method stub
return Pair.newPair(objective, gradient);
}
示例9: main
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public static void main(String[] args) {
List<String> elem1 = CollectionUtils.makeList("a", "b", "c");
List<String> elem2 = CollectionUtils.makeList("a", "b");
Pair<List<String>, Double> d1 = Pair.newPair(elem1, 3.0);
Pair<List<String>, Double> d2 = Pair.newPair(elem2, 2.0);
FeatureExtractor<List<String>, String> featExtractor = new FeatureExtractor<List<String>, String>() {
public Counter<String> extractFeatures(List<String> instance) {
Counter<String> counts = new Counter<String>();
for (String elem : instance) {
counts.incrementCount(elem, 1.0);
}
// TODO Auto-generated method stub
return counts;
}
};
LinearRegression.Factory<List<String>> factory = new LinearRegression.Factory<List<String>>(
featExtractor);
List<Pair<List<String>, Double>> datums = CollectionUtils.makeList(d1,
d2);
LinearRegression<List<String>> linearRegressionModel = factory
.train(datums);
double guess = linearRegressionModel.getResponse(elem1);
System.out.println("guess: " + guess);
}
示例10: logAdd
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public static <T> double logAdd(Counter<T> counts) {
double[] arr = new double[counts.size()];
int index = 0;
for (Map.Entry<T, Double> entry : counts.entrySet()) {
arr[index++] = entry.getValue();
}
return SloppyMath.logAdd(arr);
}
示例11: getLinearVertexScores
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public double[] getLinearVertexScores(InstanceSequence<V, E, L> sequence,
int index, double[] w) {
int numLabels = encoding.getNumLabels();
double[] s = new double[numLabels];
Counter<F> vertexFeatures = vertexExtractor.extractFeatures(sequence
.getVertexInstance(index));
for (int vc = 0; vc < numLabels; vc++) {
double vertexScore = dotProduct(vertexFeatures, vc, w);
s[vc] = vertexScore;
}
return s;
}
示例12: dotProduct
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
private double dotProduct(Counter<F> features, int labelIndex, double[] w) {
double val = 0.0;
for (F feature : features.keySet()) {
if (encoding.hasFeature(feature)) {
int featureIndex = encoding.getFeatureIndex(feature);
int linearIndex = il.getLinearIndex(featureIndex, labelIndex);
val += features.getCount(feature) * w[linearIndex];
}
}
return val;
}
示例13: combinePathCosts
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
/**
* @param pathCosts
* @param incomingRule
* @param outgoingRule
* @return
*/
private double combinePathCosts(Counter<Edge<V>> pathCosts,
Edge<V> incomingRule, Edge<V> outgoingRule) {
return this.sumInsteadOfMultipy ? (pathCosts.getCount(incomingRule) + pathCosts
.getCount(outgoingRule))
: (pathCosts.getCount(incomingRule) * pathCosts
.getCount(outgoingRule));
}
示例14: CorpusStatistics
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
/**
* Count statistics for a collection of StateSet trees.
*/
public CorpusStatistics(Numberer tagNumberer,
Collection<Tree<StateSet>> trees) {
counts = new int[tagNumberer.objects().size()];
this.trees = trees;
unaryRuleCounter = new Counter<UnaryRule>();
binaryRuleCounter = new Counter<BinaryRule>();
contexts = new int[tagNumberer.objects().size()];
posCounter = new CounterMap<Integer, String>();
}
示例15: main
import edu.berkeley.nlp.util.Counter; //导入依赖的package包/类
public static void main(String[] args) {
Counter<String> counter = new Counter<String>();
System.out.println(counter);
counter.incrementCount("planets", 7);
System.out.println(counter);
counter.incrementCount("planets", 1);
System.out.println(counter);
counter.setCount("suns", 1);
System.out.println(counter);
counter.setCount("aliens", 0);
System.out.println(counter);
System.out.println(counter.toString(2));
System.out.println("Total: " + counter.totalCount());
}