001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.mallet.dataset;
025
026 import java.util.Iterator;
027 import java.util.Map;
028
029 import org.jdmp.core.dataset.ClassificationDataSet;
030 import org.jdmp.core.sample.ClassificationSample;
031 import org.jdmp.mallet.sample.Instance2Sample;
032 import org.ujmp.core.collections.SoftHashMap;
033 import org.ujmp.core.interfaces.Wrapper;
034
035 import cc.mallet.types.FeatureVectorSequence;
036 import cc.mallet.types.Instance;
037 import cc.mallet.types.InstanceList;
038
039 public class InstanceList2DataSet extends ClassificationDataSet implements Wrapper<InstanceList> {
040 private static final long serialVersionUID = -4306499108417523670L;
041
042 private InstanceList instanceList = null;
043
044 private final Map<Integer, Instance2Sample> integerToSampleMap = new SoftHashMap<Integer, Instance2Sample>();
045
046 public InstanceList2DataSet(InstanceList instanceList) {
047 this.instanceList = instanceList;
048 }
049
050 public InstanceList getWrappedObject() {
051 return instanceList;
052 }
053
054 public void setWrappedObject(InstanceList object) {
055 this.instanceList = object;
056 }
057
058 public int getSampleCount() {
059 int count = 0;
060 Iterator<?> it = instanceList.iterator();
061 while (it.hasNext()) {
062 Instance instance = (Instance) it.next();
063 Object data = instance.getData();
064 if (data instanceof FeatureVectorSequence) {
065 FeatureVectorSequence fvs = (FeatureVectorSequence) data;
066 count += fvs.size();
067 }
068 }
069 return count;
070 }
071
072 public ClassificationSample getSample(int index) {
073 Instance2Sample s = integerToSampleMap.get(index);
074 if (s == null) {
075 int offset = 0;
076 Iterator<?> it = instanceList.iterator();
077 while (it.hasNext()) {
078 Instance instance = (Instance) it.next();
079 Object data = instance.getData();
080 if (data instanceof FeatureVectorSequence) {
081 FeatureVectorSequence fvs = (FeatureVectorSequence) data;
082 int size = fvs.size();
083 if (index - offset < size) {
084 s = new Instance2Sample(instance, index - offset);
085 integerToSampleMap.put(index, s);
086 return s;
087 } else {
088 offset += fvs.size();
089 }
090 }
091 }
092 }
093 return s;
094 }
095
096 }