繁簡轉換程式

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

using System;
using System.Collections.Generic;
using System.Text;

namespace OpenCL
{
    class ChineseConverter
    {
        Dict s2t, t2s;

        public static void Main(String[] args) {
            ChineseConverter converter = new ChineseConverter();
            String corpus = TextFile.fileToText(@"corpus/1500.txt");
            corpus = STR.expand(corpus, "\r=|\n=|\b=");
            String[] lines = corpus.Split('。');
            int i = 0;
            foreach (String line in lines)
            {
                String tline = converter.simplifiedToTraditional(line);
                Console.WriteLine(i + ":" + tline);
                i++;
            }
        }

        public ChineseConverter() {
            t2s = new Dict("dictionary/繁簡字對照表.txt");
            s2t = new Dict();
            foreach (KeyValuePair<String, String> pair in t2s)
            {
                if (!s2t.ContainsKey(pair.Value))
                    s2t.Add(pair.Value, pair.Key);
            }
        }

        public String simplifiedToTraditional(String simplifiedText)
        {
            StringBuilder traditionalText = new StringBuilder();
            for (int i=0; i<simplifiedText.Length; i++) 
            {
                String ch = simplifiedText[i]+"";
                if (s2t.ContainsKey(ch))
                    traditionalText.Append(s2t[ch]);
                else
                    traditionalText.Append(ch);
            }
            return traditionalText.ToString();
        }

        public String traditionalToSimplified(String traditionalText)
        {
            StringBuilder simplifiedText = new StringBuilder();
            for (int i = 0; i < traditionalText.Length; i++)
            {
                String ch = simplifiedText[i] + "";
                if (t2s.ContainsKey(ch))
                    simplifiedText.Append(t2s[ch]);
                else
                    simplifiedText.Append(ch);
            }
            return simplifiedText.ToString();
        }
    }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License