Beispiel #1
0
        // ダウンロード後プロセス
        public void CompleteDownloadProc(Object sender, DownloadStringCompletedEventArgs e)
        {
            var htmlDoc = new HtmlAgilityPack.HtmlDocument();

            object[]     sendObjects     = (object[])e.UserState;
            logContainer sourceContainer = (logContainer)sendObjects[0];
            int          maxOwnerLength  = (int)sendObjects[1];

            try
            {
                htmlDoc.LoadHtml(e.Result);
            }
            catch
            {
                return;
            }

            /* PC版翻訳ページをパース */
            HtmlAgilityPack.HtmlNode tranNode = htmlDoc.DocumentNode.SelectSingleNode("//span[@id=\"result_box\"]");
            String tranString = tranNode.InnerText;

            tranString = HttpUtility.HtmlDecode(tranString);                           // ブラウザ上でのunicode文字列は&#の通常文字列な為変換する
            String[] delimiter = { Constants.TranslationLogDelimiter.Replace("\r", "") };
            String[] tranArray = tranString.Split(delimiter, StringSplitOptions.None); //これで1行づつになる

            int           i = 0;
            string        processedValue;
            StringBuilder stringBuilder = new StringBuilder();

            foreach (string value in tranArray)
            {
                processedValue = value;

                // 固有名詞の接置詞+specialWordList上の該当する中国語固有名詞のid+固有名詞の接置詞を日本語固有名詞文字列に置換
                processedValue = Regex.Replace(
                    processedValue,
                    Constants.SpecialWordAdposition + @"(?<m>.*?)" + Constants.SpecialWordAdposition,
                    new MatchEvaluator(MatchWordReplaceToSpecialWord),
                    RegexOptions.Singleline | RegexOptions.ExplicitCapture
                    );

                // 不要な文字の削除
                processedValue = processedValue.Replace("\r", "");

                if (sourceContainer._ownerList[i] != "")
                {
                    stringBuilder.Append(string.Format("{0:s} : {1:s}\r\n", Strings.StrConv(sourceContainer._ownerList[i], VbStrConv.Wide).PadRight(maxOwnerLength, ' '), processedValue));
                }
                else
                {
                    stringBuilder.Append(string.Format("{0:s}\r\n", processedValue));
                }
                i++;
            }
            // 出力
            dumpTextBox.AppendText(stringBuilder.ToString());
        }
Beispiel #2
0
        // 監視用タイマー
        private void watchTimer_Tick(object sender, EventArgs e)
        {
            long distanceByteSize = 0;

            Byte[] result       = new Byte[] { };
            Byte[] suteru       = new Byte[] { };
            String resultString = "";

            String nowLogFilePath = GetNewestFilePath(Settings.Instance.LogDir);

            if (nowLogFilePath == "")
            {
                MessageBox.Show("監視を設定出来ませんでした");
                return;
            }

            FileInfo sfi = new FileInfo(nowLogFilePath);
            // 未処理のファイルのサイズを取得
            long nowLogFileSize = sfi.Length;

            FileInfo fi = new FileInfo(nowLogFilePath);
            // 未処理のファイルをストリームとして扱う
            FileStream stream = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            if (nowLogFilePath != processedLogFilePath)
            { // 未処理のファイルパスと処理済ファイルパスが異なる場合、処理済ファイルサイズとパスを更新し終了
                processedLogFilePath = nowLogFilePath;
                processedLogFileSize = nowLogFileSize;
                return;
            }

            distanceByteSize = nowLogFileSize - processedLogFileSize;
            if (distanceByteSize <= 0)
            { // ファイルサイズが変わらない場合はなにもせずに終了
                return;
            }

            Array.Resize <Byte>(ref result, (int)distanceByteSize);
            // ストリームのカレント位置を処理済ファイルの位置へ移動
            stream.Seek(processedLogFileSize, SeekOrigin.Current);
            // result に差分のストリームを格納
            stream.Read(result, 0, (int)distanceByteSize);

            // 処理済のファイルのサイズを更新
            processedLogFileSize = nowLogFileSize;

            // resultはbyte配列なので、Stringに加工
            resultString = Encoding.GetEncoding("utf-16").GetString(result);
            // 最終行の改行は削除
            resultString = resultString.TrimEnd('\r', '\n');
            // 改行の後の[記号を1つのデータとして扱う為、区切り文字に置き換え
            resultString = resultString.Replace("\r\n[", Constants.TranslationLogDelimiter + "[");

            // 取得した文字列をログコンテナに格納
            logContainer sourceContainer = new logContainer(resultString, 5, 60);

            // コンテナから発言だけを抜き出して、翻訳用の文字列を区切り文字でつなぐ
            string joinedString = string.Join(Constants.TranslationLogDelimiter, sourceContainer._echoList.ToArray());

            // CTシミュレータの時刻を設定
            SetAllSecAction(sourceContainer._ownerList);

            // 文字列を自軍のCTシミュレータに送る
            SelfBarAction(joinedString);

            // 文字列を敵軍のCTシミュレータに送る
            EnamyBarAction(joinedString);

            // 中国語固有名詞を固有名詞の接置詞+specialWordList上の該当する中国語固有名詞のid+固有名詞の接置詞に置換する
            // specialWordListではなくspecialWordListOfKeyDescSortでループを回しているのは、より長い文字列で先に置換する為
            foreach (KeyValuePair <string, string> kvp in specialWordListOfKeyDescSort)
            {
                // 中国語固有名詞を固有名詞の接置詞+specialWordList上の該当する中国語固有名詞のid+固有名詞の接置詞に置換
                joinedString = joinedString.Replace(kvp.Key, Constants.SpecialWordAdposition + specialWordList.IndexOfKey(kvp.Key) + Constants.SpecialWordAdposition);
            }

            // コンテナから発言者だけ抜き出して、ループを回す
            foreach (string owner in sourceContainer._ownerList)
            {
                if (maxOwnerLength < owner.Length)
                { // 発言者の文字長で、最大文字長の物が出現した時は更新する
                    maxOwnerLength = owner.Length;
                }
            }

            // WEBクライアントに持っていくデータをsendObjectsに纏める
            object[] sendObjects = new object[2];
            sendObjects[0] = sourceContainer;
            sendObjects[1] = maxOwnerLength;

            // TODO:wc.Encoding = Encoding.UTF8;って本当は書くべきなんだけど書くと文字化けするから書かない。調べなくちゃね。
            WebClient wc = new WebClient();

            try
            {
                wc.DownloadStringCompleted += CompleteDownloadProc; // ダウンロードが終わるとCompleteDownloadProcが呼び出される
                wc.DownloadStringAsync(new Uri("https://translate.google.com/?hl=ja&sl=zh-CN&tl=ja&q=" + Uri.EscapeUriString(joinedString)), sendObjects);
            }
            catch (WebException exc)
            {
                Console.WriteLine(exc.ToString());
                return;
            }
        }