import java.io.BufferedReader; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringEscapeUtils; public class ValidateGold { public static void main(String[] args) throws Exception { if (args.length == 2) { checkData(args[0], args[1], false, null); }else if (args.length == 4 && args[2].equals("wn30")) { checkData(args[0], args[1], true, args[3]); }else exit(); System.out.println("Test finished."); } private static void exit(){ System.out.println("ValidateGold data_xml gold_key [wn30 dict_path]\n"); System.exit(0); } private static void checkData(String pathXML, String pathGoldKey, boolean wn, String dict_path) throws Exception { int numInstances_gold = 0; Map> map_id2GoldKeys = new HashMap>(); BufferedReader in = Files.newBufferedReader(Paths.get(pathGoldKey), Charset.forName("UTF-8")); String line = null; System.out.println("Reading gold_key file..."); while ((line = in.readLine()) != null) { numInstances_gold++; String goldKeys[] = line.split(" "); int prev=-1; String lemmaPrev = ""; Set golds = new HashSet<>(); for(int i=1;i, Set> map_lemmaPos2Keys = null; if(wn){ map_lemmaPos2Keys = new HashMap<>(); System.out.println("Loading dict file..."); in = Files.newBufferedReader(Paths.get(dict_path), Charset.forName("UTF-8")); while ((line = in.readLine()) != null) { Set golds = new HashSet<>(); String []wn_dict = line.split("\t"); for(int i=2;i(lemma, pos), golds); } in.close(); } int numLine=0; int numInstances_xml=0; System.out.println("Reading data_xml file..."); in = Files.newBufferedReader(Paths.get(pathXML), Charset.forName("UTF-8")); while ((line = in.readLine()) != null) { line = line.trim(); if(line.startsWith("")); Set goldKeys = map_id2GoldKeys.get(id); if(goldKeys==null){ System.err.println("instance id not found in gold file! line "+numLine+"\n"+line); System.exit(1); } if(wn){ String goldKey = goldKeys.iterator().next(); int posGold = Integer.parseInt(goldKey.substring(goldKey.lastIndexOf("%")+1, goldKey.indexOf(":"))); // WORDNET POS NUMBER: // 1 NOUN // 2 VERB // 3 ADJECTIVE // 4 ADVERB // 5 ADJECTIVE SATELLITE char posWN = 0; if(posGold==1){ posWN = 'n'; if(!pos.equals("NOUN") && !pos.equals("PROPN")){ System.err.println("pos in xml and gold file not equals! line "+numLine+"\n"+line); System.exit(1); } }else if(posGold==2){ posWN = 'v'; if(!pos.equals("VERB") && !pos.equals("AUX")){ System.err.println("pos in xml and gold file not equals! line "+numLine+"\n"+line); System.exit(1); } }else if(posGold==3 || posGold==5){ posWN = 'a'; if(!pos.equals("ADJ")){ System.err.println("pos in xml and gold file not equals! line "+numLine+"\n"+line); System.exit(1); } }else if(posGold==4){ posWN = 'r'; if(!pos.equals("ADV")){ System.err.println("pos in xml and gold file not equals! line "+numLine+"\n"+line); System.exit(1); } } Set possiblesGoldKeys = map_lemmaPos2Keys.get(new Pair(lemma, posWN)); if(possiblesGoldKeys==null){ System.err.println("lemma and pos not found in WN30! line "+numLine+"\n"+line); System.exit(1); } for(String s :goldKeys){ if(!possiblesGoldKeys.contains(s)){ System.err.println("gold key not retrievable from lemma and pos in WN3.0! line "+numLine+"\n"+line); System.exit(1); } } } numInstances_xml++; } numLine++; } in.close(); if(numInstances_xml!=numInstances_gold){ System.err.println("number of instances different between gold and data files"); System.exit(1); } } public static class Pair { private F first; private S second; public Pair(F first, S second) { this.first = first; this.second = second; } public void setFirst(F first) { this.first = first; } public void setSecond(S second) { this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((first == null) ? 0 : first.hashCode()); result = prime * result + ((second == null) ? 0 : second.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first == null) { if (other.first != null) return false; } else if (!first.equals(other.first)) return false; if (second == null) { if (other.second != null) return false; } else if (!second.equals(other.second)) return false; return true; } } }