gnu.trove.iterator.TObjectIntIterator Java Examples

The following examples show how to use gnu.trove.iterator.TObjectIntIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractAttributeClustering.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
private void buildAttributeModels(int datasetId, List<EntityProfile> profiles) {
    attrNameIndex = new TObjectIntHashMap<>();
    profiles.forEach((profile) -> {
        profile.getAttributes().forEach((attribute) -> {
            attrNameIndex.putIfAbsent(attribute.getName(), attrNameIndex.size() + 1);
        });
    });

    int currentAttributes = attrNameIndex.size();
    attributeModels[datasetId] = new ITextModel[currentAttributes];
    final TObjectIntIterator<String> it = attrNameIndex.iterator();
    while (it.hasNext()) {
        it.advance();
        attributeModels[datasetId][it.value() - 1] = RepresentationModel.getModel(datasetId, repModel, simMetric, it.key());
    }

    profiles.forEach((profile) -> {
        profile.getAttributes().forEach((attribute) -> {
            updateModel(datasetId, attribute);
        });
    });

    for (int i = 0; i < currentAttributes; i++) {
        attributeModels[datasetId][i].finalizeModel();
    }
}
 
Example #2
Source File: TokenNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getSigmaSimilarity(TokenNGramsWithGlobalWeights oModel) {
    float totalTerms2 = oModel.getNoOfTotalTerms();
    final TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        int frequency2 = itemVector2.get(iterator.key());
        if (0 < frequency2) {
            numerator += iterator.value() / noOfTotalTerms * getIdfWeight(iterator.key())
                       + frequency2 / totalTerms2 * oModel.getIdfWeight(iterator.key());
        }
    }

    final Set<String> allKeys = new HashSet<>(itemsFrequency.keySet());
    allKeys.addAll(itemVector2.keySet());
    float denominator = 0.0f;
    denominator = allKeys.stream().map((key) -> itemsFrequency.get(key) / noOfTotalTerms  * getIdfWeight(key) + 
            itemVector2.get(key) / totalTerms2 * oModel.getIdfWeight(key)).reduce(denominator, (accumulator, _item) -> accumulator + _item);

    return (float)(numerator / denominator);
}
 
Example #3
Source File: TokenNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getTfIdfCosineSimilarity(TokenNGramsWithGlobalWeights oModel) {
    float totalTerms2 = oModel.getNoOfTotalTerms();
    final TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        int frequency2 = itemVector2.get(iterator.key());
        if (0 < frequency2) {
            numerator += (iterator.value() / noOfTotalTerms) * getIdfWeight(iterator.key())
                       * (frequency2 / totalTerms2) * oModel.getIdfWeight(iterator.key());
        }
    }

    float denominator = getVectorMagnitude() * oModel.getVectorMagnitude();
    return (float)(numerator / denominator);
}
 
Example #4
Source File: CharacterNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getTfIdfGeneralizedJaccardSimilarity(CharacterNGramsWithGlobalWeights oModel) {
    float totalTerms2 = oModel.getNoOfTotalTerms();
    final TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        int frequency2 = itemVector2.get(iterator.key());
        if (0 < frequency2) {
            numerator += Math.min(iterator.value() / noOfTotalTerms * getIdfWeight(iterator.key()),
                                  frequency2 / totalTerms2 * oModel.getIdfWeight(iterator.key()));
        }
    }

    final Set<String> allKeys = new HashSet<>(itemsFrequency.keySet());
    allKeys.addAll(itemVector2.keySet());
    float denominator = 0.0f;
    denominator = allKeys.stream().map((key) -> Math.max(itemsFrequency.get(key) / noOfTotalTerms  * getIdfWeight(key),
            itemVector2.get(key) / totalTerms2 * oModel.getIdfWeight(key))).reduce(denominator, (accumulator, _item) -> accumulator + _item);

    return (float)(numerator / denominator);
}
 
Example #5
Source File: CharacterNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getTfIdfCosineSimilarity(CharacterNGramsWithGlobalWeights oModel) {
    float totalTerms2 = oModel.getNoOfTotalTerms();
    final TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        int frequency2 = itemVector2.get(iterator.key());
        if (0 < frequency2) {
            numerator += (iterator.value() / noOfTotalTerms) * getIdfWeight(iterator.key())
                       * (frequency2 / totalTerms2) * oModel.getIdfWeight(iterator.key());
        }
    }

    float denominator = getVectorMagnitude() * oModel.getVectorMagnitude();
    return (float)(numerator / denominator);
}
 
Example #6
Source File: CharacterNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getSigmaSimilarity(CharacterNGramsWithGlobalWeights oModel) {
    float totalTerms2 = oModel.getNoOfTotalTerms();
    final TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        int frequency2 = itemVector2.get(iterator.key());
        if (0 < frequency2) {
            numerator += iterator.value() / noOfTotalTerms * getIdfWeight(iterator.key())
                       + frequency2 / totalTerms2 * oModel.getIdfWeight(iterator.key());
        }
    }

    final Set<String> allKeys = new HashSet<>(itemsFrequency.keySet());
    allKeys.addAll(itemVector2.keySet());
    float denominator = 0.0f;
    denominator = allKeys.stream().map((key) -> itemsFrequency.get(key) / noOfTotalTerms  * getIdfWeight(key) + 
            itemVector2.get(key) / totalTerms2 * oModel.getIdfWeight(key)).reduce(denominator, (accumulator, _item) -> accumulator + _item);

    return (float)(numerator / denominator);
}
 
Example #7
Source File: TokenNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getTfIdfGeneralizedJaccardSimilarity(TokenNGramsWithGlobalWeights oModel) {
    float totalTerms2 = oModel.getNoOfTotalTerms();
    final TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        int frequency2 = itemVector2.get(iterator.key());
        if (0 < frequency2) {
            numerator += Math.min(iterator.value() / noOfTotalTerms * getIdfWeight(iterator.key()),
                                  frequency2 / totalTerms2 * oModel.getIdfWeight(iterator.key()));
        }
    }

    final Set<String> allKeys = new HashSet<>(itemsFrequency.keySet());
    allKeys.addAll(itemVector2.keySet());
    float denominator = 0.0f;
    denominator = allKeys.stream().map((key) -> Math.max(itemsFrequency.get(key) / noOfTotalTerms  * getIdfWeight(key),
            itemVector2.get(key) / totalTerms2 * oModel.getIdfWeight(key))).reduce(denominator, (accumulator, _item) -> accumulator + _item);

    return (float)(numerator / denominator);
}
 
Example #8
Source File: BagModel.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getTfCosineSimilarity(BagModel oModel) {
    float totalTerms2 = oModel.getNoOfTotalTerms();

    TObjectIntMap<String> itemVector1 = itemsFrequency;
    TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();
    if (itemVector2.size() < itemVector1.size()) {
        itemVector1 = oModel.getItemsFrequency();
        itemVector2 = itemsFrequency;
    }

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemVector1.iterator(); iterator.hasNext();) {
        iterator.advance();
        numerator += iterator.value() * itemVector2.get(iterator.key()) / noOfTotalTerms / totalTerms2;
    }

    float denominator = getVectorMagnitude() * oModel.getVectorMagnitude();
    return (float)(numerator / denominator);
}
 
Example #9
Source File: BagModel.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
@Override
public float getEntropy(boolean normalized) {
    float entropy = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        float p_i = (iterator.value() / noOfTotalTerms);
        entropy -= (p_i * (Math.log10(p_i) / Math.log10(2.0d)));
    }
    
    if (normalized) {
        float maxEntropy = (float) Math.log10(noOfTotalTerms) / (float) Math.log10(2.0f);
        return entropy / maxEntropy;
    } 
        
    return entropy;
}
 
Example #10
Source File: BagModel.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected float getEnhancedJaccardSimilarity(BagModel oModel) {
    TObjectIntMap<String> itemVector1 = itemsFrequency;
    TObjectIntMap<String> itemVector2 = oModel.getItemsFrequency();
    if (itemVector2.size() < itemVector1.size()) {
        itemVector1 = oModel.getItemsFrequency();
        itemVector2 = itemsFrequency;
    }

    float numerator = 0.0f;
    for (TObjectIntIterator<String> iterator = itemVector1.iterator(); iterator.hasNext();) {
        iterator.advance();
        numerator += Math.min(iterator.value(), itemVector2.get(iterator.key()));
    }

    float denominator = noOfTotalTerms + oModel.getNoOfTotalTerms() - numerator;
    return numerator / (float)denominator;
}
 
Example #11
Source File: Term.java    From ontopia with Apache License 2.0 6 votes vote down vote up
/**
 * PUBLIC: Returns the preferred variant of the term. This is a form
 * of the term which actually occurred in the classified content.
 */
public String getPreferredName() {
  if (variants.isEmpty())
    return getStem();
  Variant maxKey = null;
  int maxValue = -1;
  TObjectIntIterator<Variant> iter = variants.iterator();
  while (iter.hasNext()) {
    iter.advance();
    int thisValue = iter.value();
    Variant thisKey = iter.key();
    // select variant with most occurrences, or lowest lexical value if equal for predictability
    if ((thisValue > maxValue) ||
        ((thisValue == maxValue) && (thisKey.getValue().compareTo(maxKey.getValue()) < 0))) {
      maxValue = thisValue;
      maxKey = thisKey;
    }
  }
  return maxKey.getValue();
}
 
Example #12
Source File: Term.java    From ontopia with Apache License 2.0 6 votes vote down vote up
protected void merge(Term other) {
  if (other == this) return;
  
  this.score = this.score + other.score;    
  this.totalOccurrences = this.totalOccurrences + other.totalOccurrences;

  TObjectIntIterator<Variant> iter = other.variants.iterator();
  while (iter.hasNext()) {
    iter.advance();
    Variant key = iter.key();
    int value = iter.value();
    if (this.variants.containsKey(key))
      this.variants.adjustValue(key, value);
    else
      this.variants.put(key, value);
    key.replaceTerm(this);
  }
}
 
Example #13
Source File: DataAccess.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static Entities getAidaEntitiesForKBEntities(Set<KBIdentifiedEntity> entities) throws EntityLinkingDataAccessException {
  TObjectIntHashMap<KBIdentifiedEntity> kbEntities = DataAccess.getInternalIdsForKBEntities(entities);
  Entities aidaEntities = new Entities();
  for (TObjectIntIterator<KBIdentifiedEntity> itr = kbEntities.iterator(); itr.hasNext(); ) {
    itr.advance();
    aidaEntities.add(new Entity(itr.key(), itr.value()));
  }
  return aidaEntities;
}
 
Example #14
Source File: TokenNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
@Override
protected float getVectorMagnitude() {
    float magnitude = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        magnitude += Math.pow(iterator.value() * getIdfWeight(iterator.key()) / noOfTotalTerms, 2.0);
    }

    return (float) Math.sqrt(magnitude);
}
 
Example #15
Source File: Train.java    From Signals with GNU General Public License v3.0 5 votes vote down vote up
public boolean updatePositions(NetworkState<TPos> state){
    if(!railLinkHolds.isEmpty()) {
        TObjectIntIterator<TPos> iterator = railLinkHolds.iterator();
        while(iterator.hasNext()) {
            iterator.advance();
            if(iterator.value() == 1) {
                iterator.remove();
            } else {
                iterator.setValue(iterator.value() - 1);
            }
        }
    }
    return false;
}
 
Example #16
Source File: LabelAlphabet.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 得到索引集合
 * @return
 */
public Set<Integer> getIndexSet() {
	Set<Integer> set = new HashSet<Integer>();
	for (TObjectIntIterator<String> it = data.iterator(); it.hasNext();) {
		it.advance();
		set.add(it.value());
	}
	return set;
}
 
Example #17
Source File: LabelAlphabet.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Map<String,Integer> toMap() {
	HashMap<String,Integer> map = new HashMap<String,Integer>();
	for (TObjectIntIterator<String> it = data.iterator(); it.hasNext();) {
		it.advance();
		map.put(it.key(), it.value());
	}
	return map;
}
 
Example #18
Source File: LabelAlphabet.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 得到标签集合
 */
public Set<String> toSet() {
	Set<String> set = new HashSet<String>();
	for (TObjectIntIterator<String> it = data.iterator(); it.hasNext();) {
		it.advance();
		set.add(it.key());
	}
	return set;
}
 
Example #19
Source File: LabelAlphabet.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 将标签集合输出为字符串
 */
public String toString() {
	StringBuffer sb = new StringBuffer();
	for (TObjectIntIterator<String> it = data.iterator(); it.hasNext();) {
		it.advance();
		sb.append(it.key());
		sb.append(",");
	}
	return sb.toString();
}
 
Example #20
Source File: CharacterNGramsWithGlobalWeights.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
@Override
protected float getVectorMagnitude() {
    float magnitude = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        magnitude += Math.pow(iterator.value() * getIdfWeight(iterator.key()) / noOfTotalTerms, 2.0);
    }

    return (float) Math.sqrt(magnitude);
}
 
Example #21
Source File: BagModel.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
protected float getVectorMagnitude() {
    float magnitude = 0.0f;
    for (TObjectIntIterator<String> iterator = itemsFrequency.iterator(); iterator.hasNext();) {
        iterator.advance();
        magnitude += Math.pow(iterator.value() / noOfTotalTerms, 2.0);
    }

    return (float) Math.sqrt(magnitude);
}
 
Example #22
Source File: FileUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static <T> void writeTObjectIntMapToFile(File file, TObjectIntHashMap<T> map) throws IOException {
  BufferedWriter writer = getBufferedUTF8Writer(file);
  for (TObjectIntIterator<T> itr = map.iterator(); itr.hasNext(); ) {
    itr.advance();
    writer.write(itr.key() + "\t" + itr.value());
    writer.newLine();
  }
  writer.flush();
  writer.close();
}
 
Example #23
Source File: UnitMeasureTracer.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static TIntObjectHashMap<String> getAllWordIds() throws EntityLinkingDataAccessException {
  TObjectIntHashMap<String> wordIds = DataAccess.getAllWordIds();
  TIntObjectHashMap<String> idWords = new TIntObjectHashMap<>(wordIds.size());
  for (TObjectIntIterator<String> itr = wordIds.iterator(); itr.hasNext(); ) {
    itr.advance();
    idWords.put(itr.value(), itr.key());
  }
  return idWords;
}
 
Example #24
Source File: KeytermEntityEntityMeasureTracer.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static TIntObjectHashMap<String> getAllWordIds() throws EntityLinkingDataAccessException {
  TObjectIntHashMap<String> wordIds = DataAccess.getAllWordIds();
  TIntObjectHashMap<String> idWords = new TIntObjectHashMap<String>(wordIds.size());
  for (TObjectIntIterator<String> itr = wordIds.iterator(); itr.hasNext(); ) {
    itr.advance();
    idWords.put(itr.value(), itr.key());
  }
  return idWords;
}
 
Example #25
Source File: KeyphrasesMeasureTracer.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static TIntObjectHashMap<String> getAllWordIds() throws EntityLinkingDataAccessException {
  TObjectIntHashMap<String> wordIds = DataAccess.getAllWordIds();
  TIntObjectHashMap<String> idWords = new TIntObjectHashMap<String>(wordIds.size());
  for (TObjectIntIterator<String> itr = wordIds.iterator(); itr.hasNext(); ) {
    itr.advance();
    idWords.put(itr.value(), itr.key());
  }
  return idWords;
}
 
Example #26
Source File: DataAccessSQL.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override
public Entities getAllEntities(boolean isNamedEntity) throws EntityLinkingDataAccessException {
  TObjectIntHashMap<KBIdentifiedEntity> entityIds = getAllEntityIds(isNamedEntity);
  Entities entities = new Entities();
  for (TObjectIntIterator<KBIdentifiedEntity> itr = entityIds.iterator();
      itr.hasNext(); ) {
    itr.advance();
    entities.add(new Entity(itr.key(), itr.value()));
  }
  return entities;
}
 
Example #27
Source File: DataAccessSQL.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override public Entities getAllEntities() throws EntityLinkingDataAccessException {
  TObjectIntHashMap<KBIdentifiedEntity> entityIds = getAllEntityIds();
  Entities entities = new Entities();
  for (TObjectIntIterator<KBIdentifiedEntity> itr = entityIds.iterator(); itr.hasNext(); ) {
    itr.advance();
    entities.add(new Entity(itr.key(), itr.value()));
  }
  return entities;
}
 
Example #28
Source File: DataAccess.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static Map<KBIdentifiedEntity, EntityMetaData> getEntitiesMetaData(Set<KBIdentifiedEntity> entities)
    throws EntityLinkingDataAccessException {
  TObjectIntHashMap<KBIdentifiedEntity> ids = getInternalIdsForKBEntities(entities);
  TIntObjectHashMap<EntityMetaData> metadata = getEntitiesMetaData(ids.values());
  Map<KBIdentifiedEntity, EntityMetaData> result = new HashMap<KBIdentifiedEntity, EntityMetaData>();
  for (TObjectIntIterator<KBIdentifiedEntity> itr = ids.iterator(); itr.hasNext(); ) {
    itr.advance();
    int id = itr.value();
    result.put(itr.key(), metadata.get(id));
  }
  return result;
}
 
Example #29
Source File: EntityLinkingManager.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static Entities getEntities(Set<KBIdentifiedEntity> kbEntities) throws EntityLinkingDataAccessException {
  TObjectIntHashMap<KBIdentifiedEntity> ids = DataAccess.getInternalIdsForKBEntities(kbEntities);
  Entities entities = new Entities();
  for (TObjectIntIterator<KBIdentifiedEntity> itr = ids.iterator(); itr.hasNext(); ) {
    itr.advance();
    entities.add(new Entity(itr.key(), itr.value()));
  }
  return entities;
}
 
Example #30
Source File: StringFeatureAlphabet.java    From fnlp with GNU Lesser General Public License v3.0 4 votes vote down vote up
public TObjectIntIterator<String> iterator()	{
	return data.iterator();
}