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
10 public class Chunk implements Serializable {
11
12
13 private String cid;
14
15
16 private String phrase;
17
18
19 private String chunkcase;
20
21
22 private Span<Term> span;
23
24 Chunk(String cid, Span<Term> span) {
25 if (span.size() < 1) {
26 throw new IllegalStateException("Chunks must contain at least one term target");
27 }
28 this.cid = cid;
29 this.span = span;
30 }
31
32 Chunk(Chunk chunk, HashMap<String, Term> terms) {
33 this.cid = chunk.cid;
34 this.phrase = chunk.phrase;
35 this.chunkcase = chunk.chunkcase;
36
37 String id = chunk.getId();
38 Span<Term> span = chunk.span;
39 List<Term> targets = span.getTargets();
40 List<Term> copiedTargets = new ArrayList<Term>();
41 for (Term term : targets) {
42 Term copiedTerm = terms.get(term.getId());
43 if (copiedTerm == null) {
44 throw new IllegalStateException("Term not found when copying " + id);
45 }
46 copiedTargets.add(copiedTerm);
47 }
48 if (span.hasHead()) {
49 Term copiedHead = terms.get(span.getHead().getId());
50 this.span = new Span<Term>(copiedTargets, copiedHead);
51 }
52 else {
53 this.span = new Span<Term>(copiedTargets);
54 }
55 }
56
57 public String getId() {
58 return cid;
59 }
60
61 void setId(String id) {
62 this.cid = id;
63 }
64
65 public boolean hasHead() {
66 return (span.getHead() != null);
67 }
68
69 public Term getHead() {
70 return span.getHead();
71 }
72
73 public boolean hasPhrase() {
74 return phrase != null;
75 }
76
77 public String getPhrase() {
78 return phrase;
79 }
80
81 public void setPhrase(String phrase) {
82 this.phrase = phrase;
83 }
84
85 public boolean hasCase() {
86 return chunkcase != null;
87 }
88
89 public String getCase() {
90 return chunkcase;
91 }
92
93 public void setCase(String chunkcase) {
94 this.chunkcase = chunkcase;
95 }
96
97 public List<Term> getTerms() {
98 return this.span.getTargets();
99 }
100
101 public void addTerm(Term term) {
102 this.span.addTarget(term);
103 }
104
105 public void addTerm(Term term, boolean isHead) {
106 this.span.addTarget(term, isHead);
107 }
108
109 public Span<Term> getSpan() {
110 return this.span;
111 }
112
113 public void setSpan(Span<Term> span) {
114 this.span = span;
115 }
116
117 public String getStr() {
118 String str = "";
119 for (Term term : this.span.getTargets()) {
120 if (!str.isEmpty()) {
121 str += " ";
122 }
123 str += term.getStr();
124 }
125 return str;
126 }
127
128
129 public void setHead(Term term) {
130 this.span.setHead(term);
131 }
132
133 }