1 package ixa.kaflib;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7
8
9 public class Entity implements Relational, Serializable {
10
11 private static final long serialVersionUID = 1L;
12
13
14 private String eid;
15
16
17
18
19
20
21
22
23
24
25
26 private String type;
27
28
29 private boolean named;
30
31
32 private List<Span<Term>> references;
33
34
35 private List<ExternalRef> externalReferences;
36
37 Entity(String eid, List<Span<Term>> references) {
38 if (references.size() < 1) {
39 throw new IllegalStateException("Entities must contain at least one reference span");
40 }
41 if (references.get(0).size() < 1) {
42 throw new IllegalStateException("Entities' reference's spans must contain at least one target");
43 }
44 this.eid = eid;
45 this.references = references;
46 this.externalReferences = new ArrayList<ExternalRef>();
47 this.named = true;
48 }
49
50 Entity(Entity entity, HashMap<String, Term> terms) {
51 this.eid = entity.eid;
52 this.type = entity.type;
53
54 String id = entity.getId();
55 this.references = new ArrayList<Span<Term>>();
56 for (Span<Term> span : entity.getSpans()) {
57
58 List<Term> targets = span.getTargets();
59 List<Term> copiedTargets = new ArrayList<Term>();
60 for (Term term : targets) {
61 Term copiedTerm = terms.get(term.getId());
62 if (copiedTerm == null) {
63 throw new IllegalStateException("Term not found when copying " + id);
64 }
65 copiedTargets.add(copiedTerm);
66 }
67 if (span.hasHead()) {
68 Term copiedHead = terms.get(span.getHead().getId());
69 this.references.add(new Span<Term>(copiedTargets, copiedHead));
70 }
71 else {
72 this.references.add(new Span<Term>(copiedTargets));
73 }
74 }
75
76 this.externalReferences = new ArrayList<ExternalRef>();
77 for (ExternalRef externalRef : entity.getExternalRefs()) {
78 this.externalReferences.add(new ExternalRef(externalRef));
79 }
80 this.named = true;
81 }
82
83 public String getId() {
84 return eid;
85 }
86
87 void setId(String id) {
88 this.eid = id;
89 }
90
91 public boolean hasType() {
92 return type != null;
93 }
94
95 public String getType() {
96 return type;
97 }
98
99 public void setType(String type) {
100 this.type = type;
101 }
102
103 public boolean isNamed() {
104 return named;
105 }
106
107 public void setNamed(boolean named) {
108 this.named = named;
109 }
110
111
112 public List<Term> getTerms() {
113 return this.references.get(0).getTargets();
114 }
115
116
117 public void addTerm(Term term) {
118 this.references.get(0).addTarget(term);
119 }
120
121
122 public void addTerm(Term term, boolean isHead) {
123 this.references.get(0).addTarget(term, isHead);
124 }
125
126 public List<Span<Term>> getSpans() {
127 return this.references;
128 }
129
130 public void addSpan(Span<Term> span) {
131 this.references.add(span);
132 }
133
134 public ExternalRef getExternalRef(String resource) {
135 for (ExternalRef ref : externalReferences) {
136 if (ref.getResource().equalsIgnoreCase(resource)) {
137 return ref;
138 }
139 }
140 return null;
141 }
142
143 public List<ExternalRef> getExternalRefs() {
144 return externalReferences;
145 }
146
147 public void addExternalRef(ExternalRef externalRef) {
148 externalReferences.add(externalRef);
149 }
150
151 public void addExternalRefs(List<ExternalRef> externalRefs) {
152 externalReferences.addAll(externalRefs);
153 }
154
155 public String getSpanStr(Span<Term> span) {
156 String str = "";
157 for (Term term : span.getTargets()) {
158 if (!str.isEmpty()) {
159 str += " ";
160 }
161 str += term.getStr();
162 }
163 return str;
164 }
165
166 public String getStr() {
167 return getSpanStr(this.getSpans().get(0));
168 }
169
170
171 public List<List<Term>> getReferences() {
172 List<List<Term>> list = new ArrayList<List<Term>>();
173 for (Span<Term> span : this.references) {
174 list.add(span.getTargets());
175 }
176 return list;
177 }
178
179
180 public void addReference(List<Term> span) {
181 this.references.add(KAFDocument.<Term>list2Span(span));
182 }
183 }