/** * This function gets the feature from the lexicon using the id. Use this function if you use * either getFeatureId or convert to avoid off-by-one errors. */ public static edu.illinois.cs.cogcomp.lbjava.classify.Feature getFeature(Lexicon lexicon, int id) { return lexicon.lookupKey(id - 1); }
/** * This function gets the feature from the lexicon using the id. Use this function if you use * either getFeatureId or convert to avoid off-by-one errors. */ public static edu.illinois.cs.cogcomp.lbjava.classify.Feature getFeature(Lexicon lexicon, int id) { return lexicon.lookupKey(id - 1); }
public Set<String> getTagValues() { Lexicon labelLexicon = taggerKnown.getLabelLexicon(); Set<String> tagSet = new HashSet(); for (int i =0; i < labelLexicon.size(); ++i) { tagSet.add(labelLexicon.lookupKey(i).getStringValue()); } return tagSet; }
public Set<String> getTagValues() { Lexicon labelLexicon = taggerKnown.getLabelLexicon(); Set<String> tagSet = new HashSet(); for (int i =0; i < labelLexicon.size(); ++i) { tagSet.add(labelLexicon.lookupKey(i).getStringValue()); } return tagSet; }
/** * Produces a set of scores indicating the degree to which each possible discrete classification * value is associated with the given example object. * * @param exampleFeatures The example's array of feature indices. * @param exampleValues The example's array of feature values. * @return The accumulated alpha values of weak learners that predicted the associated * classification value. **/ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { double[] scores = sumAlphas(exampleFeatures, exampleValues); String[] values = new String[] {labelLexicon.lookupKey(0).getStringValue(), labelLexicon.lookupKey(1).getStringValue()}; return new ScoreSet(values, scores); }
/** * Writes the algorithm's internal representation as text. * * @param out The output stream. **/ public void write(PrintStream out) { int N = network.size(); for (int i = 0; i < N; ++i) { out.println("label: " + labelLexicon.lookupKey(i).getStringValue()); ((NaiveBayesVector) network.get(i)).write(out); } out.println("End of NaiveBayes"); }
/** * Sets the value of {@link #parentLexicon} and makes sure that any features marked for removal * in this lexicon are the identical objects also present in the parent. This is useful in * particular just after lexicons have been read from disk. * * @param p The new parent lexicon. **/ public void setParent(Lexicon p) { parentLexicon = p; int N = lexiconInv.size(); for (int i = 0; i < N; ++i) { Feature f = lexiconInv.get(i); if (f != null && parents.get(i) < 0) { Feature pf = p.lookupKey(p.lookup(f)); if (pf == null) { System.err.println("LBJava ERROR: Can't find feature " + f + " in parent lexicon."); new Exception().printStackTrace(); System.exit(1); } lexiconInv.set(i, pf); if (lexicon != null) lexicon.put(pf, lexicon.remove(f)); } } }
/** * Reads the representation of a feature with this object's run-time type as stored by a * lexicon, overwriting the data in this object. * * <p> * This method is appropriate for reading features as written by * {@link #lexWrite(ExceptionlessOutputStream,Lexicon,String,String,String,String,ByteString)}. * * @param in The input stream. * @param lex The lexicon we are reading in to. * @param p The assumed package string. If no package name is given in the input stream, the * instantiated feature is given this package. * @param g The assumed classifier name string. If no classifier name is given in the input * stream, the instantiated feature is given this classifier name. * @param si The assumed identifier as a string. If the feature being read has a string * identifier field and no identifier is given in the input stream, the feature is given * this identifier. * @param bi The assumed identifier as a byte string. If the feature being read has a byte * string identifier field and no identifier is given in the input stream, the feature is * given this identifier. **/ public void lexRead(ExceptionlessInputStream in, Lexicon lex, String p, String g, String si, ByteString bi) { super.lexRead(in, lex, p, g, si, bi); referent = (DiscreteFeature) lex.lookupKey(in.readInt()); } }
/** * Writes the algorithm's internal representation as text. * * @param out The output stream. **/ public void write(PrintStream out) { out.println(baseLTU.getClass().getName()); baseLTU.write(out); int N = network.size(); for (int i = 0; i < N; ++i) { LinearThresholdUnit ltu = (LinearThresholdUnit) network.get(i); if (ltu == null) continue; out.println("label: " + labelLexicon.lookupKey(i).getStringValue()); ltu.setLexicon(lexicon); ltu.write(out); ltu.setLexicon(null); } out.println("End of SparseNetworkLearner"); out.close(); }
/** * Return possible tag values that the NERAnnotator can produce. * * @return the set of string representing the tag values */ @Override public Set<String> getTagValues() { if (!isInitialized()) { doInitialize(); } Lexicon labelLexicon = this.params.taggerLevel1.getLabelLexicon(); Set<String> tagSet = new HashSet<String>(); for (int i =0; i < labelLexicon.size(); ++i) { tagSet.add(labelLexicon.lookupKey(i).getStringValue()); } return tagSet; } }
/** * Return possible tag values that the ChunkerAnnotator can produce. * * @return the set of string representing the tag values */ @Override public Set<String> getTagValues() { if (!isInitialized()) { doInitialize(); } Lexicon labelLexicon = tagger.getLabelLexicon(); Set<String> tagSet = new HashSet(); for (int i =0; i < labelLexicon.size(); ++i) { tagSet.add(labelLexicon.lookupKey(i).getStringValue()); } return tagSet; }
/** * Return possible tag values that the ChunkerAnnotator can produce. * * @return the set of string representing the tag values */ @Override public Set<String> getTagValues() { if (!isInitialized()) { doInitialize(); } Lexicon labelLexicon = tagger.getLabelLexicon(); Set<String> tagSet = new HashSet(); for (int i =0; i < labelLexicon.size(); ++i) { tagSet.add(labelLexicon.lookupKey(i).getStringValue()); } return tagSet; }
/** * Return possible tag values that the NERAnnotator can produce. * * @return the set of string representing the tag values */ @Override public Set<String> getTagValues() { if (!isInitialized()) { doInitialize(); } Lexicon labelLexicon = this.params.taggerLevel1.getLabelLexicon(); Set<String> tagSet = new HashSet<String>(); for (int i =0; i < labelLexicon.size(); ++i) { tagSet.add(labelLexicon.lookupKey(i).getStringValue()); } return tagSet; } }
/** * Instantiates a feature vector from example arrays and lexicons. * * @param ex The example array. * @param lex The feature lexicon. * @param llex The label lexicon. **/ public FeatureVector(Object[] ex, Lexicon lex, Lexicon llex) { this(); int[] fs = (int[]) ex[0]; double[] vs = (double[]) ex[1]; for (int i = 0; i < fs.length; ++i) { Feature f = lex.lookupKey(fs[i]); Feature ff = f.withStrength(vs[i]); addFeature(ff == null ? f : ff); } if (ex.length > 2) { int[] ls = (int[]) ex[2]; double[] lvs = (double[]) ex[3]; for (int i = 0; i < ls.length; ++i) { Feature f = llex.lookupKey(ls[i]); if (!f.isDiscrete()) f = f.withStrength(lvs[i]); addLabel(f); } } }
/** * Return possible tag values that the NERAnnotator can produce. * * @return the set of string representing the tag values */ @Override public Set<String> getTagValues() { if (!isInitialized()) { doInitialize(); } Lexicon labelLexicon = t1.getLabelLexicon(); Set<String> tagSet = new HashSet(); for (int i =0; i < labelLexicon.size(); ++i) { tagSet.add(labelLexicon.lookupKey(i).getStringValue()); } return tagSet; } }
/** * If it hasn't been created already, this method will create the prediction feature in * {@link #predictions} associated with the label feature at the given index of <code>lex</code> * . This method does not create {@link RealFeature}s in {@link #predictions} since their * strengths cannot be modified. In association with {@link DiscreteFeature}s it creates a * {@link DiscretePrimitiveStringFeature} with an empty identifier. Its <code>value</code>, * <code>valueIndex</code>, and <code>totalValues</code> fields are filled by calling the label * feature's {@link Feature#getStringValue() getStringValue()}, {@link Feature#getValueIndex() * getValueIndex()}, and {@link Feature#totalValues() totalValues()} methods respectively. * * @param lex The label lexicon to associate prediction features with. * @param index The index of a label feature in <code>lex</code>. **/ protected void createPrediction(Lexicon lex, int index) { if (predictions.get(index) != null || !getOutputType().equals("discrete")) return; Feature label = lex.lookupKey(index); predictions.set(index, new DiscretePrimitiveStringFeature(containingPackage, name, "", label.getStringValue(), label.getValueIndex(), label.totalValues())); }
/** * Produces a set of scores indicating the degree to which each possible discrete classification * value is associated with the given example object. These scores are just the dot product of * each weight vector with the example vector. * * @param exampleFeatures The example's array of feature indices. * @param exampleValues The example's array of feature values. **/ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { ScoreSet result = new ScoreSet(); int N = network.size(); for (int l = 0; l < N; l++) { double score = ((BiasedRandomWeightVector) network.get(l)).dot(exampleFeatures, exampleValues); result.put(labelLexicon.lookupKey(l).getStringValue(), score); } return result; }
/** * Update the score of each binary variable (label) based on the gold value of each example for * that variable. When using a {@code SparseNetworkLearner} to keep the model there is an LTU * for each label. If the gold is same as a specific label then its binary value for that label * is 1 and the score for that label will be {@code oldScore - lossOffset}; otherwise it will be * 0 and the score will be {@code oldScore + lossOffset}. * * @param example The object to make decisions about. * @param resultS The original scores (see {@link #scores(Object)}). * @return The augmented set of scores. */ public ScoreSet scoresAugmented(Object example, ScoreSet resultS) { ScoreSet augmentedScores = new ScoreSet(); Lexicon lLexicon = getLabelLexicon(); String gold = getLabeler().discreteValue(example); for (int i = 0; i < lLexicon.size(); i++) { String candidate = lLexicon.lookupKey(i).getStringValue(); double originalScore = resultS.getScore(candidate).score; double lossOffset = 1 / (double) (candidates); if (candidate.equals(gold)) augmentedScores.put(candidate, originalScore - lossOffset); else augmentedScores.put(candidate, originalScore + lossOffset); } return augmentedScores; }
/** * Produces a set of scores indicating the degree to which each possible discrete classification * value is associated with the given example object. These scores are just the scores of each * LTU's positive classification as produced by <code>LinearThresholdUnit.scores(Object)</code>. * * @see LinearThresholdUnit#scores(Object) * @param exampleFeatures The example's array of feature indices. * @param exampleValues The example's array of feature values. * @return The set of scores produced by the LTUs **/ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { ScoreSet result = new ScoreSet(); int N = network.size(); for (int l = 0; l < N; l++) { LinearThresholdUnit ltu = (LinearThresholdUnit) network.get(l); if (ltu == null) continue; result.put(labelLexicon.lookupKey(l).getStringValue(), ltu.score(exampleFeatures, exampleValues) - ltu.getThreshold()); } return result; }