001/**
002 * Copyright (c) 2012, 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.hadoop.tools.twitter.utils;
031
032import java.io.IOException;
033import java.io.PrintWriter;
034import java.util.Scanner;
035
036import org.openimaj.io.ReadWriteableASCII;
037import org.openimaj.ml.timeseries.TimeSeriesArithmaticOperator;
038import org.openimaj.ml.timeseries.series.ConcreteTimeSeries;
039import org.openimaj.ml.timeseries.series.DoubleTimeSeries;
040import org.openimaj.ml.timeseries.series.DoubleTimeSeriesProvider;
041import org.openimaj.util.pair.IndependentPair;
042
043/**
044 * A time series of WordDFIDF instances
045 * 
046 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
047 * 
048 */
049public class WordDFIDFTimeSeries
050                extends ConcreteTimeSeries<WordDFIDF, WordDFIDFTimeSeries>
051                implements
052                TimeSeriesArithmaticOperator<WordDFIDF, WordDFIDFTimeSeries>,
053                DoubleTimeSeriesProvider,
054                ReadWriteableASCII
055{
056
057        @Override
058        public WordDFIDFTimeSeries newInstance() {
059                return new WordDFIDFTimeSeries();
060        }
061
062        @Override
063        public WordDFIDF zero() {
064                return new WordDFIDF();
065        }
066
067        /**
068         * An explicit assumption is made that {@link WordDFIDF} instances all come
069         * from the same period of time and therefore have the same total number of
070         * tweets and total number of word instances across time (i.e.
071         * {@link WordDFIDF#Ttf} and {@link WordDFIDF#Twf} remain untouched)
072         */
073        @Override
074        public WordDFIDF sum() {
075                final WordDFIDF ret = zero();
076                for (final WordDFIDF time : this.getData()) {
077                        ret.tf += time.tf;
078                        ret.wf += time.wf;
079                        ret.Ttf = time.Ttf;
080                        ret.Twf = time.Twf;
081                }
082                return ret;
083        }
084
085        @Override
086        public DoubleTimeSeries doubleTimeSeries() {
087                final long[] times = this.getTimes();
088                final double[] values = new double[times.length];
089                final WordDFIDF[] current = this.getData();
090                int i = 0;
091                for (final WordDFIDF wordDFIDF : current) {
092                        values[i++] = wordDFIDF.dfidf();
093                }
094                return new DoubleTimeSeries(times, values);
095        }
096
097        @Override
098        public void readASCII(Scanner in) throws IOException {
099                final int count = in.nextInt();
100                for (int i = 0; i < count; i++) {
101                        final WordDFIDF instance = new WordDFIDF();
102                        instance.readASCII(in);
103                        this.add(instance.timeperiod, instance);
104                }
105        }
106
107        @Override
108        public String asciiHeader() {
109                return "";
110        }
111
112        @Override
113        public void writeASCII(PrintWriter out) throws IOException {
114                out.print(this.size() + " ");
115                for (final IndependentPair<Long, WordDFIDF> i : this) {
116                        i.secondObject().writeASCII(out);
117                        out.print(" ");
118                }
119        }
120
121}