1 package eu.fbk.dkm.pikes.query;
2
3 import java.util.Map;
4 import java.util.Objects;
5
6 import javax.annotation.Nullable;
7
8 import com.google.common.collect.ImmutableMap;
9 import com.google.common.collect.Ordering;
10
11 public final class Term implements Comparable<Term> {
12
13 private final String document;
14
15 private final Term.Layer layer;
16
17 private final String token;
18
19 private final Map<String, String> attributes;
20
21 private static Map<String, String> mapFor(final Object... attributeKeyValues) {
22 if (attributeKeyValues.length == 0) {
23 return ImmutableMap.of();
24 } else {
25 ImmutableMap.Builder<String, String> builder = null;
26 builder = ImmutableMap.builder();
27 for (int i = 0; i < attributeKeyValues.length; i += 2) {
28 builder.put(attributeKeyValues[i].toString(), attributeKeyValues[i + 1].toString());
29 }
30 return builder.build();
31 }
32 }
33
34 public Term(final String document, final Term.Layer layer, final String token,
35 final Object... attributeKeyValues) {
36 this(document, layer, token, mapFor(attributeKeyValues));
37 }
38
39 public Term(final String document, final Term.Layer layer, final String token,
40 @Nullable final Map<String, String> attributes) {
41 this.document = Objects.requireNonNull(document);
42 this.layer = Objects.requireNonNull(layer);
43 this.token = Objects.requireNonNull(token);
44 this.attributes = attributes == null ? ImmutableMap.of() : ImmutableMap.copyOf(attributes);
45 }
46
47 public String getDocument() {
48 return this.document;
49 }
50
51 public Term.Layer getLayer() {
52 return this.layer;
53 }
54
55 public String getToken() {
56 return this.token;
57 }
58
59 public Map<String, String> getAttributes() {
60 return this.attributes;
61 }
62
63 @Override
64 public int compareTo(final Term other) {
65 int result = this.document.compareTo(other.document);
66 if (result == 0) {
67 result = this.layer.compareTo(other.layer);
68 if (result == 0) {
69 result = this.token.compareTo(other.token);
70 if (result == 0) {
71 result = this.attributes.hashCode() - other.attributes.hashCode();
72 }
73 }
74 }
75 return result;
76 }
77
78 @Override
79 public boolean equals(final Object object) {
80 if (object == this) {
81 return true;
82 }
83 if (!(object instanceof Term)) {
84 return false;
85 }
86 final Term other = (Term) object;
87 return this.layer == other.layer && this.document.equals(other.document)
88 && this.token.equals(other.token) && this.attributes.equals(other.attributes);
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hash(this.document, this.layer, this.token, this.attributes);
94 }
95
96 @Override
97 public String toString() {
98 final StringBuilder builder = new StringBuilder();
99 builder.append(this.document);
100 builder.append("\t");
101 builder.append(this.layer.getID());
102 builder.append("\t");
103 builder.append(this.token);
104 if (!this.attributes.isEmpty()) {
105 for (final String key : Ordering.natural().sortedCopy(this.attributes.keySet())) {
106 builder.append("\t");
107 builder.append(key);
108 builder.append("=");
109 builder.append(this.attributes.get(key));
110 }
111 }
112 return builder.toString();
113 }
114
115 public static enum Layer {
116
117 RAW("raw"),
118
119 STEM_TEXT("stem.text"),
120
121 STEM_SYNONYM("stem.synonym"),
122
123 STEM_RELATED("stem.related"),
124
125 STEM_SUBWORD("stem.subword"),
126
127 LEMMA_TEXT("lemma.text"),
128
129 LEMMA_SYNONYM("lemma.synonym"),
130
131 LEMMA_RELATED("lemma.related"),
132
133 SYNSET_SPECIFIC("synset.specific"),
134
135 SYNSET_RELATED("synset.related"),
136
137 SYNSET_HYPERNYN("synset.hypernym"),
138
139 URI_DBPEDIA("uri.dbpedia"),
140
141 URI_CUSTOM("uri.custom"),
142
143 URI_RELATED("uri.related"),
144
145 TYPE_YAGO("type.yago"),
146
147 TYPE_SUMO("type.sumo"),
148
149 PREDICATE_FRB("predicate.frb"),
150
151 PREDICATE_PB("predicate.pb"),
152
153 PREDICATE_NB("predicate.nb"),
154
155 ROLE_FRB("role.frb"),
156
157 ROLE_PB("role.pb"),
158
159 ROLE_NB("role.nb"),
160
161 CONCEPT("concept");
162
163 private final String id;
164
165 private Layer(final String id) {
166 this.id = id;
167 }
168
169 public String getID() {
170 return this.id;
171 }
172
173 }
174
175 }