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 public void addKeywordAnnotation(KeywordAnnotation keywordAnnotation) {
107 keywordAnnotations.add(keywordAnnotation);
108 }
109
110
111
112
113
114
115
116 public void addFreeText(String text) {
117 addFreeTextAnnotation(new FreeTextAnnotationImpl(text));
118 }
119
120
121
122
123 public void addFreeTextAnnotation(FreeTextAnnotation freeTextAnnotation) {
124 freeTextAnnotations.add(freeTextAnnotation);
125 }
126
127
128
129
130 public float getConfidence() {
131 return confidence;
132 }
133
134
135
136
137 public String getLanguage() {
138 return language;
139 }
140
141
142
143
144 public float getRelevance() {
145 return relevance;
146 }
147
148
149
150
151 public Iterator<KeywordAnnotation> keywordAnnotations() {
152 return keywordAnnotations.iterator();
153 }
154
155
156
157
158 public Iterator<FreeTextAnnotation> freeTextAnnotations() {
159 return freeTextAnnotations.iterator();
160 }
161
162
163
164
165 public Node toXml(Document document) {
166 DecimalFormat format = new DecimalFormat("0.0");
167 format.setDecimalFormatSymbols(standardSymbols);
168 Element node = document.createElement("TextAnnotation");
169 if (confidence >= 0.0)
170 node.setAttribute("confidence", format.format(confidence));
171 if (relevance >= 0.0)
172 node.setAttribute("relevance", format.format(relevance));
173 if (language != null)
174 node.setAttribute("xml:lang", language);
175
176
177 if (keywordAnnotations.size() > 0) {
178 Element kwAnnotationNode = document.createElement("KeywordAnnotation");
179 for (KeywordAnnotation annotation : keywordAnnotations) {
180 kwAnnotationNode.appendChild(annotation.toXml(document));
181 }
182 node.appendChild(kwAnnotationNode);
183 }
184
185
186 for (FreeTextAnnotation annotation : freeTextAnnotations) {
187 node.appendChild(annotation.toXml(document));
188 }
189
190 return node;
191 }
192
193 }