public List<Integer> createUnallowedEdges( String relation, Map<Integer, Integer> inverses, Map<String, List<String>> embeddings, Dictionary edgeDict) { List<Integer> unallowedEdges = new ArrayList<Integer>(); // The relation itself is an unallowed edge type. int relIndex = edgeDict.getIndex(relation); unallowedEdges.add(relIndex); // If the relation has an inverse, it's an unallowed edge type. Integer inverseIndex = inverses.get(relIndex); String inverse = null; if (inverseIndex != null) { unallowedEdges.add(inverseIndex); inverse = edgeDict.getString(inverseIndex); } // And if the relation has an embedding (really a set of cluster ids), // those should be // added to the unallowed edge type list. if (embeddings != null) { List<String> relationEmbeddings = embeddings.get(relation); if (relationEmbeddings != null) { for (String embedded : embeddings.get(relation)) { unallowedEdges.add(edgeDict.getIndex(embedded)); } } if (inverse != null) { List<String> inverseEmbeddings = embeddings.get(inverse); if (inverseEmbeddings != null) { for (String embedded : embeddings.get(inverse)) { unallowedEdges.add(edgeDict.getIndex(embedded)); } } } } return unallowedEdges; }
/** * Reads a file containing a mapping between relations and their inverses, and returns the result * as a map. */ public Map<Integer, Integer> createInverses(String filename, Dictionary dict) throws IOException { Map<Integer, Integer> inverses = new HashMap<Integer, Integer>(); BufferedReader reader = new BufferedReader(new FileReader(filename)); String line; while ((line = reader.readLine()) != null) { String[] parts = line.split("\t"); int rel1 = dict.getIndex(parts[0]); int rel2 = dict.getIndex(parts[1]); inverses.put(rel1, rel2); // Just for good measure, in case the file only lists each relation // once. inverses.put(rel2, rel1); } reader.close(); return inverses; }