Repository navigation
Add bounded batch parsing to HermitCrab #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,6 +218,65 @@ public IEnumerable<Word> ParseWord(string word, out object trace, bool guessRoot | |
| return syntheses; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Eagerly parses surface forms in input order, capturing each parse's exception and trace in its result. | ||
| /// Requires a Morpher constructed with maxDegreeOfParallelism: 1. Tracing runs the batch serially. | ||
| /// Keep the Language, settings, and tracing fixed until completion; selectors and custom rules must | ||
| /// support concurrent calls when maxWordConcurrency is greater than 1 and tracing is disabled. | ||
| /// Do not overlap traced calls sharing a trace manager. Failed parses have empty analyses. | ||
| /// Batch argument validation and input enumeration errors propagate to the caller. | ||
| /// </summary> | ||
| public IReadOnlyList<WordParseResult> ParseWords( | ||
| IEnumerable<string> words, | ||
| int maxWordConcurrency = 4, | ||
| bool guessRoot = false | ||
| ) | ||
| { | ||
| if (words == null) | ||
| throw new ArgumentNullException(nameof(words)); | ||
| if (maxWordConcurrency < 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: F2. A |
||
| throw new ArgumentOutOfRangeException(nameof(maxWordConcurrency)); | ||
| if (MaxDegreeOfParallelism != 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: F1. |
||
| { | ||
| throw new InvalidOperationException( | ||
| "ParseWords requires a Morpher constructed with maxDegreeOfParallelism: 1." | ||
| ); | ||
| } | ||
|
|
||
| string[] inputs = words.ToArray(); | ||
| var results = new WordParseResult[inputs.Length]; | ||
| void Parse(int index) | ||
| { | ||
| object trace = null; | ||
| try | ||
| { | ||
| var analyses = ParseWord(inputs[index], out trace, guessRoot).ToList().AsReadOnly(); | ||
| results[index] = new WordParseResult(inputs[index], analyses, trace, null); | ||
| } | ||
| catch (Exception error) | ||
| { | ||
| results[index] = new WordParseResult(inputs[index], Array.Empty<Word>(), trace, error); | ||
| } | ||
| } | ||
|
|
||
| if (_traceManager.IsTracing || maxWordConcurrency == 1) | ||
| { | ||
| for (int i = 0; i < inputs.Length; i++) | ||
| Parse(i); | ||
| } | ||
| else | ||
| { | ||
| Parallel.For( | ||
| 0, | ||
| inputs.Length, | ||
| new ParallelOptions { MaxDegreeOfParallelism = maxWordConcurrency }, | ||
| Parse | ||
| ); | ||
| } | ||
|
|
||
| return Array.AsReadOnly(results); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Generates surface forms from the specified word synthesis information. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace SIL.Machine.Morphology.HermitCrab | ||
| { | ||
| public sealed class WordParseResult | ||
| { | ||
| internal WordParseResult(string surfaceForm, IReadOnlyList<Word> analyses, object trace, Exception error) | ||
| { | ||
| SurfaceForm = surfaceForm; | ||
| Analyses = analyses; | ||
| Trace = trace; | ||
| Error = error; | ||
| } | ||
|
|
||
| public string SurfaceForm { get; } | ||
|
|
||
| public IReadOnlyList<Word> Analyses { get; } | ||
|
|
||
| public object Trace { get; } | ||
|
|
||
| public Exception Error { get; } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: F3. With
MaxAlternatives > 0, everyApplycall copies both rule lists into a new cascade and a newRuleBatch.AnalysisLanguageRulemakes that call once per input word per stratum, so the allocations grow with ambiguity. Building the capped pair once per stratum per parse, before theinDataloop, gives the same limits. Unverified cost: not measured.