Ejemplo n.º 1
0
 public ParseResult parse(string text, ParseOptions parseOptions)
 {
     if (state != State.WORKING) {
         return null;
     }
     IList<ParseResult> results = new List<ParseResult>();
     int i = 0;
     while (i < text.Length) {
         StringBuilder part = new StringBuilder();
         if (!isAcceptableChar(text[i])) {
             do {
                 part.Append(text[i]);
                 i += 1;
             } while (i < text.Length && !isAcceptableChar(text[i]));
             results.Add(ParseResult.unparsed(part.ToString()));
         } else {
             do {
                 part.Append(text[i]);
                 i += 1;
             } while (i < text.Length && isAcceptableChar(text[i]));
             results.Add(parseTextPart(part.ToString(), parseOptions));
         }
     }
     return ParseResult.concat(results);
 }
Ejemplo n.º 2
0
        public TranslationForm()
        {
            InitializeComponent();

                hintForm = new HintForm();
                hintForm.setMainForm(this);
                backgroundForm = new BackgroundForm();
                backgroundForm.setMainForm(this);
                FormUtil.restoreLocation(this);
                TopMost = Settings.app.stayOnTop;
                webBrowser1.ObjectForScripting = new BrowserInterop(webBrowser1, new InteropMethods(this));
                webBrowser1.Url = Utils.getUriForBrowser("translation.html");
                TranslationService.instance.onTranslationRequest += (id, raw, src) =>
                {
                    var translators = Settings.app.getSelectedTranslators(!Atlas.instance.isNotFound);
                    if (translators.Count == 1 && Settings.session.po != null)
                    {
                        // trying .po translation
                        var poTrans = PoManager.instance.getTranslation(raw);
                        if (!string.IsNullOrEmpty(poTrans))
                        {
                            webBrowser1.callScript("newTranslationResult", id, Utils.toJson(new TranslationResult(poTrans, false)));
                            return;
                        }
                    }
                    webBrowser1.callScript("translate", id, raw, src, Utils.toJson(translators));
                };
                TranslationService.instance.onEdictDone += (id, parse) =>
                {
                    lastParseResult = parse;
                    if (id == waitingForId)
                    {
                        waitingForId = -1;
                        return;
                    }
                    lastParseOptions = null;
                    submitParseResult(parse);
                };
                if (OptionsForm.instance.Visible)
                {
                    this.SuspendTopMostBegin();
                }
                OptionsForm.instance.VisibleChanged += (sender, e) =>
                {
                    if ((sender as Form).Visible)
                    {
                        this.SuspendTopMostBegin();
                    }
                    else
                    {
                        this.SuspendTopMostEnd();
                    }
                };
                SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
                //Utils.setWindowNoActivate(this.Handle);
                Winapi.RegisterHotKey(Handle, 0, (int)Winapi.KeyModifier.Control, (int)Keys.Oemtilde);
        }
Ejemplo n.º 3
0
 private void parseSelectionToolStripMenuItem_Click(object sender, EventArgs e)
 {
     string sel = lastIsRealSelection ? lastSelection : null;
     if (!string.IsNullOrWhiteSpace(sel)) {
         ParseResult pr = lastSelectedParseResult;
         if (pr != null) {
             Settings.app.removeBannedWord(sel);
             if (lastParseOptions == null) {
                 lastParseOptions = new ParseOptions();
             }
             lastParseOptions.addUserWord(sel);
             waitingForId = pr.id;
             TranslationService.instance.updateId(pr.id, pr.asText(), lastParseOptions).ContinueWith((res) => {
                 ParseResult newRes = res.Result;
                 if (newRes != null && !newRes.getParts().Any((p) => p.asText() == sel)) {
                     webBrowser1.callScript("flash", "No match found");
                 } else {
                     submitParseResult(newRes);
                 }
             });
         }
     }
 }
Ejemplo n.º 4
0
        private ParseResult parseTextPart(string text, ParseOptions parseOptions)
        {
            DynamicParseResult[] scoreTable = new DynamicParseResult[text.Length + 1];
            for (int i = 0; i <= text.Length; ++i) {
                scoreTable[i] = new DynamicParseResult { score = i * (-10000), parsed = null };
            }
            for (int i = 0; i < text.Length; ++i) {
                double skipScore = scoreTable[i].score - (("ぁぃぅぇぉゃゅょっ".IndexOf(text[i]) != -1) ? -1 : 10000); // ignore lower letters
                DynamicParseResult oldSkipScore = scoreTable[i + 1];
                if (oldSkipScore.score < skipScore) {
                    oldSkipScore.score = skipScore;
                    oldSkipScore.parsed = null;
                }
                IEnumerable<EdictMatchWithType> matches = dict.findMatching(text, i);
                foreach (EdictMatchWithType match in matches) {
                    IEnumerable<InflectionState> inflections;
                    inflections = inflect.findMatching(match.matchType == EdictMatchType.FROM_KATAKANA, match.match.getAllPOS(), text, i + match.match.stemLength);
                    foreach (InflectionState inf in inflections) {
                        int totalLength = match.match.stemLength + inf.length;
                        string matchText = text.Substring(i, totalLength);
                        if (Settings.app.isWordBanned(matchText, match.matchType)) {
                            continue;
                        }
                        double baseFormMult;
                        if (match.match.entries[0].entry.getText() == matchText) {
                            baseFormMult = 1.05;
                        } else {
                            baseFormMult = 1.0;
                        }
                        double score = scoreTable[i].score + match.match.getMultiplier(inf.POS, inf.suffix.Length == 0) * getLengthMultiplier(totalLength) * baseFormMult;
                        if (parseOptions != null) {
                            score += parseOptions.bonusRating(matchText);
                        }
                        DynamicParseResult oldScore = scoreTable[i + totalLength];
                        if (oldScore.parsed != null && oldScore.parsed.asText() == matchText) {
                            (oldScore.parsed as WordParseResult).addMatch(match, inf, score);
                            if (oldScore.score < score) {
                                oldScore.score = score;
                            }
                        } else {
                            if (oldScore.score < score) {
                                oldScore.score = score;
                                oldScore.parsed = new WordParseResult(matchText, match, inf, score);
                            }
                        }
                    }
                }
            }

            IList<ParseResult> results = new List<ParseResult>();
            int x = text.Length;
            while (x > 0) {
                DynamicParseResult res = scoreTable[x];
                if (res.parsed == null) {
                    results.Add(ParseResult.unparsed(text[x - 1].ToString()));
                    x -= 1;
                } else {
                    results.Add(res.parsed);
                    x -= res.parsed.length;
                }
            }
            return ParseResult.concat(results.Reverse());
        }