So I have the following code that takes the input of two arrays, and apply some queries to match elements from DBpediaClassesArray
with elements from YagoClassesArray
, then it returns the number of elements that are similar in the two ArrayLists.
public static void get_ND_Matches() throws IOException{
@SuppressWarnings("rawtypes")
List<String> DBpediaClassesArray = new ArrayList<String>();
List<String> YagoClassesArray = new ArrayList<String>();
DBpediaClassesArray = new ArrayList<String>( ClassesRetrieval.getDBpediaClasses());
YagoClassesArray = new ArrayList<String>( ClassesRetrieval.fileToArrayListYago());
String YagoClassWithMaxMatches="";
HashMap<String,Integer> mappingYagoLabelMaxValue = new HashMap<String,Integer>();
int NumberOfCommonInstances;
HashMap<String,ArrayList<String>> MappingYagoClassWithInstances = new HashMap<>();
for(String yagoClass:YagoClassesArray){
MappingYagoClassWithInstances.put(yagoClass, GetYagooClassInstances(yagoClass));
System.out.println("Done for : "+yagoClass );
}
for(String dbClass:DBpediaClassesArray){
ArrayList<String> DBpediaInstancesArray = GetDBpediaClassInstances(dbClass);
for(Map.Entry<String, ArrayList<String>> YagoMapEntry : MappingYagoClassWithInstances.entrySet()){
String yagoClass=YagoMapEntry.getKey();
Set<String> IntersectionSet =Sets.intersection(Sets.newHashSet(DBpediaInstancesArray), Sets.newHashSet(YagoMapEntry.getValue()));
//System.out.println(dbClass + " and "+ yagoClass+ " = "+ IntersectionSet.size());
NumberOfCommonInstances = IntersectionSet.size();
mappingYagoLabelMaxValue.put(yagoClass, NumberOfCommonInstances);
}
int MaximumValueOfIntersection=(Collections.max(mappingYagoLabelMaxValue.values()));
for(Entry<String, Integer> YagoLabelToValueEntry:mappingYagoLabelMaxValue.entrySet()){
if(YagoLabelToValueEntry.getValue()==MaximumValueOfIntersection && MaximumValueOfIntersection != 0){
YagoClassWithMaxMatches = YagoLabelToValueEntry.getKey();
}
if(MaximumValueOfIntersection==0){
YagoClassWithMaxMatches = "Nothing in yago";
}
}
System.out.println("-------------------------------");
System.out.println(dbClass+" Corresponds to "+ YagoClassWithMaxMatches+" with : "+MaximumValueOfIntersection);
System.out.println("-------------------------------");
}
}
This code returns for example:
Actor from DBPEDIA Corresponds to Yago_Actor
Album from DBPEDIA Corresponds to Yago_Album
SomeClass from DBPEDIA Corresponds to nothing in Yago
Etc..
Behind the scenes, this code uses getDBpediaClasses
and then applies GetDBpediaClassInstances
method to get an ArrayList
of results for each class. Each ArrayList resulted is then compared to another ArrayList
generated by GetYagooClassInstances
for each class of fileToArrayListYago()
.
Now, because of all the calculations made in the background (there are millions of elements in each array), this process takes hours to execute, because it runs each comparison after another, while it would be better to get them all done at once and then just get the YagoClassWithMaxMatches
after all of them have been computed.
I was thinking of concurrency, but I first wanted to see if there is an easier way.
Are there any suggestions to make the code run faster?