1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.metadata.mpeg7;
24
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.Node;
28
29 import java.text.DecimalFormat;
30 import java.text.DecimalFormatSymbols;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Locale;
35
36
37
38
39 public class TextAnnotationImpl implements TextAnnotation {
40
41
42 private static DecimalFormatSymbols standardSymbols = new DecimalFormatSymbols(Locale.US);
43
44
45 protected float confidence = -1.0f;
46
47
48 protected float relevance = -1.0f;
49
50
51 protected String language = null;
52
53
54 protected List<KeywordAnnotation> keywordAnnotations = null;
55
56
57 protected List<FreeTextAnnotation> freeTextAnnotations = null;
58
59 static {
60 standardSymbols.setDecimalSeparator('.');
61 }
62
63
64
65
66
67
68
69
70
71
72
73 public TextAnnotationImpl(float confidence, float relevance, String language) {
74 this.confidence = confidence;
75 this.relevance = relevance;
76 this.language = language;
77 keywordAnnotations = new ArrayList<KeywordAnnotation>();
78 freeTextAnnotations = new ArrayList<FreeTextAnnotation>();
79 }
80
81
82
83
84
85
86
87 public void addKeyword(String keyword) {
88 addKeywordAnnotation(new KeywordAnnotationImpl(keyword));
89 }
90
91
92
93
94
95
96
97
98
99 public void addKeyword(String keyword, KeywordAnnotation.Type type) {
100 addKeywordAnnotation(new KeywordAnnotationImpl(keyword, type));
101 }
102
103
104
105
106
107 public void addKeywordAnnotation(KeywordAnnotation keywordAnnotation) {
108 keywordAnnotations.add(keywordAnnotation);
109 }
110
111
112
113
114
115
116
117 public void addFreeText(String text) {
118 addFreeTextAnnotation(new FreeTextAnnotationImpl(text));
119 }
120
121
122
123
124
125 public void addFreeTextAnnotation(FreeTextAnnotation freeTextAnnotation) {
126 freeTextAnnotations.add(freeTextAnnotation);
127 }
128
129
130
131
132 public float getConfidence() {
133 return confidence;
134 }
135
136
137
138
139 public String getLanguage() {
140 return language;
141 }
142
143
144
145
146 public float getRelevance() {
147 return relevance;
148 }
149
150
151
152
153 public Iterator<KeywordAnnotation> keywordAnnotations() {
154 return keywordAnnotations.iterator();
155 }
156
157
158
159
160 public Iterator<FreeTextAnnotation> freeTextAnnotations() {
161 return freeTextAnnotations.iterator();
162 }
163
164
165
166
167 public Node toXml(Document document) {
168 DecimalFormat format = new DecimalFormat("0.0");
169 format.setDecimalFormatSymbols(standardSymbols);
170 Element node = document.createElement("TextAnnotation");
171 if (confidence >= 0.0) {
172 node.setAttribute("confidence", format.format(confidence));
173 }
174 if (relevance >= 0.0) {
175 node.setAttribute("relevance", format.format(relevance));
176 }
177 if (language != null) {
178 node.setAttribute("xml:lang", language);
179 }
180
181
182 if (keywordAnnotations.size() > 0) {
183 Element kwAnnotationNode = document.createElement("KeywordAnnotation");
184 for (KeywordAnnotation annotation : keywordAnnotations) {
185 kwAnnotationNode.appendChild(annotation.toXml(document));
186 }
187 node.appendChild(kwAnnotationNode);
188 }
189
190
191 for (FreeTextAnnotation annotation : freeTextAnnotations) {
192 node.appendChild(annotation.toXml(document));
193 }
194
195 return node;
196 }
197
198 }