Esempio n. 1
0
        private void SelectedDataEventHandler(object sender, HookParam hp)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                if (!Properties.Settings.Default.OnlyMachineTranslation)
                {
                    DisplayTextCollection.Clear();

                    var pattern = SimpleIoc.Default.GetInstance <GameInfo>().Regexp;
                    if (!string.IsNullOrEmpty(pattern))
                    {
                        var list = Regex.Split(hp.Text, pattern);
                        hp.Text  = string.Join("", list);
                    }

                    if (hp.Text.Length > 80)
                    {
                        hp.Text = "长度大于80的文本自动跳过";
                    }

                    var mecabWordList = _mecabHelper.SentenceHandle(hp.Text);
                    foreach (MecabWordInfo mecabWord in mecabWordList)
                    {
                        DisplayTextCollection.Add(new SingleTextItem
                        {
                            Text             = mecabWord.Word,
                            RubyText         = mecabWord.Kana,
                            PartOfSpeed      = mecabWord.PartOfSpeech,
                            TextTemplateType = TextTemplateConfig
                        });
                    }

                    if (lastSentence == "")
                    {
                        currentSentence = hp.Text;
                    }
                    else
                    {
                        lastSentence    = currentSentence;
                        currentSentence = hp.Text;
                    }

                    TransText        = "";
                    TransTextVisible = Visibility.Collapsed;
                    // XXX: learn Task first
                    Task.Run(async() => await DoPreTranslateAsync());
                }
                else
                {
                    TranslateTextList.Clear();
                    if (hp.Text.Length > 80)
                    {
                        TranslateTextList.Add("长度大于80的文本自动跳过");
                        return;
                    }

                    TrasnlateAllAsync(hp.Text, Utils.GetTranslatorList());
                }
            });
        }
Esempio n. 2
0
        private void OCR()
        {
            if (IsPauseFlag)
            {
                if (IsOCRingFlag == false)
                {
                    IsOCRingFlag = true;

                    int j = 0;

                    for (; j < 3; j++)
                    {
                        Thread.Sleep(Common.UsingOCRDelay);

                        string srcText = Common.ocr.OCRProcess();
                        GC.Collect();

                        if (!string.IsNullOrEmpty(srcText))
                        {
                            Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                //0.清除面板
                                SourceTextPanel.Children.Clear();

                                //1.得到原句
                                string source = srcText;

                                _currentsrcText = source;

                                if (_isShowSource)
                                {
                                    //3.分词
                                    List <MecabWordInfo> mwi = _mecabHelper.SentenceHandle(source);
                                    //分词后结果显示
                                    for (int i = 0; i < mwi.Count; i++)
                                    {
                                        StackPanel stackPanel = new StackPanel();
                                        stackPanel.Orientation = Orientation.Vertical;
                                        stackPanel.Margin = new Thickness(10, 0, 0, 10);

                                        TextBlock textBlock = new TextBlock();
                                        if (!string.IsNullOrEmpty(SourceTextFont))
                                        {
                                            FontFamily fontFamily = new FontFamily(SourceTextFont);
                                            textBlock.FontFamily = fontFamily;
                                        }
                                        textBlock.Text = mwi[i].Word;
                                        textBlock.Tag = mwi[i].Kana;
                                        textBlock.Margin = new Thickness(0, 0, 0, 0);
                                        textBlock.FontSize = SourceTextFontSize;
                                        textBlock.Background = Brushes.Transparent;
                                        textBlock.MouseLeftButtonDown += DictArea_MouseLeftButtonDown;
                                        //根据不同词性跟字体上色
                                        switch (mwi[i].PartOfSpeech)
                                        {
                                        case "名詞":
                                            textBlock.Foreground = Brushes.AliceBlue;
                                            break;

                                        case "助詞":
                                            textBlock.Foreground = Brushes.LightGreen;
                                            break;

                                        case "動詞":
                                            textBlock.Foreground = Brushes.Red;
                                            break;

                                        case "連体詞":
                                            textBlock.Foreground = Brushes.Orange;
                                            break;

                                        default:
                                            textBlock.Foreground = Brushes.White;
                                            break;
                                        }


                                        TextBlock superScript = new TextBlock();//假名或注释等的上标标签
                                        if (!string.IsNullOrEmpty(SourceTextFont))
                                        {
                                            FontFamily fontFamily = new FontFamily(SourceTextFont);
                                            superScript.FontFamily = fontFamily;
                                        }
                                        superScript.Text = mwi[i].Kana;
                                        superScript.Margin = new Thickness(0, 0, 0, 2);
                                        superScript.HorizontalAlignment = HorizontalAlignment.Center;
                                        if ((double)SourceTextFontSize - 6.5 > 0)
                                        {
                                            superScript.FontSize = (double)SourceTextFontSize - 6.5;
                                        }
                                        else
                                        {
                                            superScript.FontSize = 1;
                                        }
                                        superScript.Background = Brushes.Transparent;
                                        superScript.Foreground = Brushes.White;
                                        stackPanel.Children.Add(superScript);


                                        //是否打开假名标注
                                        if (Common.appSettings.TF_isKanaShow)
                                        {
                                            stackPanel.Children.Add(textBlock);
                                            SourceTextPanel.Children.Add(stackPanel);
                                        }
                                        else
                                        {
                                            textBlock.Margin = new Thickness(10, 0, 0, 10);
                                            SourceTextPanel.Children.Add(textBlock);
                                        }
                                    }
                                }

                                if (Convert.ToBoolean(Common.appSettings.EachRowTrans) == false)
                                {
                                    //不需要分行翻译
                                    source = source.Replace("<br>", "").Replace("</br>", "").Replace("\n", "").Replace("\t", "").Replace("\r", "");
                                }
                                //去乱码
                                source = source.Replace("_", "").Replace("-", "").Replace("+", "").Replace("&", "");

                                //4.翻译前预处理
                                string beforeString = _beforeTransHandle.AutoHandle(source);

                                //5.提交翻译
                                string transRes1 = string.Empty;
                                string transRes2 = string.Empty;
                                if (_translator1 != null)
                                {
                                    transRes1 = _translator1.Translate(beforeString, Common.UsingDstLang, Common.UsingSrcLang);
                                }
                                if (_translator2 != null)
                                {
                                    transRes2 = _translator2.Translate(beforeString, Common.UsingDstLang, Common.UsingSrcLang);
                                }

                                //6.翻译后处理
                                string afterString1 = _afterTransHandle.AutoHandle(transRes1);
                                string afterString2 = _afterTransHandle.AutoHandle(transRes2);

                                //7.翻译结果显示到窗口上
                                FirstTransText.Text = afterString1;
                                SecondTransText.Text = afterString2;

                                //8.翻译结果记录到队列
                                if (_gameTextHistory.Count > 5)
                                {
                                    _gameTextHistory.Dequeue();
                                }
                                _gameTextHistory.Enqueue(source + "\n" + afterString1 + "\n" + afterString2);

                                //9.翻译原句和结果记录到数据库
                                if (Common.appSettings.ATon)
                                {
                                    bool addRes = _artificialTransHelper.AddTrans(source, afterString1);
                                    if (addRes == false)
                                    {
                                        HandyControl.Data.GrowlInfo growlInfo = new HandyControl.Data.GrowlInfo();
                                        growlInfo.Message = Application.Current.Resources["ArtificialTransAdd_Error_Hint"].ToString();
                                        growlInfo.WaitTime = 2;
                                        Growl.InfoGlobal(growlInfo);
                                    }
                                }
                            }));

                            IsOCRingFlag = false;
                            break;
                        }
                    }

                    if (j == 3)
                    {
                        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            FirstTransText.Text = "[OCR]自动识别三次均为空,请自行刷新!";
                        }));

                        IsOCRingFlag = false;
                    }
                }
            }
        }
        private void OCR()
        {
            if (IsPauseFlag)
            {
                if (IsOCRingFlag == false)
                {
                    IsOCRingFlag = true;

                    int j = 0;

                    for (; j < 3; j++)
                    {
                        Thread.Sleep(Common.UsingOCRDelay);

                        string srcText = Common.ocr.OCRProcess();
                        GC.Collect();

                        if (srcText != null && srcText != "")
                        {
                            Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                //0.清除面板
                                SourceTextPanel.Children.Clear();

                                //1.得到原句
                                string source = srcText;

                                currentsrcText = source;

                                if (IsShowSource == true)
                                {
                                    //3.分词
                                    List <MecabWordInfo> mwi = mh.SentenceHandle(source);
                                    //分词后结果显示
                                    for (int i = 0; i < mwi.Count; i++)
                                    {
                                        TextBlock tb = new TextBlock();
                                        if (sourceTextFont != null && sourceTextFont != "")
                                        {
                                            FontFamily ff = new FontFamily(sourceTextFont);
                                            tb.FontFamily = ff;
                                        }
                                        tb.Text = mwi[i].Word;
                                        tb.Margin = new Thickness(10, 0, 0, 10);
                                        tb.FontSize = sourceTextFontSize;
                                        //根据不同词性跟字体上色
                                        switch (mwi[i].PartOfSpeech)
                                        {
                                        case "名詞":
                                            tb.Foreground = Brushes.AliceBlue;
                                            break;

                                        case "助詞":
                                            tb.Foreground = Brushes.LightGreen;
                                            break;

                                        case "動詞":
                                            tb.Foreground = Brushes.Red;
                                            break;

                                        case "連体詞":
                                            tb.Foreground = Brushes.Orange;
                                            break;

                                        default:
                                            tb.Foreground = Brushes.White;
                                            break;
                                        }
                                        SourceTextPanel.Children.Add(tb);
                                    }
                                }

                                if (Convert.ToBoolean(Common.appSettings.EachRowTrans) == true)
                                {
                                    //需要分行翻译
                                    source = source.Replace("<br>", "").Replace("</br>", "").Replace("\n", "").Replace("\t", "").Replace("\r", "");
                                }
                                //去乱码
                                source = source.Replace("_", "").Replace("-", "").Replace("+", "");

                                //4.翻译前预处理
                                string beforeString = bth.AutoHandle(source);

                                //5.提交翻译
                                string transRes1 = "";
                                string transRes2 = "";
                                if (translator1 != null)
                                {
                                    transRes1 = translator1.Translate(beforeString, Common.UsingDstLang, Common.UsingSrcLang);
                                }
                                if (translator2 != null)
                                {
                                    transRes2 = translator2.Translate(beforeString, Common.UsingDstLang, Common.UsingSrcLang);
                                }

                                //6.翻译后处理
                                string afterString1 = ath.AutoHandle(transRes1);
                                string afterString2 = ath.AutoHandle(transRes2);

                                //7.翻译结果显示到窗口上
                                FirstTransText.Text = afterString1;
                                SecondTransText.Text = afterString2;

                                //8.翻译结果记录到队列
                                if (GameTextHistory.Count > 5)
                                {
                                    GameTextHistory.Dequeue();
                                }
                                GameTextHistory.Enqueue(source + "\n" + afterString1 + "\n" + afterString2);
                            }));

                            IsOCRingFlag = false;
                            break;
                        }
                    }

                    if (j == 3)
                    {
                        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            FirstTransText.Text = "[OCR]自动识别三次均为空,请自行刷新!";
                        }));

                        IsOCRingFlag = false;
                    }
                }
            }
        }
Esempio n. 4
0
        private void OCR()
        {
            if (IsPauseFlag)
            {
                if (IsOCRingFlag == false)
                {
                    IsOCRingFlag = true;

                    int j = 0;

                    for (; j < 3; j++)
                    {
                        Thread.Sleep(Common.UsingOCRDelay);

                        string srcText = Common.ocr.OCRProcess();
                        GC.Collect();

                        if (!string.IsNullOrEmpty(srcText))
                        {
                            Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                //0.清除面板
                                SourceTextPanel.Children.Clear();

                                //1.得到原句
                                string source = srcText;

                                _currentsrcText = source;

                                if (_isShowSource)
                                {
                                    //3.分词
                                    List <MecabWordInfo> mwi = _mecabHelper.SentenceHandle(source);
                                    //分词后结果显示
                                    for (int i = 0; i < mwi.Count; i++)
                                    {
                                        TextBlock textBlock = new TextBlock();
                                        if (!string.IsNullOrEmpty(SourceTextFont))
                                        {
                                            FontFamily fontFamily = new FontFamily(SourceTextFont);
                                            textBlock.FontFamily = fontFamily;
                                        }
                                        textBlock.Text = mwi[i].Word;
                                        textBlock.Tag = mwi[i].Kana;
                                        textBlock.Margin = new Thickness(10, 0, 0, 10);
                                        textBlock.FontSize = SourceTextFontSize;
                                        textBlock.Background = Brushes.Transparent;
                                        textBlock.MouseLeftButtonDown += DictArea_MouseLeftButtonDown;
                                        //根据不同词性跟字体上色
                                        switch (mwi[i].PartOfSpeech)
                                        {
                                        case "名詞":
                                            textBlock.Foreground = Brushes.AliceBlue;
                                            break;

                                        case "助詞":
                                            textBlock.Foreground = Brushes.LightGreen;
                                            break;

                                        case "動詞":
                                            textBlock.Foreground = Brushes.Red;
                                            break;

                                        case "連体詞":
                                            textBlock.Foreground = Brushes.Orange;
                                            break;

                                        default:
                                            textBlock.Foreground = Brushes.White;
                                            break;
                                        }

                                        SourceTextPanel.Children.Add(textBlock);
                                    }
                                }

                                if (Convert.ToBoolean(Common.appSettings.EachRowTrans))
                                {
                                    //需要分行翻译
                                    source = source.Replace("<br>", string.Empty).Replace("</br>", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty).Replace("\r", string.Empty);
                                }
                                //去乱码
                                source = source.Replace("_", string.Empty).Replace("-", string.Empty).Replace("+", string.Empty);

                                //4.翻译前预处理
                                string beforeString = _beforeTransHandle.AutoHandle(source);

                                //5.提交翻译
                                string transRes1 = string.Empty;
                                string transRes2 = string.Empty;
                                if (_translator1 != null)
                                {
                                    transRes1 = _translator1.Translate(beforeString, Common.UsingDstLang, Common.UsingSrcLang);
                                }
                                if (_translator2 != null)
                                {
                                    transRes2 = _translator2.Translate(beforeString, Common.UsingDstLang, Common.UsingSrcLang);
                                }

                                //6.翻译后处理
                                string afterString1 = _afterTransHandle.AutoHandle(transRes1);
                                string afterString2 = _afterTransHandle.AutoHandle(transRes2);

                                //7.翻译结果显示到窗口上
                                FirstTransText.Text = afterString1;
                                SecondTransText.Text = afterString2;

                                //8.翻译结果记录到队列
                                if (_gameTextHistory.Count > 5)
                                {
                                    _gameTextHistory.Dequeue();
                                }
                                _gameTextHistory.Enqueue(source + "\n" + afterString1 + "\n" + afterString2);
                            }));

                            IsOCRingFlag = false;
                            break;
                        }
                    }

                    if (j == 3)
                    {
                        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            FirstTransText.Text = "[OCR]自动识别三次均为空,请自行刷新!";
                        }));

                        IsOCRingFlag = false;
                    }
                }
            }
        }
        /// <summary>
        /// 更新原文
        /// 注意执行过程中调用了StackPanel等UI组件,必须交回主线程才能执行。
        /// </summary>
        /// <param name="repairedText">原文</param>
        private void UpdateSource(string repairedText)
        {
            if (_isShowSource)
            {
                // 使用BeginInvoke,在更新原文时可以去获取翻译
                Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    //3.分词
                    var mwi = _mecabHelper.SentenceHandle(repairedText);
                    //分词后结果显示
                    for (int i = 0; i < mwi.Count; i++)
                    {
                        StackPanel stackPanel = new StackPanel();
                        stackPanel.Orientation = Orientation.Vertical;
                        stackPanel.Margin = new Thickness(10, 0, 0, 10);

                        TextBlock textBlock = new TextBlock();
                        if (!string.IsNullOrEmpty(SourceTextFont))
                        {
                            FontFamily fontFamily = new FontFamily(SourceTextFont);
                            textBlock.FontFamily = fontFamily;
                        }
                        textBlock.Text = mwi[i].Word;
                        textBlock.Tag = mwi[i].Kana;
                        textBlock.Margin = new Thickness(0, 0, 0, 0);
                        textBlock.FontSize = SourceTextFontSize;
                        textBlock.Background = Brushes.Transparent;
                        textBlock.MouseLeftButtonDown += DictArea_MouseLeftButtonDown;
                        //根据不同词性跟字体上色
                        switch (mwi[i].PartOfSpeech)
                        {
                        case "名詞":
                            textBlock.Foreground = Brushes.AliceBlue;
                            break;

                        case "助詞":
                            textBlock.Foreground = Brushes.LightGreen;
                            break;

                        case "動詞":
                            textBlock.Foreground = Brushes.Red;
                            break;

                        case "連体詞":
                            textBlock.Foreground = Brushes.Orange;
                            break;

                        default:
                            textBlock.Foreground = Brushes.White;
                            break;
                        }


                        TextBlock superScript = new TextBlock();//假名或注释等的上标标签
                        if (!string.IsNullOrEmpty(SourceTextFont))
                        {
                            FontFamily fontFamily = new FontFamily(SourceTextFont);
                            superScript.FontFamily = fontFamily;
                        }
                        superScript.Text = mwi[i].Kana;
                        superScript.Margin = new Thickness(0, 0, 0, 2);
                        superScript.HorizontalAlignment = HorizontalAlignment.Center;
                        if ((double)SourceTextFontSize - 6.5 > 0)
                        {
                            superScript.FontSize = (double)SourceTextFontSize - 6.5;
                        }
                        else
                        {
                            superScript.FontSize = 1;
                        }
                        superScript.Background = Brushes.Transparent;
                        superScript.Foreground = Brushes.White;
                        stackPanel.Children.Add(superScript);


                        //是否打开假名标注
                        if (Common.appSettings.TF_isKanaShow)
                        {
                            stackPanel.Children.Add(textBlock);
                            SourceTextPanel.Children.Add(stackPanel);
                        }
                        else
                        {
                            textBlock.Margin = new Thickness(10, 0, 0, 10);
                            SourceTextPanel.Children.Add(textBlock);
                        }
                    }
                }));
            }
        }