All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.mallet.pipe.CharSequence2TokenSequence Maven / Gradle / Ivy

Go to download

MALLET is a Java-based package for statistical natural language processing, document classification, clustering, topic modeling, information extraction, and other machine learning applications to text.

The newest version!
/* 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. */




/** 
   @author Andrew McCallum [email protected]
 */

package cc.mallet.pipe;


import java.io.*;
import java.util.regex.Pattern;

import cc.mallet.extract.StringSpan;
import cc.mallet.extract.StringTokenization;
import cc.mallet.types.Instance;
import cc.mallet.types.SingleInstanceIterator;
import cc.mallet.types.TokenSequence;
import cc.mallet.util.CharSequenceLexer;

/**
 *  Pipe that tokenizes a character sequence.  Expects a CharSequence
 *   in the Instance data, and converts the sequence into a token
 *   sequence using the given regex or CharSequenceLexer.  
 *   (The regex / lexer should specify what counts as a token.)
 */
public class CharSequence2TokenSequence extends Pipe implements Serializable
{
	CharSequenceLexer lexer;
	
	public CharSequence2TokenSequence (CharSequenceLexer lexer)
	{
		this.lexer = lexer;
	}

	public CharSequence2TokenSequence (String regex)
	{
		this.lexer = new CharSequenceLexer (regex);
	}

	public CharSequence2TokenSequence (Pattern regex)
	{
		this.lexer = new CharSequenceLexer (regex);
	}

	public CharSequence2TokenSequence ()
	{
		this (new CharSequenceLexer());
	}

	public Instance pipe (Instance carrier)
	{
		CharSequence string = (CharSequence) carrier.getData();
		lexer.setCharSequence (string);
		TokenSequence ts = new StringTokenization (string);
		while (lexer.hasNext()) {
			lexer.next();
			ts.add (new StringSpan (string, lexer.getStartOffset (), lexer.getEndOffset ()));
		}
		carrier.setData(ts);
		return carrier;
	}

	public static void main (String[] args)
	{
		try {
			for (int i = 0; i < args.length; i++) {
				Instance carrier = new Instance (new File(args[i]), null, null, null);
				SerialPipes p = new SerialPipes (new Pipe[] {
					new Input2CharSequence (),
					new CharSequence2TokenSequence(new CharSequenceLexer())});
				carrier = p.newIteratorFrom (new SingleInstanceIterator(carrier)).next();
				TokenSequence ts = (TokenSequence) carrier.getData();
				System.out.println ("===");
				System.out.println (args[i]);
				System.out.println (ts.toString());
			}
		} catch (Exception e) {
			System.out.println (e);
			e.printStackTrace();
		}
	}

	// Serialization 
	
	private static final long serialVersionUID = 1;
	private static final int CURRENT_SERIAL_VERSION = 0;
	
	private void writeObject (ObjectOutputStream out) throws IOException {
		out.writeInt(CURRENT_SERIAL_VERSION);
		out.writeObject(lexer);
	}
	
	private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
		int version = in.readInt ();
		lexer = (CharSequenceLexer) in.readObject();
	}


	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy