Java Code Examples for gnu.trove.map.hash.TIntObjectHashMap#get()

The following examples show how to use gnu.trove.map.hash.TIntObjectHashMap#get() . 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: SuperdocEntityKeyphraseCountCollector.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private void writeToFile(TIntObjectHashMap<TIntIntHashMap> superdocKeyphraseCounts) throws IOException, SQLException {
  int eCount = 0;

  logger.info("Writing entity-keyphrase pairs for " + superdocKeyphraseCounts.size() + " entities");

  for (int eid : superdocKeyphraseCounts.keys()) {

    if ((++eCount % 100000) == 0) {
      double percent = (double) eCount / (double) superdocKeyphraseCounts.size();

      logger.info("Wrote " + eCount + " entities (" + new DecimalFormat("#.##").format(percent) + ")");
    }

    TIntIntHashMap keyphraseCounts = superdocKeyphraseCounts.get(eid);

    for (int kid : keyphraseCounts.keys()) {
      int count = keyphraseCounts.get(kid);

      writer.write(eid + "\t" + kid + "\t" + String.valueOf(count));
      writer.newLine();
    }
  }
}
 
Example 2
Source File: BmeowTypeAnnotator.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private static BmeowTypeDictionary getDicitionaryForEntities(Entities entities) throws EntityLinkingDataAccessException {
	logger.trace("Entities retrieved from DB: " + entities.getEntities().stream()
			.map(Entity::getNMEnormalizedName).collect(Collectors.joining(", ")));

	TIntObjectHashMap<List<MentionObject>> mentions = DataAccess.getMentionsForEntities(entities);
	int[] entityIds = mentions.keys();

	TIntObjectHashMap<int[]> typeIds = DataAccess.getTypesIdsForEntitiesIds(entityIds);

	BmeowTypeDictionary dict = new BmeowTypeDictionary();

	for (int i = 0; i < entityIds.length; i++) {
		int[] typeId = typeIds.get(entityIds[i]); //for each entity its own set of type ids
		Set<NerType.Label> types = NerType.getNerTypesForTypeIds(typeId);
		for(MentionObject mention : mentions.get(entityIds[i])){
			for (NerType.Label nerType : types) {
				dict.put(mention.getMention(), nerType);
				logger.trace("Dict entry added: " + mention + ", " + nerType);
			}
		}
	}
	return dict;
}
 
Example 3
Source File: TaskScheduler.java    From scheduler with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * check resource profile on each host
 *
 * @param changes  the resource profiles
 * @param initFree the initial amount of free resources
 * @return the satisfaction status
 */
private ESat checkProfiles(TIntIntHashMap[][] changes, int[][] initFree) {
    boolean ok = true;
    for (int h = 0; h < nbHosts; h++) {
        TIntObjectHashMap<int[]> myChanges = myChanges(changes[h]);
        int[] moments = myChanges.keys(new int[myChanges.size()]);
        Arrays.sort(moments);
        for (int t : moments) {
            boolean bad = false;
            for (int d = 0; d < nbDims; d++) {
                initFree[h][d] += myChanges.get(t)[d];
                if (initFree[h][d] < 0) {
                    bad = true;
                }
            }
            if (bad) {
                ok = false;
                break;
            }
        }
    }
    return ESat.eval(ok);
}
 
Example 4
Source File: GraphTracer.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private String buildEntityUriAnchor(int entityId, TIntObjectHashMap<EntityMetaData> entitiesMetaData) {
  String uriString = "NO_METADATA";
  String displayString = new Integer(entityId).toString();
  if (entitiesMetaData != null && entitiesMetaData.size() > 0) {
    EntityMetaData md = entitiesMetaData.get(entityId);
    if (md != null) {
      uriString = entitiesMetaData.get(entityId).getUrl();
      displayString = Char.toHTML(entitiesMetaData.get(entityId).getHumanReadableRepresentation());
    }
  }
  String entityAnchor = "<a class='entityAnchor' target='_blank' href='" + uriString + "'>" + displayString + "</a>";
  return entityAnchor;
}
 
Example 5
Source File: ModelAnalysis.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 统计信息,计算删除非0特征后,权重的长度
 */
public void removeZero() {
	boolean freeze = false;
	if (feature.isStopIncrement()) {
		feature.setStopIncrement(false);
		freeze = true;
	}
	TIntObjectHashMap<String> index = (TIntObjectHashMap<String>) feature.toInverseIndexMap();
	
	System.out.println("原字典大小"+index.size());
	System.out.println("原字典大小"+feature.size());
	StringFeatureAlphabet newfeat = new StringFeatureAlphabet();
	cl.factory.setDefaultFeatureAlphabet(newfeat);
	for(int i=0;i<weights.length;i++){
			TIntFloatIterator itt = weights[i].data.iterator();
			HashSparseVector ww = new HashSparseVector();
			while(itt.hasNext()){
				itt.advance();
				float v = itt.value();
				if(Math.abs(v)<1e-3f)
					continue;
				String fea = index.get(itt.key());
				int newidx = newfeat.lookupIndex(fea);
				ww.put(newidx, v);				
		}
		weights[i] = ww;	
	}
	
	newfeat.setStopIncrement(freeze);
	System.out.println("新字典大小"+newfeat.size());		
	System.out.println("新字典大小"+feature.size());		
	index.clear();		
}
 
Example 6
Source File: DynamicGrid.java    From cineast with MIT License 5 votes vote down vote up
@Override
public T get(int x, int y) {
  if (!grid.containsKey(x)) {
    return defaultElement;
  }
  TIntObjectHashMap<T> map = grid.get(x);
  if (!map.containsKey(y)) {
    return defaultElement;
  }
  return map.get(y);
}
 
Example 7
Source File: WordCluster.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String toString(){
	StringBuilder sb = new StringBuilder();

	TIntObjectHashMap<TLinkedHashSet<String>> sets = new TIntObjectHashMap<TLinkedHashSet<String>>();

	for(int i=0;i<alpahbet.size();i++){
		int head = getHead(i);
		TLinkedHashSet<String> s = sets.get(head);
		if(s==null){
			s = new TLinkedHashSet();
			sets.put(head, s);
		}
		s.add(alpahbet.lookupString(i));
	}

	TIntObjectIterator<TLinkedHashSet<String>> it = sets.iterator();
	while(it.hasNext()){
		it.advance();
		if(it.value().size()<2)
			continue;
		sb.append(wordProb.get(it.key()));
		sb.append(" ");
		TObjectHashIterator<String> itt = it.value().iterator();
		while(itt.hasNext()){
			String ss = itt.next();
			sb.append(ss);
			sb.append(" ");
		}
		sb.append("\n");
	}

	return sb.toString();

}
 
Example 8
Source File: UnitBuilder.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static String buildUnit(TIntList unitTokens, TIntObjectHashMap<String> id2word) {
  String[] tokenStrings = new String[unitTokens.size()];
  for (int i = 0; i < unitTokens.size(); i++) {
    tokenStrings[i] = id2word.get(unitTokens.get(i));
  }
  return buildUnit(tokenStrings);
}
 
Example 9
Source File: LanguageModelContext.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public int getEntityUnitCount(Entity entity, int unit, UnitType unitType) {
  TIntObjectHashMap<TIntIntHashMap> curEntityUnitCounts = entityUnitCounts.get(unitType.ordinal());
  if (curEntityUnitCounts == null) return 0;
  TIntIntHashMap curUnitCounts = curEntityUnitCounts.get(entity.getId());
  if (curUnitCounts == null) return 0;
  return curUnitCounts.get(unit);
}
 
Example 10
Source File: DataAccessSQL.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override public Entities getEntitiesForInternalIds(int[] ids) throws EntityLinkingDataAccessException {
  Entities entities = new Entities();
  TIntObjectHashMap<KBIdentifiedEntity> yagoEntityIds = getKnowlegebaseEntitiesForInternalIds(ids);
  if (yagoEntityIds.isEmpty()) {
    return entities;
  }
  for (int id : ids) {
    KBIdentifiedEntity kbi = yagoEntityIds.get(id);
    entities.add(new Entity(kbi, id));
  }
  return entities;
}
 
Example 11
Source File: DataAccessKeyValueStore.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override public TIntDoubleHashMap getEntityPriors(String mention, boolean isNamedentity) throws EntityLinkingDataAccessException {
  TIntDoubleHashMap entityPriors = new TIntDoubleHashMap();
  try {
    DatabaseKeyValueStore db = DatabaseKeyValueStore.DICTIONARY_MENTION;
    if(db.getPartitions() != 1) {
      throw new IllegalArgumentException("Multiple partitions not supported for this key-value store");
    }
    Codec codec = DataAccessKeyValueStoreHandler.singleton().getCodec(db);
    mention = EntityLinkingManager.conflateToken(mention, isNamedentity);
    KeyValueStore<byte[],byte[]> keyValueStore = DataAccessKeyValueStoreHandler.singleton().getKeyValueStore(db);
    byte[] resultBytes = keyValueStore.get(codec.encodeKey(mention));
    if (resultBytes != null) {
      TIntDoubleHashMap tempResults = (TIntDoubleHashMap) codec.decodeValue(resultBytes);
      TIntObjectHashMap<EntityType> entityClasses = DataAccessCache.singleton().getEntityClasses(tempResults.keys());

      for(int entityId: entityClasses.keys()) {
        if (isNamedentity) {
          if (!(entityClasses.get(entityId) == EntityType.CONCEPT)) {
            entityPriors.put(entityId, tempResults.get(entityId));
          }
        }
        else {
          if (!(entityClasses.get(entityId) == EntityType.NAMED_ENTITY)) {
            entityPriors.put(entityId, tempResults.get(entityId));
          }
        }
      }
    }
  } catch (Exception e) {
    throw new EntityLinkingDataAccessException(e);
  }
  return entityPriors;
}
 
Example 12
Source File: TaskScheduler.java    From scheduler with GNU Lesser General Public License v3.0 5 votes vote down vote up
private TIntObjectHashMap<int[]> myChanges(TIntIntHashMap[] change) {
    TIntObjectHashMap<int[]> map = new TIntObjectHashMap<>();
    for (int d = 0; d < nbDims; d++) {
        for (int t : change[d].keys()) {
            int[] upd = map.get(t);
            if (upd == null) {
                upd = new int[nbDims];
                map.put(t, upd);
            }
            upd[d] += change[d].get(t);
        }
    }
    return map;
}
 
Example 13
Source File: UnitsStatCollector.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public void startCollectingStatsInMemory(UnitType unitType) throws SQLException, EntityLinkingDataAccessException {
  TIntObjectHashMap<TIntHashSet> entityUnits = UnitUtil.loadEntityUnits(unitType.getUnitSize());

  // Count the occurrences of units with respect to entities
  TIntIntHashMap unitCounts = new TIntIntHashMap();
  for (int entity : entityUnits.keys()) {
    TIntHashSet units = entityUnits.get(entity);
    for (int unit : units.toArray()) {
      unitCounts.adjustOrPutValue(unit, 1, 1);
    }
  }

  logger.info("Storing data ...");
  storeUnitsIntoDB(unitCounts, unitType);
}
 
Example 14
Source File: YagoEntityKeyphraseCooccurrenceDataProvider.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
private void fillSuperdocKeyphraseCounts(TIntObjectHashMap<int[]> linkTo, TIntObjectHashMap<TIntIntHashMap> superdocKeyphraseCounts)
    throws SQLException {
  Connection con = EntityLinkingManager.getConnectionForDatabase(EntityLinkingManager.DB_AIDA);

  con.setAutoCommit(false);
  Statement stmt = con.createStatement();
  stmt.setFetchSize(fetchSize);

  String sql = "SELECT entity,keyphrase FROM " + DataAccessSQL.ENTITY_KEYPHRASES;
  ResultSet rs = stmt.executeQuery(sql);

  int reportFreq = 1000000;
  int rowCount = 0;
  long totalCount = 0;
  long totalLinksSinceLast = 0;
  long startTime = System.currentTimeMillis();

  while (rs.next()) {
    // write message every 1,000,000 rows
    if ((++rowCount % reportFreq) == 0) {
      long duration = System.currentTimeMillis() - startTime;
      double avgLinks = (double) totalLinksSinceLast / (double) reportFreq;
      double linksPerMs = (double) totalLinksSinceLast / duration;
      logger.info(
          "Read " + rowCount / 1000000 + " mio e/kp ... " + totalCount + " kp-counts adjusted." + " Average number of links was: " + avgLinks + " ("
              + linksPerMs + " links/ms).");
      startTime = System.currentTimeMillis();
      totalLinksSinceLast = 0;
    }

    // get ids
    int eid = rs.getInt("entity");
    int kid = rs.getInt("keyphrase");

    // add keyphrase to entity itself
    TIntIntHashMap keyphraseCount = superdocKeyphraseCounts.get(eid);
    boolean adjusted = keyphraseCount.adjustValue(kid, 1);
    // Just bookkeeping.
    if (adjusted) ++totalCount;

    // add keyphrase to entities this entity links to
    int[] links = linkTo.get(eid);
    if (links != null) {
      totalLinksSinceLast += links.length;
      for (int linkedEid : links) {
        keyphraseCount = superdocKeyphraseCounts.get(linkedEid);

        if (keyphraseCount != null) {
          adjusted = keyphraseCount.adjustValue(kid, 1);
          // Just bookkeeping.
          if (adjusted) ++totalCount;
        } else {
          logger.warn("No dictionary for entity '" + eid + "'");
        }
      }
    }
  }

  rs.close();
  logger.info(totalCount + " kp-counts adjusted");

  con.setAutoCommit(true);
  EntityLinkingManager.releaseConnection(con);
}
 
Example 15
Source File: LanguageModelContext.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public TIntIntMap getUnitCountsForEntity(Entity entity, UnitType unitType) {
  TIntObjectHashMap<TIntIntHashMap> curEntityUnitCounts = entityUnitCounts.get(unitType.ordinal());
  if (curEntityUnitCounts == null) return new TIntIntHashMap();
  TIntIntHashMap curUnitCounts = curEntityUnitCounts.get(entity.getId());
  return curUnitCounts != null ? curUnitCounts : new TIntIntHashMap();
}
 
Example 16
Source File: DataAccess.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public static EntityMetaData getEntityMetaData(int entityId) throws EntityLinkingDataAccessException {
  int[] entitiesIds = new int[1];
  entitiesIds[0] = entityId;
  TIntObjectHashMap<EntityMetaData> results = getEntitiesMetaData(entitiesIds);
  return results.get(entityId);
}
 
Example 17
Source File: DataAccess.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public static Set<Type> getTypes(Entity entity) throws EntityLinkingDataAccessException {
  Entities entities = new Entities();
  entities.add(entity);
  TIntObjectHashMap<Set<Type>> entityTypes = getTypes(entities);
  return entityTypes.get(entity.getId());
}
 
Example 18
Source File: DataAccess.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public static int[] getInlinkNeighbors(Entity e) throws EntityLinkingDataAccessException {
  Entities entities = new Entities();
  entities.add(e);
  TIntObjectHashMap<int[]> neighbors = getInlinkNeighbors(entities);
  return neighbors.get(e.getId());
}
 
Example 19
Source File: DirectionalityRenderer.java    From Signals with GNU General Public License v3.0 4 votes vote down vote up
private void build(TIntObjectHashMap<BakedRenderer> bakedRenderers, ImmutableList<NetworkRail<MCPos>> edge){

        for(int edgeIndex = 1; edgeIndex < edge.size() - 1; edgeIndex++) {
            NetworkRail<MCPos> prevRail = edge.get(edgeIndex - 1);
            MCNetworkRail curRail = (MCNetworkRail)edge.get(edgeIndex);
            NetworkRail<MCPos> nextRail = edge.get(edgeIndex + 1);

            EnumHeading prevHeading = curRail.getPos().getRelativeHeading(prevRail.getPos());
            EnumHeading nextHeading = nextRail.getPos().getRelativeHeading(curRail.getPos());
            if(prevHeading == null || nextHeading == null || prevHeading != nextHeading || curRail.getPos().getDimID() != nextRail.getPos().getDimID() || curRail.getPos().getDimID() != prevRail.getPos().getDimID()) continue;

            BakedRenderer bakedRenderer = bakedRenderers.get(curRail.getPos().getDimID());
            if(bakedRenderer == null) {
                bakedRenderer = new BakedRenderer();
                bakedRenderers.put(curRail.getPos().getDimID(), bakedRenderer);
            }

            MCPos pos = curRail.getPos();
            EnumFacing facing = HeadingUtils.toFacing(nextHeading).getOpposite();
            EnumFacing rotatedFacing = facing.rotateY();
            EnumFacing rotatedFacing2 = facing.rotateYCCW();
            int yOffset = AbstractRailRenderer.getRailHeightOffset(curRail, facing);

            Vec3d posVec = new Vec3d(pos.getX() + 0.5, pos.getY() + (yOffset != 0 ? 0.6001 : 0.1001), pos.getZ() + 0.5);

            double arrowSize = 0.1;
            double spacing = 0.1;

            for(int i = -2; i < -1; i++) {
                Vec3d shiftedPosVec = posVec.addVector(facing.getFrontOffsetX() * spacing * i, spacing * i * yOffset + 0.001, facing.getFrontOffsetZ() * spacing * i);
                Vec3d vecBack = shiftedPosVec.addVector(facing.getFrontOffsetX() * arrowSize, arrowSize * yOffset, facing.getFrontOffsetZ() * arrowSize);
                Vec3d c1 = vecBack.addVector(rotatedFacing.getFrontOffsetX() * arrowSize, 0, rotatedFacing.getFrontOffsetZ() * arrowSize);
                Vec3d c2 = vecBack.addVector(rotatedFacing2.getFrontOffsetX() * arrowSize, 0, rotatedFacing2.getFrontOffsetZ() * arrowSize);

                bakedRenderer.add(shiftedPosVec.x, shiftedPosVec.y, shiftedPosVec.z);
                bakedRenderer.add(c1.x, c1.y, c1.z);
                //buffer.pos(shiftedPosVec.x, shiftedPosVec.y, shiftedPosVec.z).color(r, g, b, 1).endVertex();
                bakedRenderer.add(c2.x, c2.y, c2.z);
            }
        }
    }
 
Example 20
Source File: SuperdocEntityKeyphraseCountCollector.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
private void fillSuperdocKeyphraseCounts(TIntObjectHashMap<int[]> linkTo, TIntObjectHashMap<TIntIntHashMap> superdocKeyphraseCounts)
    throws SQLException {
  Connection con = EntityLinkingManager.getConnectionForDatabase(EntityLinkingManager.DB_AIDA);
  con.setAutoCommit(false);
  Statement stmt = con.createStatement();
  stmt.setFetchSize(fetchSize);

  String sql = "SELECT entity,keyphrase FROM " + DataAccessSQL.ENTITY_KEYPHRASES;
  ResultSet rs = stmt.executeQuery(sql);

  int reportFreq = 1000000;
  int rowCount = 0;
  long totalCount = 0;
  long totalLinksSinceLast = 0;
  long startTime = System.currentTimeMillis();

  while (rs.next()) {
    // write message every 1,000,000 rows
    if ((++rowCount % reportFreq) == 0) {
      long duration = System.currentTimeMillis() - startTime;
      double avgLinks = (double) totalLinksSinceLast / (double) reportFreq;
      double linksPerMs = (double) totalLinksSinceLast / duration;
      logger.info(
          "Read " + rowCount / 1000000 + " mio e/kp ... " + totalCount + " kp-counts adjusted." + " Average number of links was: " + avgLinks + " ("
              + linksPerMs + " links/ms).");
      startTime = System.currentTimeMillis();
      totalLinksSinceLast = 0;
    }

    // get ids
    int eid = rs.getInt("entity");
    int kid = rs.getInt("keyphrase");

    // add keyphrase to entity itself
    TIntIntHashMap keyphraseCount = superdocKeyphraseCounts.get(eid);
    boolean adjusted = keyphraseCount.adjustValue(kid, 1);
    // Just bookkeeping.
    if (adjusted) ++totalCount;

    // add keyphrase to entities this entity links to
    int[] links = linkTo.get(eid);
    if (links != null) {
      totalLinksSinceLast += links.length;
      for (int linkedEid : links) {
        keyphraseCount = superdocKeyphraseCounts.get(linkedEid);

        if (keyphraseCount != null) {
          adjusted = keyphraseCount.adjustValue(kid, 1);
          // Just bookkeeping.
          if (adjusted) ++totalCount;
        } else {
          logger.warn("No dictionary for entity '" + eid + "'");
        }
      }
    }
  }

  rs.close();
  stmt.close();
  con.setAutoCommit(true);
  EntityLinkingManager.releaseConnection(con);

  logger.info(totalCount + " kp-counts adjusted");
}