package DOPParser;
/*
 * Target_Grammar.java
 *
 * Created on 9 december 2005, 1:42
 *
*/

/**
 *
 * @author gideon
 */


import java.util.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;


public class Target_Grammar extends Grammar{
    
    /**
     *  Reads in the rules of the artificial grammar from a text file.
     *  @param fileName Name of the grammar file.
     */
    public Target_Grammar(String fileName) throws Exception{
        
        //create TOP, and add to list
        NonTerminal myNonTerminal = new NonTerminal("TOP");
        this.nonTerminals.put("TOP", myNonTerminal);
        
        //Initialize();
        
        BufferedReader buff;
        String[] mySentence ;
        
        buff = new BufferedReader(new FileReader(fileName));
        
        String completeSentence;
        while ((completeSentence = buff.readLine()) !=null){ 
            
            //break into words
            
            mySentence = completeSentence.split("\\s");
            
            addRule(mySentence);            
        }   //next line of buffer
        
        buff.close();
    }
    
    /** Adds a nonterminal plus a rule to the grammar from the given sentence.
     *  @param sentence Words in the sentence.<BR/>
     *  It is assumed that the first word in the sentence is the lhs of the rule
     * and the other words are the rhs. lower case are assumed to be Terminals, and upper case nonTerminals.
     */
    protected void addRule(String[] sentence){
        
        NonTerminal mylhsNonTerminal, myrhsNonTerminal; 
        Constituent myConstituent;
        ArrayList<Constituent> rhsArrayofConstituents = new ArrayList<Constituent>();
        
        //first word is nonTerminal (lhs of rule) : create one if it doesn't exist already
        //check if nonTerminal already exists
        if (this.nonTerminals.get(sentence[0])== null){
            
            //create nonTerminal with fixed name and without a rule
            mylhsNonTerminal = new NonTerminal(sentence[0]);
            //add it to HashMap of nonTerminals of the grammar
            this.nonTerminals.put(sentence[0], mylhsNonTerminal);
        }
        else mylhsNonTerminal = this.nonTerminals.get(sentence[0]);
               
        for (int i = 1; i < sentence.length ; i++) {
            //turn the word either into Terminal or into nonTerminal according to uppercase/lowercase
            String tempString = new String(sentence[i]);
            
            if (sentence[i].equals(tempString.toLowerCase()) ){
                //turn into terminal
                Terminal myTerminal = new Terminal(sentence[i]);
                //if Terminal is not yet in Arraylist of grammar then add it
                if (this.Terminals.containsKey(sentence[i]))   this.Terminals.put(sentence[i],myTerminal);
                //keep track of array of constituents of this sentence
                rhsArrayofConstituents.add(myTerminal); 
            }
            else{   //nonTerminals

                //if nonTerminal is not yet in HashMap of grammar then add it
                if (this.nonTerminals.get(sentence[i])== null){
                    
                    //create nonTerminal with fixed name and without a rule
                    myrhsNonTerminal = new NonTerminal(sentence[i]);
                    //add it to HashMap of nonTerminals of the grammar
                    //??? XXX NO: if there is mistake in grammar this would create nonTerminal w/o rules
                    this.nonTerminals.put(sentence[i], myrhsNonTerminal);
                }
                else myrhsNonTerminal = this.nonTerminals.get(sentence[i]);

                 //keep track of array of constituents of this sentence
                 rhsArrayofConstituents.add(myrhsNonTerminal); 
                }
            }
        
        // add rule to lhs nonTerminal
        mylhsNonTerminal.addRule(rhsArrayofConstituents);
    }
    
}

