@Test public void testGetTypes() throws Exception { FreebaseHelper freebaseHelper = new FreebaseHelper(); FreebaseRelation typesForRelationFromFreebase = freebaseHelper.getTypesForRelationFromFreebase("ns:people.person.education..education.education.degree"); Assert.assertThat(typesForRelationFromFreebase.getDomain(), is("ns:people.person")); Assert.assertThat(typesForRelationFromFreebase.getRange(), is("ns:education.educational_degree")); typesForRelationFromFreebase = freebaseHelper.getTypesForRelationFromFreebase("ns:people.person.education..education.education.institution"); Assert.assertThat(typesForRelationFromFreebase.getDomain(), is("ns:people.person")); Assert.assertThat(typesForRelationFromFreebase.getRange(), is("ns:education.educational_institution")); typesForRelationFromFreebase = freebaseHelper.getTypesForRelationFromFreebase("ns:base.rugby.rugby_club.coaches_of_this_team..base.rugby.rugby_coaching_tenure.coach"); Assert.assertThat(typesForRelationFromFreebase.getDomain(), is("ns:base.rugby.rugby_club")); Assert.assertThat(typesForRelationFromFreebase.getRange(), is("ns:base.rugby.rugby_coach")); typesForRelationFromFreebase = freebaseHelper.getTypesForRelationFromFreebase("ns:education.educational_institution.students_graduates..education.education.specialization"); Assert.assertThat(typesForRelationFromFreebase.getDomain(), is("ns:education.educational_institution")); Assert.assertThat(typesForRelationFromFreebase.getRange(), is("ns:education.field_of_study")); }
public FreebaseRelation getTypesForRelationFromFreebase(String r) throws IOException { final String relation = r.replaceFirst("ns:", ""); String toAsk = relation; String left = null; boolean direct = true; if (relation.contains("..")) { toAsk = relation.substring(0, relation.indexOf("..")); left = relation.substring(relation.indexOf("..") + 2); direct = false; } JsonNode property; try { property = getFreebaseTopicForID(toAsk); } catch (FileNotFoundException e) { LOG.warn("Error", e); return null; } String one; String two; if (direct) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); two = property.has("/type/property/expected_type") ? property.get("/type/property/expected_type").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); } else if (!property.has("/type/property/expected_type")) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); final JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } else { one = property.get("/type/property/schema").get("values").get(0).get("id").asText(); JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } one = one.replaceFirst("/", "ns:").replaceAll("/", "."); two = two.replaceFirst("/", "ns:").replaceAll("/", "."); FreebaseRelation relDescription = new FreebaseRelation(one, two, null, null); LOG.info(relDescription.toString()); return relDescription; }
FreebaseHelper { public FreebaseRelation getTypesForRelationFromFreebase(String r) throws IOException { final String relation = r.replaceFirst("ns:", ""); String toAsk = relation; String left = null; boolean direct = true; if (relation.contains("..")) { toAsk = relation.substring(0, relation.indexOf("..")); left = relation.substring(relation.indexOf("..") + 2); direct = false; } JsonNode property; try { property = getFreebaseTopicForID(toAsk); } catch (FileNotFoundException e) { LOG.warn("Error", e); return null; } String one; String two; if (direct) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); two = property.has("/type/property/expected_type") ? property.get("/type/property/expected_type").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); } else if (!property.has("/type/property/expected_type")) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); final JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } else { one = property.get("/type/property/schema").get("values").get(0).get("id").asText(); JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } one = one.replaceFirst("/", "ns:").replaceAll("/", "."); two = two.replaceFirst("/", "ns:").replaceAll("/", "."); FreebaseRelation relDescription = new FreebaseRelation(one, two, null, null); LOG.info(relDescription.toString()); return relDescription; } }
FreebaseHelper { public FreebaseRelation getTypesForRelationFromFreebase(String r) throws IOException { final String relation = r.replaceFirst("ns:", ""); String toAsk = relation; String left = null; boolean direct = true; if (relation.contains("..")) { toAsk = relation.substring(0, relation.indexOf("..")); left = relation.substring(relation.indexOf("..") + 2); direct = false; } JsonNode property; try { property = getFreebaseTopicForID(toAsk); } catch (FileNotFoundException e) { LOG.warn("Error", e); return null; } String one; String two; if (direct) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); two = property.has("/type/property/expected_type") ? property.get("/type/property/expected_type").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); } else if (!property.has("/type/property/expected_type")) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); final JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } else { one = property.get("/type/property/schema").get("values").get(0).get("id").asText(); JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } one = one.replaceFirst("/", "ns:").replaceAll("/", "."); two = two.replaceFirst("/", "ns:").replaceAll("/", "."); FreebaseRelation relDescription = new FreebaseRelation(one, two, null, null); LOG.info(relDescription.toString()); return relDescription; }  }
FreebaseHelper { public FreebaseRelation getTypesForRelationFromFreebase(String r) throws IOException { final String relation = r.replaceFirst("ns:", ""); String toAsk = relation; String left = null; boolean direct = true; if (relation.contains("..")) { toAsk = relation.substring(0, relation.indexOf("..")); left = relation.substring(relation.indexOf("..") + 2); direct = false; } JsonNode property; try { property = getFreebaseTopicForID(toAsk); } catch (FileNotFoundException e) { LOG.warn("Error", e); return null; } String one; String two; if (direct) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); two = property.has("/type/property/expected_type") ? property.get("/type/property/expected_type").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); } else if (!property.has("/type/property/expected_type")) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); final JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } else { one = property.get("/type/property/schema").get("values").get(0).get("id").asText(); JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } one = one.replaceFirst("/", "ns:").replaceAll("/", "."); two = two.replaceFirst("/", "ns:").replaceAll("/", "."); FreebaseRelation relDescription = new FreebaseRelation(one, two, null, null); LOG.info(relDescription.toString()); return relDescription; }  static String transformOldToNewId(String old); static String transformNewToOldId(String newId); Entity getNameForID(String id); Entity getNameForIDs(String id); boolean checkIfCompoundRelationHolds(String id1, String id2, String[] relation); FreebaseRelation getTypesForRelationFromFreebase(String r); void readAllInverseRelations(); static void main(String[] args); }
FreebaseHelper { public FreebaseRelation getTypesForRelationFromFreebase(String r) throws IOException { final String relation = r.replaceFirst("ns:", ""); String toAsk = relation; String left = null; boolean direct = true; if (relation.contains("..")) { toAsk = relation.substring(0, relation.indexOf("..")); left = relation.substring(relation.indexOf("..") + 2); direct = false; } JsonNode property; try { property = getFreebaseTopicForID(toAsk); } catch (FileNotFoundException e) { LOG.warn("Error", e); return null; } String one; String two; if (direct) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); two = property.has("/type/property/expected_type") ? property.get("/type/property/expected_type").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); } else if (!property.has("/type/property/expected_type")) { one = property.has("/type/property/schema") ? property.get("/type/property/schema").get("values").get(0).get("id").asText() : property.get("/type/object/type").get("values").get(0).get("id").asText(); final JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } else { one = property.get("/type/property/schema").get("values").get(0).get("id").asText(); JsonNode second = getFreebaseTopicForID(left); two = second.has("/type/property/expected_type") ? second.get("/type/property/expected_type").get("values").get(0).get("id").asText() : second.get("/type/object/type").get("values").get(0).get("id").asText(); } one = one.replaceFirst("/", "ns:").replaceAll("/", "."); two = two.replaceFirst("/", "ns:").replaceAll("/", "."); FreebaseRelation relDescription = new FreebaseRelation(one, two, null, null); LOG.info(relDescription.toString()); return relDescription; }  static String transformOldToNewId(String old); static String transformNewToOldId(String newId); Entity getNameForID(String id); Entity getNameForIDs(String id); boolean checkIfCompoundRelationHolds(String id1, String id2, String[] relation); FreebaseRelation getTypesForRelationFromFreebase(String r); void readAllInverseRelations(); static void main(String[] args);  }