📄 attributeparser.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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 edu.udo.cs.yale.generator.FeatureGenerator;import edu.udo.cs.yale.generator.GenerationException;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.tools.LogService;import java.io.File;import java.io.Reader;import java.io.FileReader;import java.io.BufferedReader;import java.io.IOException;import java.util.Collection;import java.util.HashSet;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;/** * Parses a file containing construction descriptions and adds the new attributes to the example set. * * @author simon, ingo * @version $Id: AttributeParser.java,v 2.4 2003/05/06 20:02:10 fischer Exp $ */public class AttributeParser { /** Maps construction descriptions of all generated attributes (including intermediate attributes) * to the attributes. */ private Map allAttributes = new HashMap();; /** A list of the newly generated attributes specified in the file.*/ private List newAttributes = new LinkedList(); /** The example table to which the attributes should be added. */ private ExampleTable exampleTable; public AttributeParser(ExampleTable et) { this.exampleTable = et; for (int i = 0; i < exampleTable.getNumberOfAttributes(); i++) { addAttribute(exampleTable.getAttribute(i)); } } /** Returns all attributes that must be generated. */ private Collection getAttributes() { return allAttributes.values(); } /** Returns a list of all parsed attributes. */ public List getNewAttributes() { return newAttributes; } /** Parses one line. */ public Attribute parse(String string) throws GenerationException { Attribute att = parseAttribute(string); newAttributes.add(att); return att; } /** Parses all lines. */ public void parseAll(Reader reader) throws IOException, GenerationException { BufferedReader in = new BufferedReader(reader); String line = null; while ((line = in.readLine()) != null) parse(line); in.close(); } /** Adds a new attribute to the map. If the map already contains an attribute with the same construction * description, this attribute is returned. */ private Attribute addAttribute(Attribute a) { Attribute oldAtt = (Attribute)allAttributes.get(a.getConstructionDescription()); if (oldAtt != null) { return oldAtt; } else { allAttributes.put(a.getConstructionDescription(), a); return a; } } private static int getClosingBracketIndex(String string, int startIndex) throws GenerationException { int openCount = 1; while (true) { int nextOpen = string.indexOf("(", startIndex+1); int nextClosing = string.indexOf(")", startIndex+1); if (nextClosing == -1) throw new GenerationException("Malformed attribute description: mismatched parantheses"); if ((nextOpen != -1) && (nextOpen < nextClosing)) { openCount++; startIndex = nextOpen; } else { openCount--; startIndex = nextClosing; } if (openCount == 0) { return nextClosing; } } } private Attribute parseAttribute(String string) throws GenerationException { Attribute[] attributes = parseAttributes(string); if (attributes.length != 1) throw new GenerationException("Malformed function description: too many attributes"); return attributes[0]; } /** Recursively parses the string starting at the current position. */ private Attribute[] parseAttributes(String string) throws GenerationException { List attributes = new LinkedList(); int start = 0; while (start < string.length()) { int leftBr = string.indexOf("(", start); int comma = string.indexOf(",", start); if ((comma == -1) && (leftBr == -1)) { int end = string.length(); attributes.add(addAttribute(new Attribute(string.substring(start, end).trim()))); start = string.length(); } else if ((leftBr == -1) || (comma < leftBr)) { int end = comma; attributes.add(addAttribute(new Attribute(string.substring(start, end).trim()))); start = end+1; } else { int rightBr = getClosingBracketIndex(string, leftBr); String functionName = string.substring(0, leftBr).trim(); Attribute[] arguments = parseAttributes(string.substring(leftBr+1, rightBr).trim()); attributes.add(addAttribute(new Attribute(functionName, arguments))); start = string.indexOf(",", rightBr)+1; if (start == 0) start = string.length(); } } Attribute[] attributeArray = new Attribute[attributes.size()]; attributes.toArray(attributeArray); return attributeArray; } /** Returns a collection of feature generators that can generate the attributes that are * not yet generated. The attributes are then removed from the map. */ private Collection applicableGenerators() { Set generators = new HashSet(); Iterator i = allAttributes.values().iterator(); while (i.hasNext()) { Attribute a = (Attribute)i.next(); if (exampleTable.getAttribute(a) == null) { if (argumentsAlreadyGenerated(a)) { FeatureGenerator fg = FeatureGenerator.createGeneratorForFunction(a.getFunctionName()); if (fg != null) { Attribute[] args = a.getArguments(); for (int n = 0 ; n < args.length; n++) args[n] = exampleTable.getAttribute(args[n]); fg.setArguments(args); generators.add(fg); i.remove(); } } } else { i.remove(); } } return generators; } /** Returns true if the example set already contains <tt>a</tt>. */ private boolean argumentsAlreadyGenerated(Attribute a) { Attribute[] arguments = a.getArguments(); if (arguments == null) return true; for (int i = 0; i < arguments.length; i++) { if (exampleTable.getAttribute(arguments[i]) == null) return false; } return true; } /** Generates new attributes as long as it is possible. */ public void generateAll(ExampleSet exampleSet) throws GenerationException { List allGeneratedAttributes = new LinkedList(); Collection generators; while ((generators = applicableGenerators()).size() > 0) { // as a side effect, generated attributes are removed from allAttributes allGeneratedAttributes.addAll(FeatureGenerator.generateAll(exampleTable, generators)); } Iterator i = allAttributes.values().iterator(); while (i.hasNext()) { LogService.logMessage("Could not generate attribute "+i.next(), LogService.ERROR); } if (allAttributes.size() > 0) throw new GenerationException("Couldn't generate all attibutes."); // replace generated attribute names by user specified names and add the new // attributes to the example set if (exampleSet != null) { i = newAttributes.iterator(); while (i.hasNext()) { Attribute attribute = (Attribute)i.next(); int realIndex = allGeneratedAttributes.indexOf(attribute); Attribute realAttribute = null; if (realIndex != -1) { realAttribute = (Attribute)allGeneratedAttributes.get(realIndex); realAttribute.setName(attribute.getName()); } else { realAttribute = attribute; } try { exampleSet.addAttribute(realAttribute); } catch (RuntimeException e) { LogService.logMessage("Cannot add attribute: "+e, LogService.WARNING); } } } allGeneratedAttributes.removeAll(newAttributes); LogService.logMessage("Removing "+allGeneratedAttributes.size()+" intermediate attributes.", LogService.MINIMUM); i = allGeneratedAttributes.iterator(); while (i.hasNext()) { exampleTable.removeAttribute((Attribute)i.next()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -