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(); }
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"})); }
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" })); }