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   /** The coreference layer creates clusters of term spans (which we call mentions) which share the same referent. For instance, “London” and “the capital city of England” are two mentions referring to the same entity. It is said that those mentions corefer. */
9   public class Coref implements Serializable {
10  
11      /** Coreference's ID (required) */
12      private String coid;
13  
14      /** (optional) */
15      private String type;
16  
17  	public String getCluster() {
18  		return cluster;
19  	}
20  
21  	public void setCluster(String cluster) {
22  		this.cluster = cluster;
23  	}
24  
25  	private String cluster;
26  
27      /** Mentions to the same entity (at least one required) */
28      private List<Span<Term>> mentions;
29  
30      /** External references (optional) */
31      private List<ExternalRef> externalReferences;
32  
33  
34      Coref(String coid, List<Span<Term>> mentions) {
35  	if (mentions.size() < 1) {
36  	    throw new IllegalStateException("Coreferences must contain at least one reference span");
37  	}
38  	if (mentions.get(0).size() < 1) {
39  	    throw new IllegalStateException("Coreferences' reference's spans must contain at least one target");
40  	}
41  	this.coid = coid;
42  	this.mentions = mentions;
43  	this.externalReferences = new ArrayList<ExternalRef>();
44      }
45  
46      Coref(Coref coref, HashMap<String, Term> terms) {
47  	this.coid = coref.coid;
48  	/* Copy references */
49  	String id = coref.getId();
50  	this.mentions = new ArrayList<Span<Term>>();
51  	for (Span<Term> span : coref.getSpans()) {
52  	    /* Copy span */
53  	    List<Term> targets = span.getTargets();
54  	    List<Term> copiedTargets = new ArrayList<Term>();
55  	    for (Term term : targets) {
56  		Term copiedTerm = terms.get(term.getId());
57  		if (copiedTerm == null) {
58  		    throw new IllegalStateException("Term not found when copying " + id);
59  		}
60  		copiedTargets.add(copiedTerm);
61  	    }
62  	    if (span.hasHead()) {
63  		Term copiedHead = terms.get(span.getHead().getId());
64  		this.mentions.add(new Span<Term>(copiedTargets, copiedHead));
65  	    }
66  	    else {
67  		this.mentions.add(new Span<Term>(copiedTargets));
68  	    }
69  	}
70      }
71  
72      public String getId() {
73  	return coid;
74      }
75  
76      void setId(String id) {
77  	this.coid = id;
78      }
79  
80      public boolean hasType() {
81  	return this.type != null;
82      }
83  
84      public boolean hasCluster() {
85  	return this.cluster != null;
86      }
87  
88      public String getType() {
89  	return this.type;
90      }
91  
92      public void setType(String type) {
93  	this.type = type;
94      }
95  
96      public List<ExternalRef> getExternalRefs() {
97  	return externalReferences;
98      }
99  
100     public void addExternalRef(ExternalRef externalRef) {
101 	externalReferences.add(externalRef);
102     }
103 
104     public void addExternalRefs(List<ExternalRef> externalRefs) {
105 	externalReferences.addAll(externalRefs);
106     }
107 
108     /** Returns the term targets of the first span. When targets of other spans are needed getReferences() method should be used. */ 
109     public List<Term> getTerms() {
110 	return this.mentions.get(0).getTargets();
111     }
112 
113     /** Adds a term to the first span. */
114     public void addTerm(Term term) {
115 	this.mentions.get(0).addTarget(term);
116     }
117 
118     /** Adds a term to the first span. */
119     public void addTerm(Term term, boolean isHead) {
120 	this.mentions.get(0).addTarget(term, isHead);
121     }
122 
123     public List<Span<Term>> getSpans() {
124 	return this.mentions;
125     }
126 
127     public void addSpan(Span<Term> span) {
128 	this.mentions.add(span);
129     }
130 
131     public String getSpanStr(Span<Term> span) {
132 	String str = "";
133 	for (Term term : span.getTargets()) {
134 	    if (!str.isEmpty()) {
135 		str += " ";
136 	    }
137 	    str += term.getStr();
138 	}
139 	return str;
140     }
141 
142     /** Deprecated */
143     public List<List<Target>> getReferences() {
144 	List<List<Target>> list = new ArrayList<List<Target>>();
145 	for (Span<Term> span : this.mentions) {
146 	    list.add(KAFDocument.span2TargetList(span));
147 	}
148 	return list;
149     }
150 
151     /** Deprecated */
152     public void addReference(List<Target> span) {
153 	this.mentions.add(KAFDocument.targetList2Span(span));
154     }
155 }