package OT; import java.util.*; /** * GEN. * @version 1999-01-09 * @author Andrea Heiberg, University of Arizona */ public class Gen extends Vector { protected Debug debug; protected Representation input; protected LanguageFeatureTypeSet languageFeatureTypeSet; public Gen(Representation input, LanguageFeatureTypeSet languageFeatureTypeSet, Debug debug) { this.input = input; this.languageFeatureTypeSet = languageFeatureTypeSet; this.debug = debug; } //end constructor public Vector getByFeatureType(FeatureType ft1, FeatureType ft2) { Vector v = new Vector(); if (ft1!=null) { String s1 = ft1.toString(); String s2 = ""; if (ft2!=null) s2 = ft2.toString(); for (Enumeration e=super.elements(); e.hasMoreElements();) { Operation o = (Operation)e.nextElement(); String os = o.featureType.toString(); if ((s1.equals(os)) || (s2.equals(os))) { v.addElement(o); } //end if } //end for } //end if return v; } //end getByFeatureType public void constructOperations() { Association association; Feature feature; FeatureTokenSet featureTokenSet = null; FeatureType featureType; int numTokens; Node root; Operation operation; Random randomToken = new Random(); String token; debug.write("input " + input.toOrthography() + ":" + "\n" + input.toString()); //add operations to delete existing root associations for (Enumeration e = input.rootAssociations.elements(); e.hasMoreElements();) { association = (Association)e.nextElement(); Feature f = (Feature)association.child; if (!(f.featureType.inert)) { DeleteAssociation deleteA = new DeleteAssociation(association); //create operation to delete feature super.addElement(deleteA); } //end if } //end for for (Enumeration e = input.featureTypeSet.elements(); e.hasMoreElements();) { featureTokenSet = (FeatureTokenSet)e.nextElement(); //add operation to delete each existing feature for (Enumeration f = featureTokenSet.elements(); f.hasMoreElements();) { feature = (Feature)f.nextElement(); if (!(feature.featureType.inert)) { DeleteFeature deleteF = new DeleteFeature(feature); //create operation to delete feature super.addElement(deleteF); //add operations to insert paths between feature and anchors constructInsertPath(feature); } //end if } //end for } //end for FeatureTokenSet f = null; //add operations to insert new feature tokens for (Enumeration e = languageFeatureTypeSet.elements(); e.hasMoreElements();) { featureType = (FeatureType)e.nextElement(); if (!(featureType.inert)) { Hashtable newTokens = new Hashtable(); int fsize = 0; if (input.featureTypeSet.containsKey(featureType.toString())) { //we already have some tokens of this feature f = (FeatureTokenSet)input.featureTypeSet.get(featureType.toString()); fsize = f.size(); } //end if //get anchors for this feature type float s = input.getAnchors(featureType.anchor).size(); //max number of tokens of F OCP will allow = (# of anchors/2) - # of existing tokens of F numTokens = Math.round(s/2) - fsize; for (int i=1; i<=numTokens; i++) { token = new String(new Integer(randomToken.nextInt()).toString()); //create new token string token = token.substring(token.length()-2); if (f!=null) { while (f.containsKey(token) || newTokens.containsKey(token)) { //make sure token is unique token = new String(new Integer(randomToken.nextInt()).toString()); token = token.substring(token.length()-2); } //end while } //end if newTokens.put(token, token); feature = new Feature(token, featureType); super.addElement(new InsertFeature(feature)); //add operations to insert paths between feature and anchors constructInsertPath(feature); } //end for } //end if } //end for } //end constructOperations private void constructInsertPath(Feature feature) { //add operation to insert path between feature and each anchor of anchorType NodeSet roots = null; String at = feature.featureType.anchor; if (at.equals("root")) { roots = input.rootNodes; } else { NodeSet anchors = input.getAnchors(at); roots = new NodeSet(); for (Enumeration z = anchors.elements(); z.hasMoreElements();) { Anchor a = (Anchor)z.nextElement(); while (!(a instanceof Root)) { ProsodicNode p = (ProsodicNode)a; //cast a = p.head; } //end while roots.put(a); } //end for } //end if for (Enumeration e = roots.elements(); e.hasMoreElements();) { Root root = (Root)e.nextElement(); Association a = new Association(root, feature); if (!(input.rootAssociations.containsKey(a.toString()))) { super.addElement(new InsertAssociation(a)); } //end if } //end for } //end constructInsertPath }