📄 csv2vectors.java
字号:
/* Copyright (C) 2002 Univ. of Massachusetts Amherst, Computer Science Dept. This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit). http://www.cs.umass.edu/~mccallum/mallet This software is provided under the terms of the Common Public License, version 1.0, as published by http://www.opensource.org. For further information, see the file `LICENSE' included with this distribution. */package edu.umass.cs.mallet.base.classify.tui;import edu.umass.cs.mallet.base.types.*;import edu.umass.cs.mallet.base.classify.*;import edu.umass.cs.mallet.base.pipe.*;import edu.umass.cs.mallet.base.pipe.iterator.*;import edu.umass.cs.mallet.base.util.*;import java.util.logging.*;import java.util.regex.*;import java.io.*;/** * Convert comma-separated-value files into vectors (persistent instance list). @author Andrew McCallum <a href="mailto:mccallum@cs.umass.edu">mccallum@cs.umass.edu</a> */public class Csv2Vectors{ private static Logger logger = MalletLogger.getLogger(Csv2Vectors.class.getName()); static CommandOption.File inputFile = new CommandOption.File (Csv2Vectors.class, "input", "FILE", true, null, "The file containing data to be classified, one instance per line", null); static CommandOption.File outputFile = new CommandOption.File (Csv2Vectors.class, "output", "FILE", true, new File("text.vectors"), "Write the instance list to this file; Using - indicates stdout.", null); static CommandOption.String lineRegex = new CommandOption.String (Csv2Vectors.class, "line-regex", "REGEX", true, "^(\\S*)[\\s,]*(\\S*)[\\s,]*(.*)$", "Regular expression containing regex-groups for label, name and data.", null); static CommandOption.Integer labelOption = new CommandOption.Integer (Csv2Vectors.class, "label", "INTEGER", true, 2, "The index of the group containing the label string.", null); static CommandOption.Integer nameOption = new CommandOption.Integer (Csv2Vectors.class, "name", "INTEGER", true, 1, "The index of the group containing the instance name.", null); static CommandOption.Integer dataOption = new CommandOption.Integer (Csv2Vectors.class, "data", "INTEGER", true, 3, "The index of the group containing the data.", null); static CommandOption.File usePipeFromVectorsFile = new CommandOption.File (Csv2Vectors.class, "use-pipe-from", "FILE", true, new File("text.vectors"), "Use the pipe and alphabets from a previously created vectors file. " + "Allows the creation, for example, of a test set of vectors that are" + "compatible with a previously created set of training vectors", null); public static void main (String[] args) throws FileNotFoundException, IOException { // Process the command-line options CommandOption.setSummary (Csv2Vectors.class, "A tool for creating instance lists of feature vectors from comma-separated-values"); CommandOption.process (Csv2Vectors.class, args); // Print some helpful messages for error cases if (args.length == 0) { CommandOption.getList(Csv2Vectors.class).printUsage(false); System.exit (-1); } if (inputFile == null) { System.err.println ("You must include `--input FILE ...' in order to specify a"+ "file containing the instances, one per line."); System.exit (-1); } Pipe instancePipe; InstanceList previousInstanceList = null; if (!usePipeFromVectorsFile.wasInvoked()) { instancePipe = new SerialPipes (new Pipe[] { new Target2Label (), new CharSequence2TokenSequence (), new TokenSequence2FeatureSequence(), new FeatureSequence2AugmentableFeatureVector(), //new PrintInputAndTarget () }); } else { previousInstanceList = InstanceList.load (usePipeFromVectorsFile.value); instancePipe = previousInstanceList.getPipe(); } InstanceList ilist = new InstanceList (instancePipe); FileReader fileReader = new FileReader (inputFile.value); ilist.add (new CsvIterator (fileReader, Pattern.compile(lineRegex.value), dataOption.value, labelOption.value, nameOption.value)); ObjectOutputStream oos; if (outputFile.value.toString().equals ("-")) oos = new ObjectOutputStream(System.out); else oos = new ObjectOutputStream(new FileOutputStream(outputFile.value)); oos.writeObject(ilist); oos.close(); // *rewrite* vector file used as source of pipe in case we changed the alphabet(!) if (usePipeFromVectorsFile.wasInvoked()){ System.out.println(" output usepipe ilist pipe instance id =" + previousInstanceList.getPipe().getInstanceId()); oos = new ObjectOutputStream(new FileOutputStream(usePipeFromVectorsFile.value)); oos.writeObject(previousInstanceList); oos.close(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -