public override WordDerivation[] DeriveImplementation(ExecutionContext context, WordDerivation derivation) { var derivations = new WordDerivation[] { derivation }; var newDerivations = new List <WordDerivation>(); foreach (var rule in Rules) { foreach (var d in derivations) { var results = rule.Derive(context, d); if (results.Length > 0) { newDerivations.AddRange(results); } else { newDerivations.Add(d); } } derivations = newDerivations.ToArray(); newDerivations.Clear(); } return(derivations.ToArray()); }
private static DerivationStep[] ToSteps(WordDerivation derivation) { var steps = new List <DerivationStep>(); var d = derivation; while (d.Previous != null) { steps.Add(ToStep(d)); d = d.Previous; } return(steps.AsEnumerable().Reverse().ToArray()); }
private WhiteBoxStep[] ToSteps(WordDerivation derivation) { var steps = new List <WhiteBoxStep>(); steps.Add(ToStep(derivation.Derived)); var d = derivation; while (d.Previous != null) { steps.Add(ToStep(d.Original)); d = d.Previous; } return(steps.AsEnumerable().Reverse().ToArray()); }
protected void TestSample(IRule rule, RuleContextTest ruleTest, RuleTestSample sample) { var word = sample.Input; var initialDerivation = WordDerivation.Origin(word); var derivation = rule.Derive(ExecutionContext, initialDerivation); var derived = derivation.Select(d => d.Derived).ToArray(); int expectedNb = sample.Outputs.Length; Assert.True(expectedNb == derived.Length, $"Expect [{string.Join("", word.Phonemes)}] to have [{expectedNb}] derivations ({string.Join(", ", sample.Outputs.Select(o => "[" + string.Join("", o.Word.Phonemes) + "]"))}) but only have [{derived.Length}]."); for (int i = 0; i < sample.Outputs.Length; i++) { TestSampleOutput(sample.Outputs, derived, i); } }
private static DerivationStep ToStep(WordDerivation d) { var phonemes = string.Join("", d.Derived.Phonemes); var graphicalForms = d.Derived.GraphicalForms .Select(g => string.Join("", g.Intervals .OrderBy(i => i.Start) .ThenBy(i => i.End) .Values())) .ToArray(); return(new DerivationStep( phonemes, graphicalForms, d.Rule.Id, d.Rule.TimeSpan.Start, d.Rule.TimeSpan.End, string.Join(" / ", d.Rule.Operations.Select(o => o.Name)))); }
public override WordDerivation[] DeriveImplementation(ExecutionContext context, WordDerivation derivation) { var newDerivations = new List <WordDerivation>(); if (Optional) { newDerivations.Add(derivation); } var ruleDerivations = Apply(context, derivation.Derived) .Select(w => new WordDerivation(this, derivation.Derived, w, derivation)) .ToArray(); newDerivations.AddRange(ruleDerivations); return(newDerivations.ToArray()); }
public WordDerivation[] Derive(ExecutionContext context, WordDerivation derivation) { foreach (var analyzer in PreAnalyzers) { context.RunAnalyzer(analyzer, derivation.Derived); } var results = DeriveImplementation(context, derivation); foreach (var result in results) { foreach (var analyzer in PostAnalyzers) { context.RunAnalyzer(analyzer, result.Derived); } } return(results); }
public void TestApplyWithScope() { var c = new PhonemeQuery(new[] { "b", "l" }); var v = new PhonemeQuery(new[] { "a", "e" }); var rule = new Rule( id: "test", group: "test", timeSpan: new Interval(0, 1), queries: new[] { new ContextualQuery(v, lookBehind: c, lookAhead: c, scope: "syllable"), }, operation: new[] { new Operation( name: "Test", phonological: ps => new[] { "o" }, graphical: new[] { new GraphicalMap((b, m, a) => m), }) }); var word = new Word( Phonemes("b", "e", "l", "l", "a", "b", "e", "l"), GraphicalForms(Alignment.Parse("B E L L A B E L")), Fields(Field("syllable", Alignment.Parse("long:3 short:2 long:3")))); var context = new ExecutionContext(); var originalDerivation = WordDerivation.Origin(word); var derivation = rule.DeriveImplementation(context, originalDerivation); var newWords = derivation.Select(d => d.Derived).ToArray(); var expected = new[] { new Word( Phonemes("b", "o", "l", "l", "a", "b", "o", "l"), GraphicalForms(Alignment.Parse("B E L L A B E L")), Fields(Field("syllable", Alignment.Parse("long:3 short:2 long:3")))), }; WordAssert.Equal(expected, newWords); }
public void TestApply() { var l = new PhonemeQuery(new[] { "l" }); var rule = new Rule( id: "test", group: "test", timeSpan: new Interval(0, 1), queries: new[] { new ContextualQuery(new SequenceQuery(new[] { l, l })), }, operation: new[] { new Operation( name: "Test", phonological: ps => new[] { "l" }, graphical: new[] { new GraphicalMap((b, m, a) => m), }) }); var word = new Word( Phonemes("b", "e", "l", "l", "a"), GraphicalForms(Alignment.Parse("B E L L A")), Fields(Field("type", Alignment.Parse("C V C C V")))); var context = new ExecutionContext(); var originalDerivation = WordDerivation.Origin(word); var derivation = rule.DeriveImplementation(context, originalDerivation); var newWords = derivation.Select(d => d.Derived).ToArray(); var expected = new[] { new Word( Phonemes("b", "e", "l", "a"), GraphicalForms(Alignment.Parse("B E LL:2 A")), Fields(Field("type", Alignment.Parse("C V C V")))) }; WordAssert.Equal(expected, newWords); }
public abstract WordDerivation[] DeriveImplementation(ExecutionContext context, WordDerivation derivation);
public override WordDerivation[] DeriveImplementation(ExecutionContext context, WordDerivation derivation) { return(First(r => r.Derive(context, derivation))); }