001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.annotation;
031
032import java.util.ArrayList;
033import java.util.Collection;
034import java.util.HashMap;
035import java.util.List;
036import java.util.Map;
037
038import org.openimaj.data.dataset.GroupedDataset;
039import org.openimaj.data.dataset.ListDataset;
040
041/**
042 * Basic implementation of {@link Annotated}.
043 *
044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
045 *
046 * @param <OBJECT>
047 *            Type of object.
048 * @param <ANNOTATION>
049 *            Type of annotations
050 */
051public class AnnotatedObject<OBJECT, ANNOTATION> implements Annotated<OBJECT, ANNOTATION> {
052        /**
053         * The annotated object
054         */
055        public OBJECT object;
056
057        /**
058         * The annotations
059         */
060        public Collection<ANNOTATION> annotations;
061
062        /**
063         * Construct with the given object and its annotations.
064         *
065         * @param object
066         *            the object
067         * @param annotations
068         *            the objects annotations.
069         */
070        public AnnotatedObject(OBJECT object, Collection<ANNOTATION> annotations) {
071                this.object = object;
072                this.annotations = annotations;
073        }
074
075        /**
076         * Construct with the given object and its annotation.
077         *
078         * @param object
079         *            the object
080         * @param annotation
081         *            the object's annotation.
082         */
083        public AnnotatedObject(OBJECT object, ANNOTATION annotation) {
084                this.object = object;
085
086                this.annotations = new ArrayList<ANNOTATION>();
087                annotations.add(annotation);
088        }
089
090        @Override
091        public OBJECT getObject() {
092                return object;
093        }
094
095        @Override
096        public Collection<ANNOTATION> getAnnotations() {
097                return annotations;
098        }
099
100        /**
101         * Create an {@link AnnotatedObject} with the given object and its
102         * annotations.
103         *
104         * @param <OBJECT>
105         *            Type of object.
106         * @param <ANNOTATION>
107         *            Type of annotations
108         * @param object
109         *            the object
110         * @param annotations
111         *            the objects annotations.
112         * @return the new {@link AnnotatedObject}
113         */
114        public static <OBJECT, ANNOTATION> AnnotatedObject<OBJECT, ANNOTATION> create(OBJECT object,
115                        Collection<ANNOTATION> annotations)
116                        {
117                return new AnnotatedObject<OBJECT, ANNOTATION>(object, annotations);
118                        }
119
120        /**
121         * Create an {@link AnnotatedObject} with the given object and its
122         * annotation.
123         *
124         * @param <OBJECT>
125         *            Type of object.
126         * @param <ANNOTATION>
127         *            Type of annotations
128         * @param object
129         *            the object
130         * @param annotation
131         *            the object's annotation.
132         * @return the new {@link AnnotatedObject}
133         */
134        public static <OBJECT, ANNOTATION> AnnotatedObject<OBJECT, ANNOTATION> create(OBJECT object, ANNOTATION annotation) {
135                return new AnnotatedObject<OBJECT, ANNOTATION>(object, annotation);
136        }
137
138        /**
139         * Convert a grouped dataset to a list of annotated objects. The annotations
140         * correspond to the type of group. If the same object appears in multiple
141         * groups within the dataset then it will have multiple annotations.
142         *
143         * @param <OBJECT>
144         *            Type of object.
145         * @param <ANNOTATION>
146         *            Type of annotations.
147         * @param dataset
148         *            the dataset
149         * @return the list of annotated instances
150         */
151        public static <OBJECT, ANNOTATION> List<AnnotatedObject<OBJECT, ANNOTATION>> createList(
152                        GroupedDataset<ANNOTATION, ? extends ListDataset<OBJECT>, OBJECT> dataset)
153                        {
154                final Map<OBJECT, AnnotatedObject<OBJECT, ANNOTATION>> annotated = new HashMap<OBJECT, AnnotatedObject<OBJECT, ANNOTATION>>(
155                                dataset.numInstances());
156
157                for (final ANNOTATION grp : dataset.getGroups()) {
158                        for (final OBJECT inst : dataset.getInstances(grp)) {
159                                final AnnotatedObject<OBJECT, ANNOTATION> ao = annotated.get(inst);
160
161                                if (ao == null)
162                                        annotated.put(inst, new AnnotatedObject<OBJECT, ANNOTATION>(inst, grp));
163                                else
164                                        ao.annotations.add(grp);
165                        }
166                }
167
168                return new ArrayList<AnnotatedObject<OBJECT, ANNOTATION>>(annotated.values());
169                        }
170
171        /**
172         * Convert parallel arrays of objects and annotations to a list of
173         * {@link AnnotatedObject}.
174         *
175         * @param objs
176         *            the objects
177         * @param anns
178         *            the annotation for each object (assumes 1 annotation per
179         *            object).
180         * @return the list
181         */
182        public static <OBJECT, ANNOTATION> List<AnnotatedObject<OBJECT, ANNOTATION>> createList(OBJECT[] objs,
183                        ANNOTATION[] anns)
184        {
185                final List<AnnotatedObject<OBJECT, ANNOTATION>> list = new ArrayList<AnnotatedObject<OBJECT, ANNOTATION>>();
186
187                for (int i = 0; i < objs.length; i++) {
188                        list.add(create(objs[i], anns[i]));
189                }
190
191                return list;
192        }
193
194        /**
195         * Convert parallel arrays of objects and annotations to a list of
196         * {@link AnnotatedObject}.
197         *
198         * @param objs
199         *            the objects
200         * @param anns
201         *            the annotations for each object.
202         * @return the list
203         */
204        public static <OBJECT, ANNOTATION> List<AnnotatedObject<OBJECT, ANNOTATION>> createList(OBJECT[] objs,
205                        ANNOTATION[][] anns)
206        {
207                final List<AnnotatedObject<OBJECT, ANNOTATION>> list = new ArrayList<AnnotatedObject<OBJECT, ANNOTATION>>();
208
209                for (int i = 0; i < objs.length; i++) {
210                        for (final ANNOTATION a : anns[i])
211                                list.add(create(objs[i], a));
212                }
213
214                return list;
215        }
216}