|
| 1 | +package edu.stanford.nlp.sempre.geo880; |
| 2 | + |
| 3 | +import edu.stanford.nlp.sempre.SemType; |
| 4 | +import edu.stanford.nlp.sempre.SemTypeHierarchy; |
| 5 | +import edu.stanford.nlp.sempre.TypeLookup; |
| 6 | +import fig.basic.IOUtils; |
| 7 | +import fig.basic.Option; |
| 8 | +import fig.basic.LogInfo; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +/** |
| 15 | + * Type lookup for the geo880 domain, Mostly for distinguishing locations and numbers. |
| 16 | + * We also use a type hierarchy provided by a file to match |location.us_state| and |location.location| etc. |
| 17 | + * Created by joberant on 05/12/2016. |
| 18 | + */ |
| 19 | +public class Geo880TypeLookup implements TypeLookup{ |
| 20 | + public static class Options { |
| 21 | + @Option(gloss = "Verbosity") public int verbose = 0; |
| 22 | + @Option(gloss = "A path to a file that specified the type hierarchy.") |
| 23 | + public String typeHierarchyPath; |
| 24 | + |
| 25 | + } |
| 26 | + public static Options opts = new Options(); |
| 27 | + public static final String LOCATION = "fb:location.location"; |
| 28 | + public static final String CITY = "fb:location.citytown"; |
| 29 | + public static final String STATE = "fb:location.us_state"; |
| 30 | + public static final String RIVER = "fb:location.river"; |
| 31 | + public static final String LAKE = "fb:location.lake"; |
| 32 | + public static final String MOUNTAIN = "fb:location.mountain"; |
| 33 | + public static final String COUNTRY = "fb:location.country"; |
| 34 | + |
| 35 | + public Geo880TypeLookup() { |
| 36 | + SemTypeHierarchy semTypeHierarchy = SemTypeHierarchy.singleton; |
| 37 | + if (opts.typeHierarchyPath != null) { |
| 38 | + try { |
| 39 | + for (String line : IOUtils.readLines(opts.typeHierarchyPath)) { |
| 40 | + String[] tokens = line.split("\\s+"); |
| 41 | + |
| 42 | + // Check the file only contains relations about supertypes. |
| 43 | + assert tokens[1].endsWith("included_types"); |
| 44 | + semTypeHierarchy.addSupertype(tokens[0], tokens[0]); |
| 45 | + semTypeHierarchy.addSupertype(tokens[2], tokens[2]); |
| 46 | + semTypeHierarchy.addSupertype(tokens[0], tokens[2]); |
| 47 | + } |
| 48 | + } catch (IOException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + throw new RuntimeException("Could not read lines from: " + opts.typeHierarchyPath); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public SemType getEntityType(String entity) { |
| 57 | + // Entites are of the form fb:state.florida. |
| 58 | + int colonIndex = entity.indexOf(':'); |
| 59 | + int dotIndex = entity.indexOf('.'); |
| 60 | + String type = entity.substring(colonIndex+1, dotIndex); |
| 61 | + |
| 62 | + if (type.equals("place")) { |
| 63 | + type = LOCATION; |
| 64 | + } |
| 65 | + else if (type.equals("city")) { |
| 66 | + type = CITY; |
| 67 | + } |
| 68 | + else if (type.equals("state")) { |
| 69 | + type = STATE; |
| 70 | + } |
| 71 | + else if (type.equals("river")) { |
| 72 | + type = RIVER; |
| 73 | + } |
| 74 | + else if (type.equals("lake")) { |
| 75 | + type = LAKE; |
| 76 | + } |
| 77 | + else if (type.equals("mountain")) { |
| 78 | + type = MOUNTAIN; |
| 79 | + } |
| 80 | + else if (type.equals("country")) { |
| 81 | + type = COUNTRY; |
| 82 | + } |
| 83 | + else { |
| 84 | + throw new RuntimeException("Illegal entity: " + entity); |
| 85 | + } |
| 86 | + SemType result = SemType.newUnionSemType(type); |
| 87 | + if (opts.verbose >= 1) { |
| 88 | + LogInfo.logs("Entity=%s, Type=%s", entity, result); |
| 89 | + } |
| 90 | + return result; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public SemType getPropertyType(String property) { |
| 95 | + // Properties are of the form fb:location.location.population. |
| 96 | + String arg1 = property.substring(0, property.lastIndexOf('.')); |
| 97 | + String suffix = property.substring(property.lastIndexOf('.') + 1); |
| 98 | + String arg2 = LOCATION; |
| 99 | + if (suffix.equals("density") || suffix.equals("elevation") || |
| 100 | + suffix.equals("population") || suffix.equals("size") || |
| 101 | + suffix.equals("area") || suffix.equals("length")) { |
| 102 | + arg2 = "fb:type.number"; |
| 103 | + } |
| 104 | + SemType result = SemType.newFuncSemType(arg2, arg1); |
| 105 | + if (opts.verbose >= 1) { |
| 106 | + LogInfo.logs("Property=%s, Type=%s", property, result); |
| 107 | + } |
| 108 | + return result; |
| 109 | + } |
| 110 | +} |
0 commit comments