📄 observationvectorreader.java
字号:
/* jahmm package - v0.3.1 *//* * Copyright (c) 2004, Jean-Marc Francois. * * This file is part of Jahmm. * Jahmm is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Jahmm is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jahmm; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package be.ac.ulg.montefiore.run.jahmm.io;import java.util.*;import java.io.*;import be.ac.ulg.montefiore.run.jahmm.*;/** * Reads an {@link be.ac.ulg.montefiore.run.jahmm.ObservationVector * ObservationVector} up to (and including) a semi-colon. * <p> * The format of this observation is an opening bracket (<tt>[</tt>) followed * by the components of the vector separated by spaces or tabs. Each * component is a number ([+-]?[0123456789]+[.]?[0123456789]*). * <p> * For example, reading * <pre>[76 45. -2.23];</pre> * creates an observation such as the one generated by * <code>new ObservationVector(new double[] {76., 45., -2.23});</code> */public class ObservationVectorReader extends ObservationReader { private int dimension; /** * Constructs a reader of {@link ObservationVector ObservationVector}. */ public ObservationVectorReader() { dimension = -1; } /** * Constructs a reader of {@link ObservationVector ObservationVector}. * Verifies the dimension of the observations read. * * @param dimension The dimension of each observation. */ public ObservationVectorReader(int dimension) { if (dimension <= 0) throw new IllegalArgumentException(); this.dimension = dimension; } /** * An {@link be.ac.ulg.montefiore.run.jahmm.ObservationInteger * ObservationInteger} reader, as explained in * {@link ObservationReader ObservationReader}. * * @param st A stream tokenizer. * @return An {@link be.ac.ulg.montefiore.run.jahmm.ObservationInteger * ObservationInteger}. */ public Observation read(StreamTokenizer st) throws IOException, FileFormatException { switch (st.nextToken()) { case '[': break; default: throw new FileFormatException("'[' expected"); } Vector values = new Vector(); loop: while(true) switch (st.nextToken()) { case StreamTokenizer.TT_NUMBER: values.add(new Double(st.nval)); break; case ']': if (values.size() == 0) throw new FileFormatException("Empty vector found"); break loop; default: throw new FileFormatException("Number or ']' expected"); } switch (st.nextToken()) { case ';': break; default: throw new FileFormatException("';' expected"); } if (dimension > 0 && values.size() != dimension) throw new FileFormatException("Bad observation: wrong dimension"); double[] doubleValues = new double[values.size()]; for (int i = 0; i < values.size(); i++) doubleValues[i] = ((Double) values.elementAt(i)).doubleValue(); return new ObservationVector(doubleValues); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -