/** * @param code the code representing a {@link RelationType} enum constant * @return the {@link RelationType} enum constant corresponding to the specified <code>code * </code> */ public static RelationType convert(short code) { for (RelationType relationType : values()) { if (relationType.code() == code) { return relationType; } } return null; }
private Optional<ResolvedField> resolveField(Expression node, QualifiedName name, boolean local) { List<Field> matches = relation.resolveFields(name); if (matches.size() > 1) { throwAmbiguousAttributeException(node, name); } if (matches.isEmpty()) { if (isColumnReference(name, relation)) { return Optional.empty(); } Scope boundary = this; while (!boundary.queryBoundary) { if (boundary.parent.isPresent()) { boundary = boundary.parent.get(); } else { return Optional.empty(); } } if (boundary.parent.isPresent()) { // jump over the query boundary return boundary.parent.get().resolveField(node, name, false); } return Optional.empty(); } else { return Optional.of(asResolvedField(getOnlyElement(matches), local)); } }
private static boolean isColumnReference(QualifiedName name, RelationType relation) { while (name.getPrefix().isPresent()) { name = name.getPrefix().get(); if (!relation.resolveFields(name).isEmpty()) { return true; } } return false; }
/** * Make a rule out of the params. Note that if the relation is bidirectional, it represents two * symmetrical rules, but only one is returned * * @param leftVerb * @param rightVerb * @param score * @param relationType * @return * @throws LexicalResourceException */ private LexicalRule<? extends VerbOceanRuleInfo> makeRule( String leftVerb, String rightVerb, double score, RelationType relationType) throws LexicalResourceException { if (score > maxScore) maxScore = score; return relationType.isEntailing() ? new LexicalRule<VerbOceanRuleInfo>( leftVerb, VERB, rightVerb, VERB, relationType.name(), RESOURCE_NAME, new VerbOceanRuleInfo(relationType, score)) : new LexicalRule<VerbOceanRuleInfo>( rightVerb, VERB, leftVerb, VERB, relationType.name(), RESOURCE_NAME, new VerbOceanRuleInfo(relationType, score)); }
/** * Print the specified relation * * @param from the source class (may be null) * @param fromName the source class's name * @param to the destination class (may be null) * @param toName the destination class's name */ private void relation( Options opt, RelationType rt, ClassDoc from, String fromName, ClassDoc to, String toName, String tailLabel, String label, String headLabel) { // print relation String edgetype = associationMap.get(rt); w.println("\t// " + fromName + " " + rt.toString() + " " + toName); w.println( "\t" + relationNode(from, fromName) + " -> " + relationNode(to, toName) + " [" + "taillabel=\"" + tailLabel + "\", " + "label=\"" + guillemize(opt, label) + "\", " + "headlabel=\"" + headLabel + "\", " + "fontname=\"" + opt.edgeFontName + "\", " + "fontcolor=\"" + opt.edgeFontColor + "\", " + "fontsize=" + opt.edgeFontSize + ", " + "color=\"" + opt.edgeColor + "\", " + edgetype + "];"); // update relation info RelationDirection d = RelationDirection.BOTH; if (rt == RelationType.NAVASSOC || rt == RelationType.DEPEND) d = RelationDirection.OUT; getClassInfo(fromName).addRelation(toName, rt, d); getClassInfo(toName).addRelation(fromName, rt, d.inverse()); }
private ResolvedField asResolvedField(Field field, boolean local) { int fieldIndex = relation.indexOf(field); return new ResolvedField(this, field, fieldIndex, local); }
/** * Ctor read and map all rules from the given verb ocean file, but, keep only rules with allowed * relation types, and, for each verb pair, keep only the highest scoring rule. The rules are then * mapped. * * @param scoreThreshold rules with thresholds not higher than this will be screened * @param verbOceanRelationsFile e.g. Data\RESOURCES\VerbOcean\verbocean.unrefined.2004-05-20.txt * @param allowedRelationTypes only rules with these relations will be returned. others will be * screened. If they contain any of the {@link #FORBIDDEN_RELATION_TYPES}, a * LexicalResourceException is thrown. Cannot be null, can be empty. * @throws LexicalResourceException */ public VerbOceanLexicalResource( double scoreThreshold, File verbOceanRelationsFile, Set<RelationType> allowedRelationTypes) throws LexicalResourceException { if (scoreThreshold <= 0) throw new LexicalResourceException( "the score threshold must be positive. I got " + scoreThreshold); if (verbOceanRelationsFile == null) throw new LexicalResourceException("got null relations file"); if (!verbOceanRelationsFile.exists()) throw new LexicalResourceException(verbOceanRelationsFile + " doesn't exist"); if (allowedRelationTypes == null) throw new LexicalResourceException("allowedRelationTypes is null"); for (RelationType forbiddenRelationType : FORBIDDEN_RELATION_TYPES) if (allowedRelationTypes.contains(forbiddenRelationType)) throw new LexicalResourceException( "The given allowed relation types set " + allowedRelationTypes + " contains a forbidden relation type " + forbiddenRelationType); try { VERB = new BySimplerCanonicalPartOfSpeech(SimplerCanonicalPosTag.VERB); } catch (UnsupportedPosTagStringException e) { throw new LexicalResourceException("Internal error", e); } PairMap<String, LexicalRule<? extends VerbOceanRuleInfo>> mapRulesByUnorderedPair = new PairMap<String, LexicalRule<? extends VerbOceanRuleInfo>>(); Set<Pair<String>> verbPairs = new LinkedHashSet<Pair<String>>(); // read and map all rules, but, keep only rules with allowed relation types, and, for each verb // pair, keep only the highest scoring rule try { BufferedReader reader = new BufferedReader(new FileReader(verbOceanRelationsFile)); String line; while ((line = reader.readLine()) != null) { if (line.length() != 0 && line.charAt(0) != '#') // skip empty and commented lines { String[] parts = line.split(" "); RelationType relationType = RelationType.parse(parts[1]); double score = Double.parseDouble(parts[4]); if (allowedRelationTypes.contains(relationType) && score > scoreThreshold) // screen out unallowed relation types and low scores { String leftVerb = parts[0]; String rightVerb = parts[2]; Pair<String> verbPair = new Pair<String>(leftVerb, rightVerb); LexicalRule<? extends VerbOceanRuleInfo> comparedRule = mapRulesByUnorderedPair.getValueOf(verbPair); if (comparedRule == null || score > comparedRule .getConfidence()) // if there is a better rule for the same verb pair, skip // this rule mapRulesByUnorderedPair.put( verbPair, makeRule(leftVerb, rightVerb, score, relationType)); if (comparedRule == null) verbPairs.add(verbPair); } } } reader.close(); } catch (FileNotFoundException e) { throw new LexicalResourceException("file not found: " + verbOceanRelationsFile, e); } catch (IOException e) { throw new LexicalResourceException("IO error reading: " + verbOceanRelationsFile, e); } // fill up the one sided rule maps fillTheRuleMaps(mapRulesByUnorderedPair, verbPairs); }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); // bail out if data is null, or if nobody is selected..... // TODO: you do this! (paintComponent - bail) if (currentPerson == null) return; int width = this.getBounds().width; int height = this.getBounds().height; circleDiam = Math.min(width / 10, height / 10); int nameWidth; g.setFont(myFont); // loop through each of the relationships - you'll need to draw: // 1) the line for the relationship // 2) a string with a relationshipType for the line // 3) a circle for the related person // 3b) an outline for the related person, if this one is selected. // 4) the name of the related person // You'll want to make use of getCenterXForObject(), getCenterYForObject(), // lineColor, objectColor, // TODO: you do this! (paintComponent - loop through relations) ArrayList<Relationship> relationships = controller.getAllRelationshispForPerson(currentPerson); for (int i = 0; i < relationships.size(); i++) { Relationship currentRel = relationships.get(i); PeopleClass secondaryPerson = currentRel.getSecondaryPerson(); RelationType relType = currentRel.getRelationType(); String secondaryPersonName = secondaryPerson.getFullName(); String relationTypeName; if (secondaryPerson.getIsMale()) relationTypeName = relType.getFwdMaleName(); else relationTypeName = relType.getFwdFemaleName(); int stringWidth; int stringHeight; // I realize that hte stringHeight is 10 pixels, but I will use Java's built // in methods to calculate it // draw the lines that branch off the center circle g.setColor(lineColor); g.drawLine( width / 2, height / 2, getCenterXForObject(i, relationships.size()), getCenterYForObject(i, relationships.size())); // draw ovals g.setColor(objectColor); g.fillOval( getCenterXForObject(i, relationships.size()) - circleDiam / 2, getCenterYForObject(i, relationships.size()) - circleDiam / 2, circleDiam, circleDiam); // draw the name of the secondaryPerson g.setColor(Color.BLACK); stringWidth = (int) g.getFontMetrics().getStringBounds(secondaryPersonName, g).getWidth(); stringHeight = (int) g.getFontMetrics().getStringBounds(secondaryPersonName, g).getHeight(); g.drawString( secondaryPersonName, getCenterXForObject(i, relationships.size()) - stringWidth / 2, getCenterYForObject(i, relationships.size()) + stringHeight / 2); // clear a bit of area to draw the string g.setColor(Color.LIGHT_GRAY); stringWidth = (int) g.getFontMetrics().getStringBounds(relationTypeName, g).getWidth(); stringHeight = (int) g.getFontMetrics().getStringBounds(relationTypeName, g).getHeight(); g.fillRect( getCenterXForObjectHalfDistance(i, relationships.size()) - stringWidth / 2, getCenterYForObjectHalfDistance(i, relationships.size()) - stringHeight / 2, stringWidth, stringHeight); // draw the relationTypeName string in the space you cleared out g.setColor(Color.BLACK); g.drawString( relationTypeName, getCenterXForObjectHalfDistance(i, relationships.size()) - stringWidth / 2, getCenterYForObjectHalfDistance(i, relationships.size()) + stringHeight / 2); } g.setColor(subjectColor); g.fillOval(width / 2 - circleDiam / 2, height / 2 - circleDiam / 2, circleDiam, circleDiam); // Get the name of the current person, and set 'mainName' to it. // TODO: You do this! (paintComponent - mainName) String mainName = currentPerson.getFullName(); g.setColor(Color.BLACK); nameWidth = g.getFontMetrics().stringWidth(mainName); g.drawString(mainName, width / 2 - nameWidth / 2, height / 2 + 5); }
public Map<RelationType, Set<String>> getAllRelatedWords(String word) { Map<RelationType, Set<String>> ret = createContainer(); identity.add(word); relatedWord.add(word); getAllSenses(word); if (allSenses.size() == 0) { relatedWordWithPOS.add(new String[] {word}); return ret; } Iterator<Synset> senseItr = allSenses.iterator(); Synset sense = null; Word[] words = null; Word w = null; POS pos = null; String lemma = null; while (senseItr.hasNext()) { sense = senseItr.next(); words = sense.getWords(); /** Synonyms */ String syn = null; for (int i = 0; i < words.length; i++) { w = words[i]; syn = w.getLemma(); pos = w.getPOS(); // System.out.println("Synonym:"+syn); synonyms.add(syn); // relatedWord.add(word); checkWordWithPOS(syn, pos); } /** Morphological variation */ POS sensePOS = sense.getPOS(); if (sensePOS == POS.NOUN) { addMorph(word + "s", sensePOS); addMorph(word + "es", sensePOS); addMorph(word.substring(0, word.length() - 1) + "ies", sensePOS); } else if (sensePOS == POS.VERB) { if (word.endsWith("e")) { addMorph(word + "r", sensePOS); addMorph(word.substring(0, word.length() - 1) + "ing", sensePOS); addMorph(word + "d", sensePOS); } else { addMorph(word + "er", sensePOS); addMorph(word + "ing", sensePOS); addMorph(word + "ed", sensePOS); } } /** Hypernyms, etc */ for (RelationType relation : RelationType.values()) { PointerTargetNodeList nodeList = null; Set<String> listToStore = null; // RelationType relation = relations[i]; try { switch (relation) { case hype: nodeList = pUtils.getDirectHypernyms(sense); break; case hypo: nodeList = pUtils.getDirectHyponyms(sense); break; case derv: nodeList = pUtils.getDerived(sense); break; case vgrp: nodeList = pUtils.getVerbGroup(sense); break; case cause: nodeList = pUtils.getCauses(sense); break; case entl: nodeList = pUtils.getEntailments(sense); break; case entlby: nodeList = pUtils.getEntailedBy(sense); break; case antm: nodeList = pUtils.getAntonyms(sense); break; case syn2: nodeList = pUtils.getSynonyms(sense); break; case alsoc: nodeList = pUtils.getAlsoSees(sense); break; case extd: // pUtils.getExtendedAntonyms(sense).print(); nodeList = (PointerTargetNodeList) pUtils.getExtendedAntonyms(sense).toList(); break; case indi: // pUtils.getIndirectAntonyms(sense).print(); nodeList = (PointerTargetNodeList) pUtils.getIndirectAntonyms(sense).toList(); break; } } catch (Exception e) { } if (nodeList != null) { listToStore = ret.get(relation); Iterator targetItr = nodeList.iterator(); PointerTargetNode pTargetNode = null; while (targetItr.hasNext()) { pTargetNode = (PointerTargetNode) targetItr.next(); if (!pTargetNode.isLexical()) { words = pTargetNode.getSynset().getWords(); for (int j = 0; j < words.length; j++) { w = words[j]; lemma = w.getLemma(); pos = w.getPOS(); if (lemma.contains("_")) { String[] parts = lemma.split("_"); if (parts.length == 2) { multiword.add(lemma.toLowerCase()); } } else { listToStore.add(lemma); checkWordWithPOS(lemma, pos); } } } else { w = pTargetNode.getWord(); lemma = w.getLemma(); pos = w.getPOS(); if (lemma.contains("_")) { String[] parts = lemma.split("_"); if (parts.length == 2) { multiword.add(lemma.toLowerCase()); } } else { listToStore.add(lemma); checkWordWithPOS(lemma, pos); } } } } nodeList = null; } } // end for all senses // System.out.println("Synonyms:"); // for(String w: synonyms) // System.out.println("\t"+w); // System.out.println("\nSynonym2:"); // for(String w: synonyms2) // System.out.println("\t"+w); // System.out.println("\nHypernyms:"); // for(String w: hypernyms) // System.out.println("\t"+w); // System.out.println("\nHyponyms:"); // for(String w: hyponyms) // System.out.println("\t"+w); // System.out.println("\nCoordinates:"); // for(String w: coordinates) // System.out.println("\t"+w); // System.out.println("\nDerived:"); // for(String w: derived) // System.out.println("\t"+w); // System.out.println("\nVerbGroup:"); // for(String w: verbGroup) // System.out.println("\t"+w); // System.out.println("\nCauses:"); // for(String w: causes) // System.out.println("\t"+w); // System.out.println("\nEntailments:"); // for(String w: entailments) // System.out.println("\t"+w); // System.out.println("\nEntailedBys:"); // for(String w: entailedBys) // System.out.println("\t"+w); // System.out.println("\nAntonym:"); // for(String w: antonyms) // System.out.println("\t"+w); // System.out.println("\nExtendedAntonym:"); // for(String w: extendedAntonyms) // System.out.println("\t"+w); // System.out.println("\nIndirectAntonym:"); // for(String w: indirectAntonyms) // System.out.println("\t"+w); // System.out.println("\nAlso See:"); // for(String w: alsosees) // System.out.println("\t"+w); return ret; }
RelType(boolean shouldBeSerialized, String serializedName) { type = RelationType.of(this, shouldBeSerialized, serializedName); }
/** * Print all relations for a given's class's tag * * @param tagname the tag containing the given relation * @param from the source class * @param edgetype the dot edge specification */ private void allRelation(Options opt, RelationType rt, ClassDoc from) { String tagname = rt.toString().toLowerCase(); for (Tag tag : from.tags(tagname)) { String t[] = StringUtil.tokenize(tag.text()); // l-src label l-dst target if (t.length != 4) { System.err.println( "Error in " + from + "\n" + tagname + " expects four fields (l-src label l-dst target): " + tag.text()); return; } ClassDoc to = from.findClass(t[3]); if (to != null) { if (hidden(to)) continue; relation(opt, rt, from, to, t[0], t[1], t[2]); } else { if (hidden(t[3])) continue; relation(opt, rt, from, from.toString(), to, t[3], t[0], t[1], t[2]); } } }