public IEnumerable <AutoGlosserNote> Gloss(string inputText) { var words = parser.BreakIntoWords(inputText) .ToList(); var glosses = new List <AutoGlosserNote>(); for (int i = 0; i < words.Count; i++) { var word = words[i]; var greedySelection = words.Skip(i).Greedy(wordInfos => { var entireExpression = string.Concat(wordInfos.Select(w => w.RawWord)); var l = dictLookup.Lookup(entireExpression); if (l == null) { return(false); } var entireReading = kana.ToHiragana(string.Concat(wordInfos.Select(w => w.Reading))); if (l.Any(e => e.ReadingEntries.Any(r => entireReading == kana.ToHiragana(r.Reading)))) { return(true); } return(false); }).ToList(); var lookup = dictLookup.Lookup(word.DictionaryForm ?? word.RawWord)?.ToList(); if (word.RawWord.All(c => ".!??!⁉、…。.".IndexOf(c) != -1)) { // skip punctuation continue; } if (greedySelection.Count > 1) { var greedyLookup = dictLookup.Lookup(string.Concat(greedySelection.Select(w => w.RawWord))).Materialize(); glosses.Add(new AutoGlosserNote( string.Join(" ", greedySelection.Select(w => w.RawWord)), OrderSenses(FilterOutInapplicableSenses(greedyLookup, greedySelection)).Select(FormatSense))); i += greedySelection.Count - 1; // -1 because iteration will result in one extra increase continue; } else if (lookup != null) { glosses.Add(new AutoGlosserNote(word.RawWord, OrderSenses(FilterOutInapplicableSenses(lookup, word)).Select(FormatSense))); } else { glosses.Add(new AutoGlosserNote(word.RawWord, new string[0])); } } return(glosses); }
public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token) { return(DictUtils.Lookup( request, t => jdict.Lookup(t), r => GreedyLookup(r), kana, Render)); }
public IEnumerable <IEnumerable <WordInfo> > BreakIntoSentences(string input) { if (string.IsNullOrWhiteSpace(input)) { yield break; } var entries = analyzer.ParseToEntries(input) .Where(a => a.IsRegular); var list = new List <WordInfo>(); int previousIndex = 0; int currentIndex = 0; foreach (var entry in entries) { previousIndex = currentIndex; currentIndex = input.IndexOf(entry.SurfaceForm, currentIndex, StringComparison.Ordinal); var newlines = input.SubstringFromTo(previousIndex, currentIndex).ReplaceLineEndings("\n") .Count(c => c == '\n'); for (int i = 0; i < newlines; i++) { yield return(list); list = new List <WordInfo>(); } list.Add(Map(entry)); } if (list.Count != 0) { yield return(list); } WordInfo Map(IEntry word) { var reading = word.Reading ?? lookup.Lookup(word.DictionaryForm ?? word.SurfaceForm)?.FirstOrDefault() ?.ReadingEntries.First().Reading; return(new WordInfo( word.SurfaceForm, word.PartOfSpeech, word.DictionaryForm, word.GetPartOfSpeechInfo().Contains(PartOfSpeechInfo.Pronoun) ? Option.Some(EdictPartOfSpeech.pn) : word.Type, reading, word is UnidicEntry unidicEntry ? unidicEntry.DictionaryFormReading : null)); }; }
public void LookupKana() { { var entries = jmdict.Lookup("みなみ"); Assert.True(entries.Any(e => e.Senses.Any(s => s.Glosses.Contains("south")))); Assert.True(entries.Any(e => e.Senses.Any(s => s.PartOfSpeechInfo.Contains(EdictPartOfSpeech.n)))); } }
public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token) { var rich = new RichFormatting(); var jmDictEntries = jdict.Lookup(request.NotInflected ?? request.QueryText) ?? Enumerable.Empty <JMDictEntry>(); if (request.Word.DictionaryFormReading != null) { var normalizedReading = kana.ToKatakana(request.Word.DictionaryFormReading); jmDictEntries = jmDictEntries .OrderByDescending(entry => entry.ReadingEntries .Select(readingEntry => kana.ToKatakana(readingEntry.Reading)) .Contains(normalizedReading) ? 1 : 0); } foreach (var jmDictEntry in jmDictEntries) { rich.Paragraphs.Add(new TextParagraph( new [] { jmDictEntry.KanjiEntries.Select(kanjiEntry => new Text(kanjiEntry.Kanji, emphasis: true)) .Intersperse(new Text("; ", emphasis: true)), jmDictEntry.ReadingEntries.Select(readingEntry => new Text($"{readingEntry.Reading}", emphasis: true)) .Intersperse(new Text("; ", emphasis: true)) .Prepend(new Text("【", emphasis: true)) .Append(new Text("】", emphasis: true)), jmDictEntry.Senses.SelectMany((sense, position) => sense.Glosses .Select(gloss => new Text(gloss)) .Intersperse(new Text("/")) .Prepend(new Text($" ({position+1}) ", fontSize: FontSize.Small)) .Prepend(new Text(" (" + string.Join(",", sense.PartOfSpeechInfo.Select(pos => pos.ToAbbrevation())) + ") ", fontSize: FontSize.Small))) }.SelectMany(x => x))); } if (rich.Paragraphs.Count == 0) { return(Task.FromResult(Option.None <RichFormatting>())); } return(Task.FromResult(Option.Some(rich))); }
public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token) { var rich = new RichFormatting(); if (!(request.PartOfSpeech == PartOfSpeech.Verb || request.PartOfSpeech == PartOfSpeech.Unknown)) { rich.Paragraphs.Add( new TextParagraph( EnumerableExt.OfSingle( new Text("The program estimates this word is not a verb. The results below may be garbage.")))); } var verb = request.NotInflected ?? request.Word.RawWord; var entries = jdict.Lookup(verb); if (entries == null) { rich.Paragraphs.Add( new TextParagraph( EnumerableExt.OfSingle( new Text("No word found.")))); return(Task.FromResult(Option.Some(rich))); } var verbTypes = entries.Select(e => { if (!e.ReadingEntries.Any()) { return(Option.None <EdictType>()); } return(GetEdictVerbType(e)); }) .OfNonNone() .Distinct() .OrderByDescending(e => Option.Some((int)e) == request.Word.Type.Map(t => (int)t) ? 1 : 0) .ToList(); if (verbTypes.Count == 0) { rich.Paragraphs.Add( new TextParagraph( EnumerableExt.OfSingle( new Text("No verb found.")))); return(Task.FromResult(Option.Some(rich))); } else { foreach (var verbType in verbTypes) { if (verbTypes.Count > 1) { rich.Paragraphs.Add(new TextParagraph( EnumerableExt.OfSingle(new Text(verb + ": " + LibJpConjSharp.EdictTypeUtils.ToLongString(verbType))))); } rich.Paragraphs.Add(new TextParagraph(new[] { Form("=", CForm.Present, verbType), Form("@=", CForm.Present, verbType, Politeness.Polite), Form("<", CForm.Past, verbType), Form("?", CForm.Potential, verbType), Form("#", CForm.Passive, verbType), Form("->", CForm.Causative, verbType), Form("if", CForm.Condition, verbType), Form("Te", CForm.TeForm, verbType), Form("!", CForm.Imperative, verbType), Form(":D", CForm.Volitional, verbType), })); } } rich.Paragraphs.Add(new TextParagraph(EnumerableExt.OfSingle(new Text( @"= - Present < - Past ? - Potential # - Passive -> - Causative if - Conditional Te - Te Form ! - Imperative :D - Volitional ~ - Negative form of any of those @ - Polite form ")))); return(Task.FromResult(Option.Some(rich))); Text Form(string name, CForm form, EdictType type, Politeness politeness = Politeness.Plain) { return(new Text( $"{name}: {JpConj.Conjugate(verb, type, form, politeness).Replace("|", "")}\n~{name}: {JpConj.Conjugate(verb, type, form, politeness, Polarity.Negative).Replace("|", "")}\n")); } }