⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sgml2tokensequence.java

📁 这是一个matlab的java实现。里面有许多内容。请大家慢慢捉摸。
💻 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.pipe;import edu.umass.cs.mallet.base.types.TokenSequence;import edu.umass.cs.mallet.base.types.Token;import edu.umass.cs.mallet.base.types.Instance;import edu.umass.cs.mallet.base.util.Lexer;import edu.umass.cs.mallet.base.util.CharSequenceLexer;import edu.umass.cs.mallet.base.util.MalletLogger;import java.io.*;import java.net.URI;import java.util.regex.*;import java.util.logging.Logger;/**	 Converts a string containing simple SGML tags into a dta TokenSequence of words,	 paired with a target TokenSequence containing the SGML tags in effect for each word.	 It does not handle nested SGML tags, nor gracefully handle malformed SGML.   @author Andrew McCallum <a href="mailto:mccallum@cs.umass.edu">mccallum@cs.umass.edu</a> */public class SGML2TokenSequence extends Pipe implements Serializable{    private static Logger logger = MalletLogger.getLogger (SGML2TokenSequence.class.getName());	Pattern sgmlPattern = Pattern.compile ("</?([^>]*)>");	CharSequenceLexer lexer;	String backgroundTag;		public SGML2TokenSequence (CharSequenceLexer lexer, String backgroundTag)	{		this.lexer = lexer;		this.backgroundTag = backgroundTag;	}	public SGML2TokenSequence (String regex, String backgroundTag)	{		this.lexer = new CharSequenceLexer (regex);		this.backgroundTag = backgroundTag;	}	public SGML2TokenSequence ()	{		this (new CharSequenceLexer(), "O");	}	public Instance pipe (Instance carrier)	{		TokenSequence dataTokens = new TokenSequence ();		TokenSequence targetTokens = new TokenSequence ();		CharSequence string = (CharSequence) carrier.getData();		String tag = backgroundTag;		String nextTag = backgroundTag;		Matcher m = sgmlPattern.matcher (string);		int textStart = 0;		int textEnd = 0;		int nextStart = 0;		boolean done = false;		logger.fine(sgmlPattern.pattern());		logger.finer(string.toString());		while (!done) {			done = !(m.find());			if (done)				textEnd = string.length()-1;			else {				String sgml = m.group();				logger.finer ("SGML = "+sgml);				int groupCount = m.groupCount();				logger.finer(Integer.toString (groupCount));				if (sgml.charAt(1) == '/')					nextTag = backgroundTag;				else{					//nextTag = m.group(0).intern();					nextTag = sgml.substring(1, sgml.length()-1);				}				logger.finer("nextTag: " + nextTag);				nextStart = m.end();  // m.end returns one beyond index of last match char				textEnd = m.start();  // String.subtring does not include index end				logger.finer ("Text start/end "+textStart+" "+textEnd);			}			if (textEnd - textStart > 0) {				logger.finer ("Tag = "+tag);				logger.finer ("Target = "+string.subSequence (textStart, textEnd));				lexer.setCharSequence (string.subSequence (textStart, textEnd));				while (lexer.hasNext()) {					dataTokens.add (new Token ((String) lexer.next()));					targetTokens.add (new Token (tag));				}			}			textStart = nextStart;			tag = nextTag;		}		carrier.setData(dataTokens);		carrier.setTarget(targetTokens);		carrier.setSource(dataTokens);		return carrier;	}	public static void main (String[] args)	{		try {			Pipe p = new SerialPipes (new Pipe[] {				new Input2CharSequence (),				new SGML2TokenSequence()//				new SGML2TokenSequence (new CharSequenceLexer (Pattern.compile (".")), "O")				});			for (int i = 0; i < args.length; i++) {				Instance carrier = new Instance (new File(args[i]), null, null, null, p);				TokenSequence data = (TokenSequence) carrier.getData();				TokenSequence target = (TokenSequence) carrier.getTarget();				logger.finer ("===");				logger.finer (args[i]);				for (int j = 0; j < data.size(); j++)					logger.finer (target.getToken(j).getText()+" "+data.getToken(j).getText());			}		} 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(sgmlPattern);		out.writeObject(lexer);		out.writeObject(backgroundTag);	}		private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {		int version = in.readInt ();		sgmlPattern = (Pattern) in.readObject();		lexer = (CharSequenceLexer) in.readObject();		backgroundTag = (String) in.readObject();	}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -