Exemple #1
0
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var entries = dict.Lookup(request.Word.RawWord.Trim()) ?? Enumerable.Empty <JnedictEntry>();
            var rich    = new RichFormatting();

            foreach (var e in entries)
            {
                rich.Paragraphs.Add(new TextParagraph(new []
                {
                    new Text(string.Join("; ", e.Kanji)),
                    new Text("\n"),
                    new Text(string.Join(" ", e.Reading)),
                }.Concat(e.Translation.SelectMany(t =>
                {
                    return(new[]
                    {
                        new Text("\n"),
                        new Text(string.Join("/\u200B", t.Type.Select(type => type.ToLongString()))),
                        new Text("\n"),
                        new Text(string.Join("/\u200B", t.Translation)),
                    });
                }))));
            }

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

            return(Task.FromResult(Option.Some(rich)));
        }
        public async Task <Option <RichFormatting> > Answer(Request request)
        {
            try
            {
                var rich       = new RichFormatting();
                var paragraphs = ReadParagraphs(path);
                if (paragraphs != null)
                {
                    await paragraphs.Where(paragraph => paragraph.Contains(request.QueryText)).ForEachAsync(paragraph =>
                    {
                        var text = new TextParagraph(
                            StringExt.HighlightWords(paragraph, request.QueryText)
                            .Select(p => new Text(p.text, emphasis: p.highlight)));
                        rich.Paragraphs.Add(text);
                    });

                    return(Option.Some(rich));
                }
            }
            catch (FileNotFoundException)
            {
                var text = "This data source looks for a custom_notes.txt file in the data directory. Currently no such file exists.";
                var rich = new RichFormatting(
                    EnumerableExt.OfSingle(
                        new TextParagraph(
                            EnumerableExt.OfSingle(
                                new Text(text)))));
                return(Option.Some(rich));
            }

            return(Option.None <RichFormatting>());
        }
Exemple #3
0
        public Task <Option <RichFormatting> > Answer(Request request)
        {
            var ch   = request.Character;
            var rich = new RichFormatting();

            rich.Paragraphs.Add(new TextParagraph(new[] { new Text(ch, fontName: "kanji", fontSize: FontSize.Humonguous) }));
            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #4
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 #5
0
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var entry = lookup.LookupWords(request.Word.RawWord.Trim());
            var rich  = new RichFormatting();
            var p     = new TextParagraph();

            p.Content.Add(new Text(string.Join("\n", entry.OrderByDescending(m => list.RateFrequency(m)).Distinct()), fontSize: FontSize.Large));
            rich.Paragraphs.Add(p);
            return(Task.FromResult(Option.Some(rich)));
        }
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var rich = new RichFormatting();
            var p    = new TextParagraph();

            var text = romaji.ToRomaji(request.AllText());

            p.Content.Add(new Text(text));
            rich.Paragraphs.Add(p);
            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #7
0
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var rich    = new RichFormatting();
            var text    = request.AllText();
            var glosses = autoglosser.Gloss(text);

            rich.Paragraphs.Add(
                new TextParagraph(EnumerableExt.OfSingle(new Text(Descriptor.AcknowledgementText))));
            var s = string.Join("\n", glosses.Select(gloss => $"- {gloss.Foreign}:\n{string.Join("\n", gloss.GlossCandidates.Select(c => $"    - {c}"))}"));

            rich.Paragraphs.Add(new TextParagraph(EnumerableExt.OfSingle(new Text(s))));
            return(Task.FromResult(Option.Some(rich)));
        }
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var rich = new RichFormatting();

            var key     = string.Join("", request.SubsequentWords.Take(10));
            var results = lookup.Lookup(key);

            foreach (var paragraph in results.SelectMany(Render))
            {
                rich.Paragraphs.Add(paragraph);
            }

            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #9
0
    public async Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
    {
        var api = this.memApiAccessor();

        if (api == null)
        {
            return(Option.Some(new RichFormatting(new Paragraph[]
            {
                new TextParagraph(new Text[]
                {
                    new Text("The address to the DidacticalEnigma.Mem server is not configured")
                })
            })));
        }

        var response = await api.QueryWithHttpMessagesAsync(
            query : request.Word.RawWord,
            cancellationToken : token,
            translatedOnly : true);

        var rich = new RichFormatting();

        if (response.Response.IsSuccessStatusCode)
        {
            QueryTranslationsResult result = response.Body;
            foreach (var tl in result.Translations)
            {
                var highlights    = MarkHighlights(tl.Source, tl.HighlighterSequence);
                var textParagraph = new TextParagraph(
                    highlights
                    .Select(t => new Text(t.fragment, emphasis: t.highlight)));
                textParagraph.Content.Add(new Text("\n"));
                textParagraph.Content.Add(new Text(tl.Target));
                rich.Paragraphs.Add(textParagraph);
            }
        }
        else
        {
            rich.Paragraphs.Add(
                new TextParagraph(
                    new Text[]
            {
                new Text($"Error: {response.Response.StatusCode}")
            }));
        }

        return(rich.Some());
    }
Exemple #10
0
    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();
            var lookup = LookAhead(request);

            foreach (var l in lookup)
            {
                var textParagraph = new TextParagraph(
                    l.RenderedHighlights
                    .Select(t => new Text(t.fragment, emphasis: t.highlight)));
                textParagraph.Content.Add(new Text("\n"));
                textParagraph.Content.Add(new Text(l.SentencePair.EnglishSentence));
                rich.Paragraphs.Add(textParagraph);
            }

            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #12
0
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var rich = new RichFormatting();
            var p    = new TextParagraph();
            var word = request.NotInflected ?? request.Word.RawWord;

            if (frequencies.TryGetValue(word, out var d))
            {
                p.Content.Add(new Text($"{word} is rated {d:F3} (10-grade scale)"));
            }
            else
            {
                p.Content.Add(new Text($"{word} is unrated (not enough data)"));
            }

            rich.Paragraphs.Add(p);
            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #13
0
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var rich   = new RichFormatting();
            var lookup = LookAhead(request);

            foreach (var l in lookup)
            {
                var textParagraph = new TextParagraph(
                    l.RenderedHighlights
                    .Select(t => new Text(t.fragment, emphasis: t.highlight)));
                textParagraph.Content.Add(new Text("\n"));
                textParagraph.Content.Add(new Text(string.Join("\n", l.DictionaryEntry.Senses
                                                               .Where(s => s.PartOfSpeechInfo.Contains(EdictPartOfSpeech.exp))
                                                               .Select(s => string.Join("/\u200B", s.Glosses)))));

                rich.Paragraphs.Add(textParagraph);
            }

            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #14
0
        public Task <Option <RichFormatting> > Answer(Request request)
        {
            var ch   = request.Character;
            var cp   = CodePoint.FromString(ch);
            var rich = new RichFormatting();
            var p    = new TextParagraph();

            var radicals = cp is Kanji k
                ? lang.LookupRadicals(k).ValueOr(Enumerable.Empty <CodePoint>())
                : Enumerable.Empty <CodePoint>();

            var romaji = cp is Kana kana?lang.LookupRomaji(kana) : null;

            var text = cp.ToDescriptionString() + "\n" +
                       (romaji != null ? romaji + "\n" : "") +
                       string.Join(" ; ", radicals);

            p.Content.Add(new Text(text));
            rich.Paragraphs.Add(p);
            return(Task.FromResult(Option.Some(rich)));
        }
Exemple #15
0
        public async Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var rich      = new RichFormatting();
            var sentences = jesc.SearchByJapaneseTextAsync(request.QueryText);

            foreach (var sentence in (await sentences.Take(100).ToListAsync(token)).OrderByDescending(s => s.JapaneseSentence.Length).Take(20))
            {
                var paragraph = new TextParagraph();
                foreach (var(text, highlight) in StringExt.HighlightWords(sentence.JapaneseSentence, request.QueryText))
                {
                    paragraph.Content.Add(new Text(text, highlight));
                }
                paragraph.Content.Add(new Text(sentence.EnglishSentence));
                rich.Paragraphs.Add(paragraph);
            }
            if (rich.Paragraphs.Count != 0)
            {
                return(Option.Some(rich));
            }

            return(Option.None <RichFormatting>());
        }
Exemple #16
0
        public Task <Option <RichFormatting> > Answer(Request request, CancellationToken token)
        {
            var rich      = new RichFormatting();
            var sentences = be.SearchByJapaneseText(request.QueryText);

            foreach (var sentence in sentences.OrderBy(s => s.JapaneseSentence.Length).Take(20))
            {
                var paragraph = new TextParagraph();
                foreach (var part in StringExt.HighlightWords(sentence.JapaneseSentence, request.QueryText))
                {
                    paragraph.Content.Add(new Text(part.text, part.highlight));
                }
                paragraph.Content.Add(new Text(sentence.EnglishSentence));
                rich.Paragraphs.Add(paragraph);
            }
            if (rich.Paragraphs.Count != 0)
            {
                return(Task.FromResult(Option.Some(rich)));
            }

            return(Task.FromResult(Option.None <RichFormatting>()));
        }
Exemple #17
0
        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"));
            }
        }
        public FlowDocument Render(RichFormatting document)
        {
            var flow = new FlowDocument
            {
                PagePadding   = new Thickness(4),
                TextAlignment = TextAlignment.Left
            };

            foreach (var paragraph in document.Paragraphs)
            {
                switch (paragraph)
                {
                case LinkParagraph link:
                {
                    var p     = new System.Windows.Documents.Paragraph();
                    var hlink = new Hyperlink(new Run(link.DisplayText))
                    {
                        NavigateUri = link.Target
                    };
                    hlink.RequestNavigate += (sender, args) =>
                    {
                        webBrowser.NavigateTo(args.Uri);
                        args.Handled = true;
                    };
                    p.Inlines.Add(hlink);
                    flow.Blocks.Add(p);
                }
                break;

                case TextParagraph text:
                {
                    var p = new System.Windows.Documents.Paragraph();
                    foreach (var c in text.Content)
                    {
                        Inline i;
                        var    r = new Run(c.Content);
                        switch (c.FontSize)
                        {
                        case FontSize.ExtraSmall:
                            r.FontSize = 9;
                            break;

                        case FontSize.Small:
                            r.FontSize = 12;
                            break;

                        case FontSize.Medium:
                            r.FontSize = 14;
                            break;

                        case FontSize.Normal:
                            r.FontSize = 16;
                            break;

                        case FontSize.Large:
                            r.FontSize = 24;
                            break;

                        case FontSize.ExtraLarge:
                            break;

                        case FontSize.Humonguous:
                            r.FontSize = 120;
                            break;

                        default:
                            throw new InvalidOperationException("not a valid enum value");
                        }

                        if (fontResolver.Resolve(c.FontName) is System.Windows.Media.FontFamily f)
                        {
                            r.FontFamily = f;
                        }

                        if (c.Emphasis)
                        {
                            i = new Bold(r);
                        }
                        else
                        {
                            i = r;
                        }
                        p.Inlines.Add(i);
                    }
                    flow.Blocks.Add(p);
                }
                break;
                }
            }
            return(flow);
        }
        public static FlowDocument Render(IFontResolver fontResolver, RichFormatting document)
        {
            var flow = new FlowDocument();

            flow.TextAlignment = TextAlignment.Left;
            foreach (var paragraph in document.Paragraphs)
            {
                switch (paragraph)
                {
                case TextParagraph text:
                {
                    var p = new System.Windows.Documents.Paragraph();
                    foreach (var c in text.Content)
                    {
                        Inline i;
                        var    r = new Run(c.Content);
                        switch (c.FontSize)
                        {
                        case FontSize.ExtraSmall:
                            break;

                        case FontSize.Small:
                            break;

                        case FontSize.Normal:
                            // do nothing
                            break;

                        case FontSize.Large:
                            r.FontSize = 24;
                            break;

                        case FontSize.ExtraLarge:
                            break;

                        case FontSize.Humonguous:
                            r.FontSize = 120;
                            break;

                        default:
                            throw new InvalidOperationException("not a valid enum value");
                        }

                        if (fontResolver.Resolve(c.FontName) is System.Windows.Media.FontFamily f)
                        {
                            r.FontFamily = f;
                        }

                        if (c.Emphasis)
                        {
                            i = new Bold(r);
                        }
                        else
                        {
                            i = r;
                        }
                        p.Inlines.Add(i);
                    }
                    flow.Blocks.Add(p);
                }
                break;
                }
            }
            return(flow);
        }