@Override public String getPseudoGloss(Entity entity, Set<LexicalRelation> lexicalRelations, Map<SemanticRelation, Integer> semanticRelations) throws LexicalSemanticResourceException { Set<String> pseudoGloss = new HashSet<String>(); for (LexicalRelation lexicalRelation : lexicalRelations) { for (String lexeme : entity.getLexemes()) { pseudoGloss.add(lexeme); pseudoGloss.addAll(this.getRelatedLexemes(lexeme, entity.getPos(), entity.getSense(lexeme), lexicalRelation)); } } for (SemanticRelation semanticRelation : semanticRelations.keySet()) { pseudoGloss.addAll( getSemanticRelationsRecursive(pseudoGloss, semanticRelation, entity, semanticRelations.get(semanticRelation)) ); } return StringUtils.join(pseudoGloss, " ").trim(); }
public Set<Entity> getEntity(String lexeme) throws LexicalSemanticResourceException { Entity e = new Entity(lexeme); Set<Entity> eSet = new HashSet<Entity>(); eSet.add(e); return eSet; }
public static Set<WordEntry> entityToWords (Wiktionary wkt, Entity entity) { Set<WordEntry> words = new HashSet<WordEntry>(); for (PartOfSpeech wktPos : WiktionaryUtils.mapPos(entity.getPos())) { // a Wiktionary entity only contains one lexeme words.addAll(wkt.getWordEntries(entity.getFirstLexeme(), wktPos)); } return words; }
@Override public Entity getEntity(Map<String, String> lexemes, PoS pos) throws LexicalSemanticResourceException { if (lexemes == null) { return null; } Entity e = new Entity(lexemes, pos); // querying each of the lexemes should give the correct answer set Set<Entity> possibleEntities = getEntity(lexemes.keySet().iterator().next(), pos); for (Entity possibleEntity : possibleEntities) { // return the entity if there is a real entity that matches the possible entity if (e.compareTo(possibleEntity) == 0) { return e; } } return null; }
public static Set<Synset> entityToSynsets(Dictionary dict, Entity entity, boolean isCaseSensitive) throws LexicalSemanticResourceException { Set<Synset> results = new HashSet<Synset>(); Set<String> lexemes = entity.getLexemes(); PoS pos = entity.getPos(); String sense = entity.getSense(entity.getFirstLexeme()); Long senseNumber = Long.MIN_VALUE; if (sense != Entity.UNKNOWN_SENSE) {
public static String getGlossFromEntity(Wiktionary wkt, Entity entity) { for (PartOfSpeech wktPos : WiktionaryUtils.mapPos(entity.getPos())) { for (WordEntry w : wkt.getWordEntries(entity.getFirstLexeme(), wktPos)) { String sense = entity.getSense(entity.getFirstLexeme()); if (sense.equals(Entity.UNKNOWN_SENSE)) { StringBuilder glossString = new StringBuilder(); for (WikiString gloss : w.getGlosses()) { glossString.append(gloss.getPlainText()); glossString.append(";"); } if (glossString != null && glossString.length() > 0) { return glossString.toString(); } } else { WikiString gloss = w.getGloss(new Integer(sense)); String glossString = gloss.getPlainText(); if (glossString != null && glossString.length() > 0) { return glossString; } } } } return ""; } }
public Set<Entity> getEntity(String lexeme, PoS pos, String sense) throws LexicalSemanticResourceException { Set<Entity> resultEntities = getEntity(lexeme, pos); Iterator<Entity> entityIter = resultEntities.iterator(); while (entityIter.hasNext()) { Entity e = entityIter.next(); // one of the lexemes must be the given lexeme with the given pos boolean found = false; for (String currentLexeme : e.getLexemes()) { if (currentLexeme.equals(lexeme) && e.getSense(currentLexeme).equals(sense)) { found = true; } } // remove entity from result set, if not found if (!found) { entityIter.remove(); } } return resultEntities; }
public Set<Entity> getParents(Entity entity) throws LexicalSemanticResourceException { Set<Entity> parents = new HashSet<Entity>(); Category cat; String lexeme = WikipediaCategoryUtils.getCaseSensitiveLexeme(entity.getFirstLexeme(), isCaseSensitive); if (lexeme == null) { return parents; } try { cat = wiki.getCategory(lexeme); if (cat == null) { return parents; } Set<Category> parentCategories = cat.getParents(); for (Category parentCategory : parentCategories) { parents.add(new Entity(parentCategory.getTitle().getWikiStyleTitle())); } } catch (WikiApiException e) { throw new LexicalSemanticResourceException(e); } return parents; }
@Override public Map<String, List<String>> getSenseInventory() throws SenseInventoryException { System.out.println("Be careful, this is a quite slow operation."); Map<String, List<String>> senseInventory = new HashMap<String, List<String>>(); try { for (Entity entity : lsr.getEntities()) { for (String lexeme : entity.getLexemes()) { if (!senseInventory.containsKey(lexeme)) { senseInventory.put(lexeme, new ArrayList<String>()); } String sense = entity.getId(); List<String> senses = senseInventory.get(lexeme); senses.add(sense); senseInventory.put(lexeme, senses); } } } catch (LexicalSemanticResourceException e) { throw new SenseInventoryException(e); } return senseInventory; }
/** * Recursively selects all lexemes from related entities up to the given depth. * @param rel semantic relation * @param e entity * @param depth The recursive depth * @return The set of string in the pseudo gloss. * @throws UnsupportedOperationException * @throws LexicalSemanticResourceException */ private Set<String> getSemanticRelationsRecursive(Set<String> pseudoGloss, SemanticRelation semanticRelation, Entity entity, int depth) throws LexicalSemanticResourceException { for (Entity e : this.getRelatedEntities(entity, semanticRelation)) { pseudoGloss.addAll(e.getLexemes()); if (depth > 1) { pseudoGloss = getSemanticRelationsRecursive(pseudoGloss, semanticRelation, e, depth-1); } } return pseudoGloss; }
public boolean containsEntity(Entity entity) throws LexicalSemanticResourceException { return containsLexeme(entity.getFirstLexeme()); }
@Override public String toString() { return this.getId(); }
@Override public POS getPos(String senseId) throws SenseInventoryException { Entity entity; try { entity = lsr.getEntityById(senseId); } catch (LexicalSemanticResourceException e) { throw new SenseInventoryException(e); } return LsrSenseInventoryUtil.convertPos(entity.getPos()); }
Set<String> lexemes = entity.getLexemes(); PoS pos = entity.getPos(); String sense = entity.getSense(entity.getFirstLexeme()); Long senseNumber = Long.MIN_VALUE; if (sense != Entity.UNKNOWN_SENSE) {
public Set<Entity> getChildren(Entity entity) throws LexicalSemanticResourceException { Set<Entity> children = new HashSet<Entity>(); Category cat = null; String lexeme = WikipediaCategoryUtils.getCaseSensitiveLexeme(entity.getFirstLexeme(), isCaseSensitive); if (lexeme == null) { return children; } try { cat = wiki.getCategory(lexeme); if (cat == null) { return children; } Set<Category> childCategories = cat.getChildren(); for (Category childCategory : childCategories) { children.add(new Entity(childCategory.getTitle().getWikiStyleTitle())); } } catch (WikiApiException e) { throw new LexicalSemanticResourceException(e); } return children; }
public Set<String> getRelatedLexemes(String lexeme, PoS pos, String sense, LexicalRelation lexicalRelation) throws LexicalSemanticResourceException { Entity entity = WikipediaArticleUtils.lexemeToEntity(wiki, lexeme, isCaseSensitive); Set<String> relatedLexemes = new HashSet<String>(); if (entity == null) { return relatedLexemes; } if (lexicalRelation.equals(LexicalRelation.antonymy)) { logger.warn("Wikipedia contains no antonymy information. Returning empty set."); } else if (lexicalRelation.equals(LexicalRelation.synonymy)) { for (String synonym : entity.getLexemes()) { String plainSynonym = WikipediaArticleUtils.plainString(synonym); if (!plainSynonym.equals(lexeme)) { relatedLexemes.add(plainSynonym); } } } return relatedLexemes; }
public boolean containsEntity(Entity entity) throws LexicalSemanticResourceException { return containsLexeme(entity.getFirstLexeme()); }
@Override public List<String> getSenses(String sod) throws SenseInventoryException { try { List<String> senses = new ArrayList<String>(); for (Entity entity : lsr.getEntity(sod)) { senses.add(entity.getId()); } return senses; } catch (LexicalSemanticResourceException e) { throw new SenseInventoryException(e); } }
@Override public Entity getEntity(Map<String, String> lexemes, PoS pos) throws LexicalSemanticResourceException { return new Entity(lexemes.keySet().iterator().next()); }
public static Page entityToPage(Wikipedia wiki, Entity entity, boolean isCaseSensitive) throws LexicalSemanticResourceException { String lexeme = getCaseSensitiveLexeme(entity.getFirstLexeme(), isCaseSensitive); try { return wiki.getPage(lexeme); } catch (WikiApiException e) { throw new LexicalSemanticResourceException(e); } }