예제 #1
0
        protected internal virtual bool AddOneUnaryRule(UnaryRule rule, IDictionary <string, TransducerGraph> graphs)
        {
            string parentString = stateIndex.Get(rule.parent);
            string childString  = stateIndex.Get(rule.child);

            if (IsSyntheticState(parentString))
            {
                string          topcat = GetTopCategoryOfSyntheticState(parentString);
                TransducerGraph graph  = GetGraphFromMap(graphs, topcat);
                double          output = SmartNegate(rule.Score());
                graph.AddArc(graph.GetStartNode(), parentString, childString, output);
                return(true);
            }
            else
            {
                if (IsSyntheticState(childString))
                {
                    // need to add Arc from synthetic state to endState
                    TransducerGraph graph  = GetGraphFromMap(graphs, parentString);
                    double          output = SmartNegate(rule.Score());
                    graph.AddArc(childString, parentString, End, output);
                    // parentString should the the same as endState
                    graph.SetEndNode(parentString);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
예제 #2
0
        protected internal virtual bool AddOneBinaryRule(BinaryRule rule, IDictionary <string, TransducerGraph> graphs)
        {
            // parent has to be synthetic in BinaryRule
            string parentString = stateIndex.Get(rule.parent);
            string leftString   = stateIndex.Get(rule.leftChild);
            string rightString  = stateIndex.Get(rule.rightChild);
            string source;
            string target;
            string input;
            string bracket = null;

            if (op.trainOptions.markFinalStates)
            {
                bracket = Sharpen.Runtime.Substring(parentString, parentString.Length - 1, parentString.Length);
            }
            // the below test is not necessary with left to right grammars
            if (IsSyntheticState(leftString))
            {
                source = leftString;
                input  = rightString + (bracket == null ? ">" : bracket);
            }
            else
            {
                if (IsSyntheticState(rightString))
                {
                    source = rightString;
                    input  = leftString + (bracket == null ? "<" : bracket);
                }
                else
                {
                    // we don't know what to do with this rule
                    return(false);
                }
            }
            target = parentString;
            double output = SmartNegate(rule.Score());
            // makes it a real  0 <= k <= infty
            string topcat = GetTopCategoryOfSyntheticState(source);

            if (topcat == null)
            {
                throw new Exception("can't have null topcat");
            }
            TransducerGraph graph = GetGraphFromMap(graphs, topcat);

            graph.AddArc(source, target, input, output);
            return(true);
        }