Exemple #1
0
        public Task <Option <RichFormatting> > Answer(Request request)
        {
            var entry = jdict.Lookup(request.Word.RawWord.Trim());
            var rich  = new RichFormatting();

            var(greedyEntry, greedyWord) = GreedyLookup(request);
            if (greedyEntry != null && greedyWord != request.Word.RawWord)
            {
                rich.Paragraphs.Add(new TextParagraph(new[]
                {
                    new Text("The entries below are a result of the greedy lookup: "),
                    new Text(greedyWord, emphasis: true)
                }));
                var p = new TextParagraph();
                p.Content.Add(new Text(string.Join("\n\n", greedyEntry.Select(e => e.ToString()))));
                rich.Paragraphs.Add(p);
            }

            if (entry != null)
            {
                if (greedyEntry != null && greedyWord != request.Word.RawWord)
                {
                    rich.Paragraphs.Add(new TextParagraph(new[]
                    {
                        new Text("The entries below are a result of the regular lookup: "),
                        new Text(request.Word.RawWord, emphasis: true)
                    }));
                }
                var p = new TextParagraph();
                p.Content.Add(new Text(string.Join("\n\n", entry.Select(e => e.ToString()))));
                rich.Paragraphs.Add(p);
            }

            if (request.NotInflected != null && request.NotInflected != request.Word.RawWord)
            {
                entry = jdict.Lookup(request.NotInflected);
                if (entry != null)
                {
                    rich.Paragraphs.Add(new TextParagraph(new[]
                    {
                        new Text("The entries below are a result of lookup on the base form: "),
                        new Text(request.NotInflected, emphasis: true)
                    }));
                    var p = new TextParagraph();
                    p.Content.Add(new Text(string.Join("\n\n", entry.Select(e => e.ToString()))));
                    rich.Paragraphs.Add(p);
                }
            }

            if (rich.Paragraphs.Count == 0)
            {
                return(Task.FromResult(Option.None <RichFormatting>()));
            }

            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #2
0
        public static (IEnumerable <JMDictEntry> entry, string word) GreedyLookup(this JMDict jmDict, IEnumerable <string> request, int backOffCountStart = 5)
        {
            int backOffCount = backOffCountStart;
            IEnumerable <JMDictEntry> found = null;
            string foundWord        = null;
            string concatenatedWord = "";

            foreach (var word in request)
            {
                concatenatedWord += word;
                var newEntry = jmDict.Lookup(concatenatedWord);
                if (newEntry == null)
                {
                    backOffCount--;
                    if (backOffCount == 0)
                    {
                        break;
                    }
                }
                else
                {
                    found        = newEntry;
                    foundWord    = concatenatedWord;
                    backOffCount = backOffCountStart;
                }
            }

            return(found, foundWord);
        }
        public IEnumerable <GlossNote> Gloss(string inputText)
        {
            var words = lang.BreakIntoSentences(inputText)
                        .SelectMany(x => x)
                        .ToList();

            var glosses = new List <GlossNote>();

            for (int i = 0; i < words.Count; i++)
            {
                var word            = words[i];
                var greedySelection = words.Skip(i).Select(w => w.RawWord).Greedy(s =>
                {
                    var w = string.Join("", s);
                    return(dict.Lookup(w) != null);
                }).ToList();
                var lookup = dict.Lookup(word.NotInflected ?? word.RawWord)?.ToList();

                if (word.RawWord.All(c => ".!??!⁉、".IndexOf(c) != -1))
                {
                    // skip punctuation
                    continue;
                }
                if (word.Type == Option.Some(EdictType.vs_s))
                {
                    glosses.Add(CreateGloss(word, "suru, {0}", lookup));
                }
                if (word.Type == Option.Some(EdictType.vs_i))
                {
                    glosses.Add(CreateGloss(word, "suru, {0}, verbalizing suffix", lookup));
                }
                else if (mapping.TryGetValue(word.EstimatedPartOfSpeech, out var edictType))
                {
                    var description = lookup
                                      ?.SelectMany(entry => entry.Senses)
                                      .FirstOrDefault(s => s.Type.HasValue && s.Type == Option.Some(edictType))
                                      ?.Description;
                    if ((description == null || word.Type == Option.Some(EdictType.cop_da)) && greedySelection.Count > 1)
                    {
                        var greedyWord    = string.Join("", greedySelection);
                        var greedyEntries = dict.Lookup(greedyWord);

                        var splitGreedyWord = string.Join(" ", lang.BreakIntoSentences(greedyWord).SelectMany(x => x).Select(x => x.RawWord));
                        glosses.Add(CreateGloss(new WordInfo(splitGreedyWord), "{0}", greedyEntries));

                        i += greedySelection.Count - 1; // -1 because iteration will result in one extra increase
                        continue;
                    }
                    description = description ?? lookup?.SelectMany(entry => entry.Senses).FirstOrDefault()?.Description ?? "";
                    glosses.Add(new GlossNote(word.RawWord, description));
                }
                else if (word.EstimatedPartOfSpeech == PartOfSpeech.Particle)
                {
                    var description = lookup
                                      ?.SelectMany(entry => entry.Senses)
                                      .FirstOrDefault(s => s.Type == Option.Some(EdictType.prt))
                                      ?.Description;
                    if (description != null)
                    {
                        glosses.Add(new GlossNote(word.RawWord, "Particle " + word.NotInflected + " - " + description));
                    }
                    else
                    {
                        glosses.Add(CreateGloss(word, "{0}", lookup));
                    }
                }
                else if (word.Independent == false)
                {
                    var l = glosses.Last();
                    glosses.RemoveAt(glosses.Count - 1);
                    glosses.Add(new GlossNote(
                                    l.Foreign + " " + word.RawWord,
                                    l.Text.EndsWith(" + inflections") ? l.Text : l.Text + " + inflections"));
                }
                else
                {
                    glosses.Add(CreateGloss(word, "{0}", lookup));
                }
            }
            ;
            return(glosses);
        }
        public Task <Option <RichFormatting> > Answer(Request request)
        {
            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.", emphasis: false))));
            }


            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.", emphasis: false))));
                return(Task.FromResult(Option.Some(rich)));
            }

            var verbTypes = entries.Select(e =>
            {
                if (!e.Readings.Any())
                {
                    return(Option.None <LibJpConjSharp.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.", emphasis: false))));
                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.Past, verbType),
                        Form("?", CForm.Potential, verbType),
                        Form("->", CForm.Causative, 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
-> - Causative
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, LibJpConjSharp.EdictType type)
            {
                return(new Text(
                           $"{name}: {JpConj.Conjugate(verb, type, form, Politeness.Plain, Polarity.Affirmative).Replace("|", "")}\n~{name}: {JpConj.Conjugate(verb, type, form, Politeness.Plain, Polarity.Negative).Replace("|", "")}\n"));
            }
        }