Ejemplo n.º 1
0
        internal override State <TData, TOffset> GenerateNfa(Fst <TData, TOffset> fsa, State <TData, TOffset> startState, out bool hasVariables)
        {
            hasVariables = false;
            ArcPriorityType        priorityType = IsGreedy ? ArcPriorityType.High : ArcPriorityType.Low;
            State <TData, TOffset> endState;
            State <TData, TOffset> currentState = startState;
            var startStates = new List <State <TData, TOffset> >();

            if (MinOccur == 0)
            {
                endState = startState.Arcs.Add(fsa.CreateState(), priorityType);
                endState = base.GenerateNfa(fsa, endState, out hasVariables);
                startStates.Add(currentState);
            }
            else
            {
                endState = startState;
                for (int i = 0; i < MinOccur; i++)
                {
                    currentState = endState;
                    endState     = base.GenerateNfa(fsa, currentState, out hasVariables);
                }
            }

            if (MaxOccur == Infinite)
            {
                endState.Arcs.Add(currentState, priorityType);
                if (MinOccur == 0)
                {
                    endState = fsa.CreateState();
                }
            }
            else
            {
                int numCopies = MaxOccur - MinOccur;
                if (MinOccur == 0)
                {
                    numCopies--;
                }
                for (int i = 1; i <= numCopies; i++)
                {
                    startStates.Add(endState);
                    endState = endState.Arcs.Add(fsa.CreateState(), priorityType);
                    endState = base.GenerateNfa(fsa, endState, out hasVariables);
                }
            }
            foreach (State <TData, TOffset> state in startStates)
            {
                state.Arcs.Add(endState);
            }

            return(endState);
        }
Ejemplo n.º 2
0
        public void IsDeterminizable()
        {
            var featSys = new FeatureSystem
            {
                new StringFeature("A"),
                new StringFeature("B"),
                new StringFeature("C"),
                new StringFeature("D"),
                new StringFeature("E"),
                new StringFeature("F")
            };

            var fst = new Fst <StringData, int>(_operations);

            fst.StartState = fst.CreateState();
            State <StringData, int> s1 = fst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("E").EqualTo("true").Value, fst.CreateState());
            State <StringData, int> s2 = fst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("F").EqualTo("true").Value, fst.CreateState());
            State <StringData, int> s3 = s1.Arcs.Add(FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, fst.CreateState());

            s2.Arcs.Add(FeatureStruct.New(featSys).Feature("C").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, s3);
            State <StringData, int> s4 = s3.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, fst.CreateAcceptingState());

            Assert.That(fst.IsDeterminizable, Is.True);

            s4.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, s4);

            var writer = new System.IO.StreamWriter(string.Format("c:\\ltor-nfst.dot"));

            fst.ToGraphViz(writer);
            writer.Close();

            Assert.That(fst.IsDeterminizable, Is.False);
        }
Ejemplo n.º 3
0
 public RootAllomorphTrie(Func<Annotation<ShapeNode>, bool> filter)
 {
     _fsa = new Fst<Shape, ShapeNode> {Filter = filter, UseUnification = true};
     _fsa.StartState = _fsa.CreateState();
     _filter = filter;
     _allomorphs = new Dictionary<string, RootAllomorph>();
 }
Ejemplo n.º 4
0
        public void Compose()
        {
            var featSys = new FeatureSystem
            {
                new StringFeature("value")
            };

            var fst1 = new Fst <AnnotatedStringData, int>(_operations);

            fst1.StartState = fst1.CreateState();
            State <AnnotatedStringData, int> s1 = fst1.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("a").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("x").Value, fst1.CreateAcceptingState());

            s1.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("b").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("y").Value, s1);

            var fst2 = new Fst <AnnotatedStringData, int>(_operations);

            fst2.StartState = fst2.CreateAcceptingState();
            fst2.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("x").Value, null, fst2.StartState);
            fst2.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("y").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("z").Value, fst2.StartState);

            Fst <AnnotatedStringData, int> composedFsa = fst1.Compose(fst2);
            var writer = new StringWriter();

            composedFsa.ToGraphViz(writer);
            Assert.That(writer.ToString().Replace("\r\n", "\n"), Is.EqualTo(@"digraph G {
  0 [shape=""diamond"", color=""green""];
  0 -> 1 [label=""[value:\""a\""],1:ε""];
  1 [shape=""circle"", color=""red"", peripheries=""2""];
  1 -> 1 [label=""[value:\""b\""],1:([value:\""z\""],∪)""];
}
".Replace("\r\n", "\n")));
        }
Ejemplo n.º 5
0
        public void Determinize()
        {
            var featSys = new FeatureSystem
            {
                new StringFeature("A"),
                new StringFeature("B"),
                new StringFeature("C"),
                new StringFeature("D"),
                new StringFeature("E"),
                new StringFeature("F")
            };

            var nfst = new Fst <AnnotatedStringData, int>(_operations);

            nfst.StartState = nfst.CreateState();
            State <AnnotatedStringData, int> s1 = nfst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("C").EqualTo("true").Value, nfst.CreateState());
            State <AnnotatedStringData, int> sa = s1.Arcs.Add(FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("E").EqualTo("true").Value, nfst.CreateAcceptingState());

            State <AnnotatedStringData, int> s2 = nfst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, nfst.CreateState());
            State <AnnotatedStringData, int> s3 = s2.Arcs.Add(FeatureStruct.New(featSys).Value, FeatureStruct.New(featSys).Value, nfst.CreateState());

            s3.Arcs.Add(FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, sa);

            Fst <AnnotatedStringData, int> dfst;

            Assert.That(nfst.TryDeterminize(out dfst), Is.True);
        }
Ejemplo n.º 6
0
        public void IsDeterminizable()
        {
            var featSys = new FeatureSystem
            {
                new StringFeature("A"),
                new StringFeature("B"),
                new StringFeature("C"),
                new StringFeature("D"),
                new StringFeature("E"),
                new StringFeature("F")
            };

            var fst = new Fst <AnnotatedStringData, int>(_operations);

            fst.StartState = fst.CreateState();
            State <AnnotatedStringData, int> s1 = fst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("E").EqualTo("true").Value, fst.CreateState());
            State <AnnotatedStringData, int> s2 = fst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("F").EqualTo("true").Value, fst.CreateState());
            State <AnnotatedStringData, int> s3 = s1.Arcs.Add(FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, fst.CreateState());

            s2.Arcs.Add(FeatureStruct.New(featSys).Feature("C").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, s3);
            State <AnnotatedStringData, int> s4 = s3.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, fst.CreateAcceptingState());

            Assert.That(fst.IsDeterminizable, Is.True);

            s4.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, s4);

            Assert.That(fst.IsDeterminizable, Is.False);
        }
Ejemplo n.º 7
0
        public void Compose()
        {
            var featSys = new FeatureSystem
                {
                    new StringFeature("value")
                };

            var fst1 = new Fst<AnnotatedStringData, int>(_operations);
            fst1.StartState = fst1.CreateState();
            State<AnnotatedStringData, int> s1 = fst1.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("a").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("x").Value, fst1.CreateAcceptingState());
            s1.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("b").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("y").Value, s1);

            var fst2 = new Fst<AnnotatedStringData, int>(_operations);
            fst2.StartState = fst2.CreateAcceptingState();
            fst2.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("x").Value, null, fst2.StartState);
            fst2.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("y").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("z").Value, fst2.StartState);

            Fst<AnnotatedStringData, int> composedFsa = fst1.Compose(fst2);
            var writer = new StringWriter();
            composedFsa.ToGraphViz(writer);
            Assert.That(writer.ToString().Replace("\r\n", "\n"), Is.EqualTo(@"digraph G {
              0 [shape=""diamond"", color=""green""];
              0 -> 1 [label=""[value:\""a\""],1:ε""];
              1 [shape=""circle"", color=""red"", peripheries=""2""];
              1 -> 1 [label=""[value:\""b\""],1:([value:\""z\""],∪)""];
            }
            ".Replace("\r\n", "\n")));
        }
Ejemplo n.º 8
0
        public void Compose()
        {
            var featSys = new FeatureSystem
            {
                new StringFeature("value")
            };

            var fst1 = new Fst <StringData, int>(_operations);

            fst1.StartState = fst1.CreateState();
            State <StringData, int> s1 = fst1.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("a").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("x").Value, fst1.CreateAcceptingState());

            s1.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("b").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("y").Value, s1);

            var fst2 = new Fst <StringData, int>(_operations);

            fst2.StartState = fst2.CreateAcceptingState();
            fst2.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("x").Value, null, fst2.StartState);
            fst2.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("value").EqualTo("y").Value, FeatureStruct.New(featSys).Feature("value").EqualTo("z").Value, fst2.StartState);

            Fst <StringData, int> composedFsa = fst1.Compose(fst2);
            var writer = new System.IO.StreamWriter(string.Format("c:\\ltor-composed-nfst.dot"));

            composedFsa.ToGraphViz(writer);
            writer.Close();
        }
Ejemplo n.º 9
0
        private bool GeneratePatternNfa(State <TData, TOffset> startState, Pattern <TData, TOffset> pattern, string parentName,
                                        Func <Match <TData, TOffset>, bool>[] acceptables, ref int nextPriority)
        {
            bool   deterministic = true;
            string name          = parentName == null ? pattern.Name : parentName + "*" + pattern.Name;

            if (pattern.Acceptable != null)
            {
                acceptables = acceptables.Concat(pattern.Acceptable).ToArray();
            }
            if (pattern.Children.All(node => node is Pattern <TData, TOffset>))
            {
                foreach (Pattern <TData, TOffset> childExpr in pattern.Children)
                {
                    if (!GeneratePatternNfa(startState, childExpr, name, acceptables, ref nextPriority))
                    {
                        deterministic = false;
                    }
                }
            }
            else
            {
                startState = _fsa.CreateTag(startState, _fsa.CreateState(), EntireMatch, true);
                bool hasVariables;
                startState = pattern.GenerateNfa(_fsa, startState, out hasVariables);
                if (hasVariables)
                {
                    deterministic = false;
                }
                startState = _fsa.CreateTag(startState, _fsa.CreateState(), EntireMatch, false);


                Func <TData, FstResult <TData, TOffset>, bool> acceptable = null;
                if (acceptables.Length > 0)
                {
                    acceptable = (input, match) =>
                    {
                        Match <TData, TOffset> patMatch = CreatePatternMatch(input, match);
                        return(acceptables.All(a => a(patMatch)));
                    };
                }
                State <TData, TOffset> acceptingState = _fsa.CreateAcceptingState(name, acceptable, nextPriority++);
                startState.Arcs.Add(acceptingState);
            }

            return(deterministic);
        }
Ejemplo n.º 10
0
        internal override State <TData, TOffset> GenerateNfa(Fst <TData, TOffset> fsa, State <TData, TOffset> startState, out bool hasVariables)
        {
            if (IsLeaf)
            {
                hasVariables = false;
                return(startState);
            }

            if (_name != null)
            {
                startState = fsa.CreateTag(startState, fsa.CreateState(), _name, true);
            }
            startState = base.GenerateNfa(fsa, startState, out hasVariables);
            startState = _name != null?fsa.CreateTag(startState, fsa.CreateState(), _name, false) : startState.Arcs.Add(fsa.CreateState());

            return(startState);
        }
Ejemplo n.º 11
0
 public RootAllomorphTrie(Func <Annotation <ShapeNode>, bool> filter)
 {
     _fsa = new Fst <Shape, ShapeNode> {
         Filter = filter, UseUnification = true
     };
     _fsa.StartState = _fsa.CreateState();
     _filter         = filter;
     _allomorphs     = new Dictionary <string, RootAllomorph>();
 }
Ejemplo n.º 12
0
        internal override State <TData, TOffset> GenerateNfa(Fst <TData, TOffset> fsa, State <TData, TOffset> startState, out bool hasVariables)
        {
            hasVariables = _fs.HasVariables;
            FeatureStruct condition = _fs;

            if (!_fs.IsFrozen)
            {
                condition = _fs.Clone();
                condition.Freeze();
            }
            return(startState.Arcs.Add(condition, fsa.CreateState()));
        }
Ejemplo n.º 13
0
        public void Determinize()
        {
            var featSys = new FeatureSystem
                {
                    new StringFeature("A"),
                    new StringFeature("B"),
                    new StringFeature("C"),
                    new StringFeature("D"),
                    new StringFeature("E"),
                    new StringFeature("F")
                };

            var nfst = new Fst<AnnotatedStringData, int>(_operations);
            nfst.StartState = nfst.CreateState();
            State<AnnotatedStringData, int> s1 = nfst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("C").EqualTo("true").Value, nfst.CreateState());
            State<AnnotatedStringData, int> sa = s1.Arcs.Add(FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("E").EqualTo("true").Value, nfst.CreateAcceptingState());

            State<AnnotatedStringData, int> s2 = nfst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, nfst.CreateState());
            State<AnnotatedStringData, int> s3 = s2.Arcs.Add(FeatureStruct.New(featSys).Value, FeatureStruct.New(featSys).Value, nfst.CreateState());
            s3.Arcs.Add(FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, sa);

            Fst<AnnotatedStringData, int> dfst;
            Assert.That(nfst.TryDeterminize(out dfst), Is.True);
        }
Ejemplo n.º 14
0
        private void Compile(Pattern <TData, TOffset> pattern)
        {
            _fsa = new Fst <TData, TOffset>(EqualityComparer <TOffset> .Default)
            {
                Direction      = _settings.Direction,
                Filter         = _settings.Filter,
                UseUnification = _settings.MatchingMethod == MatchingMethod.Unification
            };
            _fsa.StartState = _fsa.CreateState();
            int  nextPriority = 0;
            bool hasVariables = GeneratePatternNfa(_fsa.StartState, pattern, null,
                                                   new Func <Match <TData, TOffset>, bool> [0], ref nextPriority);

#if FST_GRAPHS
            using (var writer = new System.IO.StreamWriter(string.Format("{0}-nfa.dot",
                                                                         _settings.Direction == Direction.LeftToRight ? "ltor" : "rtol")))
                _fsa.ToGraphViz(writer);
#endif

            if (!_settings.Nondeterministic && !hasVariables && !_settings.AllSubmatches)
            {
                _fsa = _fsa.Determinize();
#if FST_GRAPHS
                using (var writer = new System.IO.StreamWriter(string.Format("{0}-dfa.dot",
                                                                             _settings.Direction == Direction.LeftToRight ? "ltor" : "rtol")))
                    _fsa.ToGraphViz(writer);
#endif
                _fsa.Minimize();
#if FST_GRAPHS
                using (var writer = new System.IO.StreamWriter(string.Format("{0}-mindfa.dot",
                                                                             _settings.Direction == Direction.LeftToRight ? "ltor" : "rtol")))
                    _fsa.ToGraphViz(writer);
#endif
            }
            else
            {
                _fsa = _fsa.EpsilonRemoval();
#if FST_GRAPHS
                using (var writer = new System.IO.StreamWriter(string.Format("{0}-ernfa.dot",
                                                                             _settings.Direction == Direction.LeftToRight ? "ltor" : "rtol")))
                    _fsa.ToGraphViz(writer);
#endif
            }
            _fsa.IgnoreVariables = !hasVariables;
            _fsa.Freeze();
        }
Ejemplo n.º 15
0
        internal override State <TData, TOffset> GenerateNfa(Fst <TData, TOffset> fsa, State <TData, TOffset> startState, out bool hasVariables)
        {
            hasVariables = false;
            if (IsLeaf)
            {
                return(startState);
            }

            State <TData, TOffset> endState = fsa.CreateState();

            foreach (PatternNode <TData, TOffset> node in Children)
            {
                bool childHasVariables;
                State <TData, TOffset> nodeEndState = node.GenerateNfa(fsa, startState, out childHasVariables);
                if (childHasVariables)
                {
                    hasVariables = true;
                }
                nodeEndState.Arcs.Add(endState);
            }
            return(endState);
        }
Ejemplo n.º 16
0
        public void Determinize()
        {
            var featSys = new FeatureSystem
            {
                new StringFeature("A"),
                new StringFeature("B"),
                new StringFeature("C"),
                new StringFeature("D"),
                new StringFeature("E"),
                new StringFeature("F")
            };

            var nfst = new Fst <StringData, int>(_operations);

            nfst.StartState = nfst.CreateState();
            State <StringData, int> s1 = nfst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("C").EqualTo("true").Value, nfst.CreateState());
            State <StringData, int> sa = s1.Arcs.Add(FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("E").EqualTo("true").Value, nfst.CreateAcceptingState());

            State <StringData, int> s2 = nfst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, nfst.CreateState());
            State <StringData, int> s3 = s2.Arcs.Add(FeatureStruct.New(featSys).Value, FeatureStruct.New(featSys).Value, nfst.CreateState());

            s3.Arcs.Add(FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, sa);

            var writer = new System.IO.StreamWriter(string.Format("c:\\ltor-nfst.dot"));

            nfst.ToGraphViz(writer);
            writer.Close();

            Fst <StringData, int> dfst;

            Assert.That(nfst.TryDeterminize(out dfst), Is.True);

            writer = new System.IO.StreamWriter(string.Format("c:\\ltor-dfst.dot"));
            dfst.ToGraphViz(writer);
            writer.Close();
        }
Ejemplo n.º 17
0
        public void IsDeterminizable()
        {
            var featSys = new FeatureSystem
                {
                    new StringFeature("A"),
                    new StringFeature("B"),
                    new StringFeature("C"),
                    new StringFeature("D"),
                    new StringFeature("E"),
                    new StringFeature("F")
                };

            var fst = new Fst<AnnotatedStringData, int>(_operations);
            fst.StartState = fst.CreateState();
            State<AnnotatedStringData, int> s1 = fst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("E").EqualTo("true").Value, fst.CreateState());
            State<AnnotatedStringData, int> s2 = fst.StartState.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("F").EqualTo("true").Value, fst.CreateState());
            State<AnnotatedStringData, int> s3 = s1.Arcs.Add(FeatureStruct.New(featSys).Feature("B").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, fst.CreateState());
            s2.Arcs.Add(FeatureStruct.New(featSys).Feature("C").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("D").EqualTo("true").Value, s3);
            State<AnnotatedStringData, int> s4 = s3.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, fst.CreateAcceptingState());
            Assert.That(fst.IsDeterminizable, Is.True);

            s4.Arcs.Add(FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, FeatureStruct.New(featSys).Feature("A").EqualTo("true").Value, s4);

            Assert.That(fst.IsDeterminizable, Is.False);
        }
Ejemplo n.º 18
0
        public void Transduce()
        {
            var fst = new Fst <AnnotatedStringData, int>(_operations)
            {
                UseUnification = false
            };

            fst.StartState = fst.CreateAcceptingState();
            fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas-", "nas?").Value, fst.StartState);
            fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas+").Symbol("cor+", "cor-").Value, fst.StartState);
            State <AnnotatedStringData, int> s1 = fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor-").Value, fst.CreateState());

            s1.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor-").Value, fst.StartState);
            State <AnnotatedStringData, int> s2 = fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor+").Value, fst.CreateAcceptingState());

            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor+").Value, s2);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas-", "nas?").Symbol("cor+", "cor?").Value, fst.StartState);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas+").Symbol("cor+").Value, fst.StartState);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor-").Value, s1);

            Fst <AnnotatedStringData, int> dfst = fst.Determinize();

            AnnotatedStringData data = CreateStringData("caNp");
            FstResult <AnnotatedStringData, int> result;

            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("camp"));

            data = CreateStringData("caN");
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("can"));

            data = CreateStringData("carp");
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("carp"));

            fst = new Fst <AnnotatedStringData, int>(_operations)
            {
                UseUnification = false
            };
            fst.StartState = fst.CreateAcceptingState();
            s1             = fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cons+").Value, fst.CreateState())
                             .Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cons-").Value, fst.CreateState());
            s2 = s1.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas+").Value, null, fst.CreateState());
            State <AnnotatedStringData, int> s3 = s1.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("voice-").Value, fst.CreateState());

            s3.Arcs.Add(null, FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo(".").Value, s2);
            s3.Arcs.Add(null, FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo("+").Value, fst.CreateState())
            .Arcs.Add(null, FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo(".").Value, s2);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cons+").Value, fst.CreateAcceptingState());

            dfst = fst.Determinize();

            data = CreateStringData("camp");
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("cap"));

            data = CreateStringData("casp");
            IEnumerable <FstResult <AnnotatedStringData, int> > results;

            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out results), Is.True);
            FstResult <AnnotatedStringData, int>[] resultsArray = results.ToArray();
            Assert.That(resultsArray.Length, Is.EqualTo(2));
            Assert.That(resultsArray.Select(r => r.Output.String), Is.EquivalentTo(new [] { "cas+.p", "cas.p" }));
        }
Ejemplo n.º 19
0
        public void Transduce()
        {
            var fst = new Fst<AnnotatedStringData, int>(_operations) {UseUnification = false};
            fst.StartState = fst.CreateAcceptingState();
            fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas-", "nas?").Value, fst.StartState);
            fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas+").Symbol("cor+", "cor-").Value, fst.StartState);
            State<AnnotatedStringData, int> s1 = fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor-").Value, fst.CreateState());
            s1.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor-").Value, fst.StartState);
            State<AnnotatedStringData, int> s2 = fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor+").Value, fst.CreateAcceptingState());
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor+").Value, s2);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas-", "nas?").Symbol("cor+", "cor?").Value, fst.StartState);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas+").Symbol("cor+").Value, fst.StartState);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cor?").Symbol("nas+").Value, FeatureStruct.New(PhoneticFeatSys).Symbol("cor-").Value, s1);

            Fst<AnnotatedStringData, int> dfst = fst.Determinize();

            AnnotatedStringData data = CreateStringData("caNp");
            FstResult<AnnotatedStringData, int> result;
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("camp"));

            data = CreateStringData("caN");
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("can"));

            data = CreateStringData("carp");
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("carp"));

            fst = new Fst<AnnotatedStringData, int>(_operations) {UseUnification = false};
            fst.StartState = fst.CreateAcceptingState();
            s1 = fst.StartState.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cons+").Value, fst.CreateState())
                .Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cons-").Value, fst.CreateState());
            s2 = s1.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("nas+").Value, null, fst.CreateState());
            State<AnnotatedStringData, int> s3 = s1.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("voice-").Value, fst.CreateState());
            s3.Arcs.Add(null, FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo(".").Value, s2);
            s3.Arcs.Add(null, FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo("+").Value, fst.CreateState())
                .Arcs.Add(null, FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo(".").Value, s2);
            s2.Arcs.Add(FeatureStruct.New(PhoneticFeatSys).Symbol("cons+").Value, fst.CreateAcceptingState());

            dfst = fst.Determinize();

            data = CreateStringData("camp");
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out result), Is.True);
            Assert.That(result.Output.String, Is.EqualTo("cap"));

            data = CreateStringData("casp");
            IEnumerable<FstResult<AnnotatedStringData, int>> results;
            Assert.That(dfst.Transduce(data, data.Annotations.First, null, true, true, true, out results), Is.True);
            FstResult<AnnotatedStringData, int>[] resultsArray = results.ToArray();
            Assert.That(resultsArray.Length, Is.EqualTo(2));
            Assert.That(resultsArray.Select(r => r.Output.String), Is.EquivalentTo(new [] {"cas+.p", "cas.p"}));
        }
Ejemplo n.º 20
0
        private void AddNode(ShapeNode node, State <Shape, ShapeNode> state, string id)
        {
            Arc <Shape, ShapeNode> arc = state.Arcs.FirstOrDefault(a => node.Annotation.FeatureStruct.ValueEquals(a.Input.FeatureStruct));
            ShapeNode nextNode         = node.GetNext(n => _filter(n.Annotation));
            State <Shape, ShapeNode> nextState;

            if (arc != null)
            {
                nextState = arc.Target;
                if (nextNode == node.List.End)
                {
                    nextState.IsAccepting = true;
                    nextState.AcceptInfos.Add(new AcceptInfo <Shape, ShapeNode>(id, (shape, match) => true, _shapeCount));
                }
            }
            else
            {
                nextState = nextNode == node.List.End ? _fsa.CreateAcceptingState(id, (shape, match) => true, _shapeCount) : _fsa.CreateState();
                FeatureStruct condition = node.Annotation.FeatureStruct.Clone();
                condition.Freeze();
                state.Arcs.Add(condition, nextState);
            }

            if (nextNode != node.List.End)
            {
                AddNode(nextNode, nextState, id);
            }
        }