Beispiel #1
0
 public void SaveFormerResultFile(SettingManager.TransType transType)
 {
     if (!isSaving)
     {
         Task task = RunSaveFormerResultFile(transType);
     }
 }
Beispiel #2
0
 public void ClearFormerResultFile(SettingManager.TransType transType)
 {
     if (!isSaving)
     {
         string saveData = "";
         string path     = string.Format(GlobalDefine.FORMER_TRANS_FILE, transType.ToString());
         Util.SaveFile(path, saveData);
     }
 }
Beispiel #3
0
        private void LoadFormerResultFile(SettingManager.TransType transType)
        {
            resultDic[transType].Clear();
            saveResultDic[transType].Clear();

            Dictionary <string, string> dic = resultDic[transType];
            string path = string.Format(GlobalDefine.FORMER_TRANS_FILE, transType.ToString());

            using (StreamReader r = Util.OpenFile(path))
            {
                if (r != null)
                {
                    string line = "";

                    string ocr    = "";
                    string result = "";

                    FileStepType eStep = FileStepType.None;

                    while ((line = r.ReadLine()) != null)
                    {
                        if (line == "/s")
                        {
                            ocr    = "";
                            result = "";
                            eStep  = FileStepType.OCR;
                        }
                        else if (line == "/t")
                        {
                            eStep = FileStepType.Trans;
                        }
                        else if (line == "/e")
                        {
                            eStep = FileStepType.End;

                            if (ocr != "" & result != "")
                            {
                                dic[ocr] = result;
                            }
                        }
                        else
                        {
                            if (eStep == FileStepType.OCR)
                            {
                                ocr += line;
                            }
                            else if (eStep == FileStepType.Trans)
                            {
                                result += line;
                            }
                        }
                    }
                }

                r.Close();
            }
        }
Beispiel #4
0
            /// <summary>
            /// 번역 결과 초기화.
            /// </summary>
            /// <param name="transString"></param>
            public void InitTransResult(string transString, SettingManager.TransType transType)
            {
                this.transString = transString;

                string[] separatingStrings = { Util.GetSpliteToken(transType) };
                string[] words             = this.transString.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < transDataList.Count && i < words.Length; i++)
                {
                    transDataList[i].trans = words[i];
                }
            }
Beispiel #5
0
        public async Task <string> StartTrans(string text, SettingManager.TransType trasType)
        {
            if (text == "")
            {
                return("");
            }

            Task <string> task1 = Task <string> .Run(() => GetTrans2(text, trasType));

            string result = await task1;

            return(result);
        }
Beispiel #6
0
 //ocr 및 번역 결과 처리
 public void updateText(string transText, string ocrText, SettingManager.TransType transType, bool isShowOCRResultFlag, bool isSaveOCRFlag)
 {
     try
     {
         this.BeginInvoke(new myDelegate(updateProgress), new object[] { transText, ocrText, isShowOCRResultFlag, isSaveOCRFlag });
     }
     catch (InvalidOperationException)
     {
         // Error logging, post processing etc.
         return;
     }
     this.BeginInvoke(new Action(UpdatePaint));
     //  UpdatePaint();
 }
Beispiel #7
0
        private void AddFormerResult(SettingManager.TransType transType, string ocrValue, string result)
        {
            if (!isSaving)
            {
                if (resultDic[transType].Count >= MAX_FORMER)
                {
                    resultDic[transType].Clear();
                    saveResultDic[transType].Clear();
                    //파일을 열고 다 지운다.

                    ClearFormerResultFile(transType);
                }

                resultDic[transType][ocrValue] = result;
                saveResultDic[transType].Add(new KeyValuePair <string, string>(ocrValue, result));
            }
        }
Beispiel #8
0
        private async Task RunSaveFormerResultFile(SettingManager.TransType transType)
        {
            isSaving = true;

            await Task.Run(() =>
            {
                try
                {
                    string saveData = "";
                    string path     = string.Format(GlobalDefine.FORMER_TRANS_FILE, transType.ToString());

                    List <KeyValuePair <string, string> > list = null;

                    if (saveResultDic.ContainsKey(transType))
                    {
                        list = saveResultDic[transType];
                    }


                    if (list != null && list.Count > 0)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            string data = "/s";
                            data       += Environment.NewLine + list[i].Key + Environment.NewLine + "/t" + Environment.NewLine + list[i].Value + Environment.NewLine + "/e";

                            saveData += data + Environment.NewLine + Environment.NewLine;
                        }

                        Util.SaveFile(path, saveData, true);
                        list.Clear();
                    }
                }
                catch
                {
                }

                isSaving = false;
            }).ConfigureAwait(true);
        }
Beispiel #9
0
        private string GetFormerResult(SettingManager.TransType transType, string ocrValue)
        {
            string result = null;

            if (!isSaving)
            {
                bool isFound = false;
                if (!resultDic.ContainsKey(transType))
                {
                    resultDic.Add(transType, new Dictionary <string, string>());
                }

                if (resultDic[transType].TryGetValue(ocrValue, out result))
                {
                    isFound = true;
                }
            }



            return(result);
        }
Beispiel #10
0
        public static string GetSpliteToken(SettingManager.TransType transType)
        {
            string token = "";

            switch (transType)
            {
            case SettingManager.TransType.google:
            case SettingManager.TransType.google_url:
                token = GlobalDefine.SPLITE_TOEKN_GOOGLE;
                break;

            case SettingManager.TransType.naver:
                token = GlobalDefine.SPLITE_TOEKN_NAVER;
                break;

            default:
                token = GlobalDefine.SPLITE_TOEKN_NAVER;
                break;
            }

            token += System.Environment.NewLine;
            return(token);
        }
Beispiel #11
0
 //ocr 및 번역 결과 처리
 public void updateText(string transText, string ocrText, SettingManager.TransType transType, bool isShowOCRResultFlag, bool isSaveOCRFlag)
 {
     try
     {
         if (thread != null)
         {
             thread.Join();
         }
         try
         {
             this.BeginInvoke(new myDelegate(updateProgress), new object[] { transText, ocrText, isShowOCRResultFlag, isSaveOCRFlag });
         }
         catch (InvalidOperationException)
         {
             // Error logging, post processing etc.
             return;
         }
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
Beispiel #12
0
        public async Task <string> StartTrans(string text, SettingManager.TransType trasType, List <string> textList = null)
        {
            if (text == "")
            {
                return("");
            }


            Task <string> task1 = null;

            if (textList == null)
            {
                task1 = Task <string> .Run(() => GetTrans2(text, trasType));
            }
            else
            {
                task1 = Task <string> .Run(() => GetTransLines(textList, trasType));
            }
            string result = await task1;


            return(result);
        }
Beispiel #13
0
        public async Task <string> GetTrans2(string text, SettingManager.TransType transType)
        {
            try
            {
                bool isError   = false;
                bool isContain = false;

                string formerResult = null;

                if (transType != SettingManager.TransType.db)
                {
                    formerResult = GetFormerResult(transType, text);

                    if (string.IsNullOrEmpty(formerResult))
                    {
                        isContain = false;
                    }
                    else
                    {
                        isContain = true;
                    }
                }

                string result = "";

                if (!isContain)
                {
                    if (transType == SettingManager.TransType.db)
                    {
                        StringBuilder sb  = new StringBuilder(text, 8192);
                        StringBuilder sb2 = new StringBuilder(8192);
                        Form1.ProcessGetDBText(sb, sb2);
                        result = sb2.ToString();
                    }
                    else
                    {
                        if (transType == SettingManager.TransType.naver)
                        {
                            result = NaverTranslateAPI.instance.GetResult(text, ref isError);
                            result = result.Replace("\r\n ", System.Environment.NewLine);
                        }
                        else if (transType == SettingManager.TransType.google)
                        {
                            result = sheets.Translate(text, ref isError);
                            result = result.Replace("\r\n ", System.Environment.NewLine);
                        }
                        else if (transType == SettingManager.TransType.google_url)
                        {
                            result = GoogleBasicTranslateAPI.instance.GetResult(text, ref isError);
                        }
                    }

                    if (!isError && transType != SettingManager.TransType.db)
                    {
                        AddFormerResult(transType, text, result);
                    }
                }
                else
                {
                    if (Form1.isShowFormerResultLog)
                    {
                        result = "[기억 결과 " + resultDic[transType].Count.ToString() + " ] " + formerResult;
                    }
                    else
                    {
                        result = formerResult;
                    }
                }

                return(result);
            }
            catch (Exception e)
            {
                return("Error " + e);
            }
        }
Beispiel #14
0
        public async Task <string> GetTrans2(string text, SettingManager.TransType trasType)
        {
            try
            {
                bool isError = false;

                bool isContain = false;

                if (trasType != SettingManager.TransType.db)
                {
                    //text = text.Replace(System.Environment.NewLine, " ");

                    isContain = formerResultDic.ContainsKey(text);
                }

                string result = "";

                if (!isContain)
                {
                    //trasType = SettingManager.TransType.google;
                    if (trasType == SettingManager.TransType.db)
                    {
                        StringBuilder sb  = new StringBuilder(text, 8192);
                        StringBuilder sb2 = new StringBuilder(8192);
                        Form1.ProcessGetDBText(sb, sb2);
                        result = sb2.ToString();
                    }
                    else
                    {
                        /*
                         * 2020 년 8월 15일 이후로 지원하지 않음.
                         * if (trasType == SettingManager.TransType.yandex)
                         * {
                         *  result = YandexAPI.instance.GetResult(text, ref isError);
                         * }
                         */
                        if (trasType == SettingManager.TransType.naver)
                        {
                            result = NaverTranslateAPI.instance.GetResult(text, ref isError);
                            result = result.Replace("\r\n ", System.Environment.NewLine);
                        }
                        else if (trasType == SettingManager.TransType.google)
                        {
                            result = sheets.Translate(text, ref isError);
                            result = result.Replace("\r\n ", System.Environment.NewLine);
                        }
                        else if (trasType == SettingManager.TransType.google_url)
                        {
                            result = GoogleBasicTranslateAPI.instance.GetResult(text, ref isError);
                        }
                    }


                    if (!isError && trasType != SettingManager.TransType.db)
                    {
                        formerResultDic.Add(text, result);

                        if (formerResultDic.Count > 5000)
                        {
                            formerResultDic.Clear();
                        }
                    }
                }
                else
                {
                    result = formerResultDic[text];
                }



                return(result);
            }
            catch (Exception e)
            {
                return("Error " + e);
            }
        }
Beispiel #15
0
        public async Task <string> GetTransLines(List <string> textList, SettingManager.TransType transType)
        {
            int removeLength = System.Environment.NewLine.Length;
            Dictionary <int, TransData> textDic = new Dictionary <int, TransData>();

            for (int i = 0; i < textList.Count; i++)
            {
                TransData data = new TransData();
                data.index = i;
                data.text  = textList[i].TrimEnd();


                data.result = "";

                textDic.Add(data.index, data);
            }

            string text = "";

            try
            {
                bool isError   = false;
                bool isContain = false;

                string formerResult = null;

                if (transType != SettingManager.TransType.db)
                {
                    string require = "";
                    foreach (var obj in textDic)
                    {
                        obj.Value.result = GetFormerResult(transType, obj.Value.text);

                        if (string.IsNullOrEmpty(obj.Value.result))
                        {
                            obj.Value.result = "";
                            require         += Util.GetSpliteToken(transType) + obj.Value.text + System.Environment.NewLine;
                        }
                        else
                        {
                            if (Form1.isDebugShowFormerResultLog)
                            {
                                obj.Value.result = "[기억 결과 " + resultDic[transType].Count.ToString() + " ] " + obj.Value.result;
                            }
                        }
                    }

                    if (require != "")
                    {
                        string transResult = "";
                        if (transType == SettingManager.TransType.naver)
                        {
                            transResult = NaverTranslateAPI.instance.GetResult(require, ref isError);
                            transResult = transResult.Replace("\r\n ", System.Environment.NewLine);
                        }
                        else if (transType == SettingManager.TransType.google)
                        {
                            transResult = sheets.Translate(require, ref isError);
                            transResult = transResult.Replace("\r\n ", System.Environment.NewLine);
                        }
                        else if (transType == SettingManager.TransType.google_url)
                        {
                            transResult = GoogleBasicTranslateAPI.instance.GetResult(require, ref isError);
                        }


                        if (isError)
                        {
                            //문제가 있으면 바로 끝낸다.
                            return(transResult);
                        }
                        else
                        {
                            string[] separatingStrings = { Util.GetSpliteToken(transType) };
                            string[] words             = transResult.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);

                            int index = 0;
                            foreach (var obj in textDic)
                            {
                                if (string.IsNullOrEmpty(obj.Value.result))
                                {
                                    if (words.Length > index)
                                    {
                                        obj.Value.result = words[index++];

                                        AddFormerResult(transType, obj.Value.text, obj.Value.result);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    foreach (var obj in textDic)
                    {
                        StringBuilder sb  = new StringBuilder(obj.Value.text, 8192);
                        StringBuilder sb2 = new StringBuilder(8192);
                        Form1.ProcessGetDBText(sb, sb2);

                        obj.Value.result = sb2.ToString();

                        if (obj.Value.result == "not thing")
                        {
                            obj.Value.result = "";
                        }
                    }
                }
                string result = "";

                foreach (var obj in textDic)
                {
                    result += Util.GetSpliteToken(transType) + obj.Value.result + System.Environment.NewLine;
                }
                return(result);
            }
            catch (Exception e)
            {
                return("Error " + e);
            }
        }