📄 exampleformatter.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.example;
import java.util.List;
import java.util.LinkedList;
/** Formats an example as specified by the format string.
* The dollar sign '$' is an escape character. Squared brackets '[' and ']'
* have a special meaning. The following escape sequences are
* interpreted:
* <dl>
* <dt>$a[separator]:</dt><dd> All attributes separated by separator</dd>
* <dt>$s[separator][indexSeparator]:</dt><dd> Sparse format. For all non 0
* attributes the following strings are concatenated:
* the column index, the value of indexSeparator, the attribute value. Attributes
* are separated by separator.</dd>
* <dt>$v[name]:</dt><dd> The value of the named attribute (both regular and special attributes)</dd>
* <dt>$c[index]:</dt><dd> The value of the attribute with the given index in the example set</dd>
* <dt>$l:</dt><dd> The label</dd>
* <dt>$p:</dt><dd> The predicted label</dd>
* <dt>$i:</dt><dd> The id</dd>
* <dt>$w:</dt><dd> The weight</dd>
* <dt>$n:</dt><dd> The newline character</dd>
* <dt>$t:</dt><dd> The tabulator character</dd>
* <dt>$$:</dt><dd> The dollar sign</dd>
* <dt>$[:</dt><dd> The '[' character</dd>
* <dt>$]:</dt><dd> The ']' character</dd>
* </dl>
*
* @version $Id: ExampleFormatter.java,v 2.6 2004/09/17 12:57:38 ingomierswa Exp $
*/
public class ExampleFormatter {
/** Represents one piece of formatting. */
public static interface FormatCommand {
public String format(Example example);
}
public static class SimpleCommand implements FormatCommand {
private char command;
private String[] arguments;
private SimpleCommand(char command, String[] arguments) {
this.command = command;
if ((command != 'a') &&
(command != 's') &&
(command != 'l') &&
(command != 'p') &&
(command != 'i') &&
(command != 'w'))
throw new IllegalArgumentException("Unknown command: '"+command+"'");
switch (command) {
case 'a':
if (arguments.length == 0) arguments = new String[] { Example.SEPARATOR };
break;
case 's':
if (arguments.length == 0)
arguments = new String[] { Example.SEPARATOR, Example.SPARSE_SEPARATOR };
if (arguments.length == 1)
arguments = new String[] { arguments[0], Example.SPARSE_SEPARATOR };
break;
}
this.arguments = arguments;
}
public String format(Example example) {
switch (command) {
case 'a': return example.getAttributesAsString(arguments[0]);
case 's': return example.getAttributesAsSparseString(arguments[0], arguments[1]);
case 'l': return example.getLabelAsString();
case 'p': return example.getPredictedLabelAsString();
case 'i': return example.getId()+"";
case 'w': return example.getWeight()+"";
default: return command+"";
}
}
}
public static class ValueCommand implements FormatCommand {
private Attribute attribute;
public ValueCommand(char command, String[] arguments, ExampleSet exampleSet) {
if (arguments.length < 1)
throw new IllegalArgumentException("Command 'v' needs argument!");
switch (command) {
case 'v':
attribute = exampleSet.getAttribute(arguments[0]);
if (attribute == null)
throw new IllegalArgumentException("Unknown attribute: '"+arguments[0]+"'!");
break;
case 'c':
try {
attribute = exampleSet.getAttribute(Integer.parseInt(arguments[0]));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Argument for 'c' must be an integer!");
} catch (Throwable e) {
throw new IllegalArgumentException("Illegal column: '"+arguments[0]+"'!");
}
break;
default: throw new IllegalArgumentException("Illegal command for ValueCommand: '"+command+"'");
}
}
public String format(Example example) {
return example.getValueAsString(attribute);
}
}
public static class TextCommand implements FormatCommand {
private String text;
private TextCommand(String text) { this.text = text; }
public String format(Example example) { return text; }
}
/** The commands used subsequently to format the example. */
private FormatCommand[] formatCommands;
/** Constructs a new ExampleFormatter that executes the given array
* of formatting commands. The preferred way of creating an instance
* of ExampleFormatter is to {@link ExampleFormatter#compile(String, ExampleSet)}
* a format string. */
public ExampleFormatter(FormatCommand[] formatCommands) {
this.formatCommands = formatCommands;
}
/** Factory method that compiles a format string and creates an
* instance of ExampleFormatter. */
public static ExampleFormatter compile(String formatString, ExampleSet exampleSet) {
List commandList = new LinkedList();
compile(formatString, exampleSet, commandList);
FormatCommand[] commands = new FormatCommand[commandList.size()];
commandList.toArray(commands);
return new ExampleFormatter(commands);
}
/** Adds all commands to the <code>commandList</code>. */
private static void compile(String formatString, ExampleSet exampleSet, List commandList) {
int start = 0;
while (true) {
int tagStart = formatString.indexOf("$", start);
if (tagStart == -1) {
commandList.add(new TextCommand(formatString.substring(start)));
break;
}
if (tagStart == formatString.length()-1)
throw new IllegalArgumentException("Format string ends in '$'.");
commandList.add(new TextCommand(formatString.substring(start, tagStart)));
char command = formatString.charAt(tagStart+1);
if ((command == '$') || (command == '[') || (command == ']')) {
commandList.add(new TextCommand(""+command));
start = tagStart+2;
continue;
} else if (command == 'n') {
commandList.add(new TextCommand("\n"));
start = tagStart+2;
continue;
} else if (command == 't') {
commandList.add(new TextCommand("\t"));
start = tagStart+2;
continue;
}
start = tagStart+2;
List argumentList = new LinkedList();
while ((start < formatString.length()) &&
(formatString.charAt(start) == '[')) {
int end = formatString.indexOf(']', start);
if (end == -1) throw new IllegalArgumentException("Unclosed '['!");
argumentList.add(formatString.substring(start+1, end));
start = end+1;
}
String[] arguments = new String[argumentList.size()];
argumentList.toArray(arguments);
switch (command) {
case 'v':
case 'c':
commandList.add(new ValueCommand(command, arguments, exampleSet));
break;
default:
commandList.add(new SimpleCommand(command, arguments));
break;
}
}
}
/** Formats a single example. */
public String format(Example example) {
StringBuffer str = new StringBuffer();
for (int i = 0; i < formatCommands.length; i++) {
str.append(formatCommands[i].format(example));
}
return str.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -