Skip to content

Commit

Permalink
taxonomy from resources
Browse files Browse the repository at this point in the history
  • Loading branch information
gadelrab committed Jun 23, 2017
1 parent a4e4b12 commit 515b88d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
13 changes: 11 additions & 2 deletions src/main/java/de/mpii/yagotools/YagoTaxonomy.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.mpii.yagotools.utils.YagoDataReader;
import de.mpii.yagotools.utils.YagoRelations;

import java.io.File;
import java.util.*;


Expand All @@ -13,15 +14,20 @@
public class YagoTaxonomy {

//private static final String SUB_CLASS_OF = "rdfs:subClassOf";
String TAXONOMY_FILE_PATH="resources/bigData/yagoTaxonomy_withGeo.tsv";
// String TAXONOMY_FILE_PATH="resources/bigData/yagoTaxonomy_withGeo.tsv";
String TAXONOMY_FILE_PATH="bigData/yagoTaxonomy_withGeo.tsv";

private static YagoTaxonomy instance;

private Multimap<String,String> typesParents;
private Map<String,Set<String>> transitiveParents;

private YagoTaxonomy(){
typesParents= YagoDataReader.loadDataInMap(TAXONOMY_FILE_PATH,new String[]{YagoRelations.SUB_CLASS_OF}, YagoDataReader.MapType.SUBJ_2_OBJ);
ClassLoader classLoader = getClass().getClassLoader();
File taxonomyFile = new File(classLoader.getResource(TAXONOMY_FILE_PATH).getFile());

typesParents= YagoDataReader.loadDataInMap(taxonomyFile,new String[]{YagoRelations.SUB_CLASS_OF}, YagoDataReader.MapType.SUBJ_2_OBJ);
// typesParents= YagoDataReader.loadDataInMap(TAXONOMY_FILE_PATH,new String[]{YagoRelations.SUB_CLASS_OF}, YagoDataReader.MapType.SUBJ_2_OBJ);
loadTransitiveParents();
}

Expand Down Expand Up @@ -83,6 +89,9 @@ public Set<String> getParents(Collection<String> keys){


public static void main (String [] args){

// args=new String[]{"<wikicat_American_film_directors>"};

YagoTaxonomy yt= YagoTaxonomy.getInstance();

System.out.println(yt.getParents(args[0]));
Expand Down
48 changes: 24 additions & 24 deletions src/main/java/de/mpii/yagotools/utils/YagoDataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mpi.tools.basics3.FactSource;
import mpi.tools.javatools.filehandlers.UTF8Reader;

import java.io.File;
import java.net.MalformedURLException;
/**
* Created by gadelrab on 2/11/16.
Expand All @@ -17,46 +18,45 @@ public enum MapType{SUBJ_2_OBJ,PRED_OBJ_2_SUBJ}


public static Multimap<String,String> loadDataInMap(String filePath, String [] relations, MapType type) {
return loadDataInMap(new File(filePath) ,relations, type);
}
public static Multimap<String,String> loadDataInMap(File file, String [] relations, MapType type) {
// add them to set for searching
ImmutableSet<String> relationsSet=null;
if (relations!=null)
relationsSet=ImmutableSet.copyOf(relations);

Multimap<String,String> subjectObjectMap= HashMultimap.create();

UTF8Reader fileReader;
try {



//String line;
//String line;

for(Fact f: FactSource.from(file))
if(relationsSet==null||relationsSet.contains(f.getRelation())){
String key=null;
String value=null;
switch (type){
case SUBJ_2_OBJ:
key=f.getSubject();
value=f.getObject();
break;
case PRED_OBJ_2_SUBJ:
key=f.getRelation()+"\t"+f.getObject();
value=f.getSubject();
break;
}
subjectObjectMap.put(key,value);}

System.out.println( "Dictionary size: "+ subjectObjectMap.size());

for(Fact f: FactSource.from(filePath))
if(relationsSet==null||relationsSet.contains(f.getRelation())){
String key=null;
String value=null;
switch (type){
case SUBJ_2_OBJ:
key=f.getSubject();
value=f.getObject();
break;
case PRED_OBJ_2_SUBJ:
key=f.getRelation()+"\t"+f.getObject();
value=f.getSubject();
break;
}
subjectObjectMap.put(key,value);}
return subjectObjectMap;

System.out.println( "Dictionary size: "+ subjectObjectMap.size());

return subjectObjectMap;


} catch (MalformedURLException e2) {
e2.printStackTrace();
}

return null;

}

Expand Down

0 comments on commit 515b88d

Please sign in to comment.