Example #1
0
        public void InteractiveTranslate(IReadOnlyList <string> segment, out WordGraph smtWordGraph, out TranslationResult ruleResult)
        {
            Load();

            smtWordGraph  = _smtEngine.GetWordGraph(segment);
            ruleResult    = _ruleEngine?.Translate(segment);
            _lastUsedTime = DateTime.Now;
        }
 private static WordGraphDto CreateDto(WordGraph wordGraph, IReadOnlyList <string> sourceSegment)
 {
     return(new WordGraphDto
     {
         InitialStateScore = (float)wordGraph.InitialStateScore,
         FinalStates = wordGraph.FinalStates.ToArray(),
         Arcs = wordGraph.Arcs.Select(a => CreateDto(a, sourceSegment)).ToArray()
     });
 }
Example #3
0
 public void GetWordGraph_EmptySegment_ReturnsEmptyGraph()
 {
     using (var smtModel = new ThotSmtModel(TestHelpers.ToyCorpusConfigFileName))
         using (ISmtEngine engine = smtModel.CreateEngine())
         {
             WordGraph wordGraph = engine.GetWordGraph(new string[0]);
             Assert.That(wordGraph.IsEmpty, Is.True);
         }
 }
Example #4
0
            public WordGraph GetOrCreateNode(char character)
            {
                if (!Nodes.TryGetValue(character, out WordGraph wordGraph))
                {
                    Nodes[character] = wordGraph = new WordGraph(Length - 1, character);
                }

                return(wordGraph);
            }
Example #5
0
        private static void Initialize(string dictionaryFilename)
        {
            Console.WriteLine("Parsing dictionary...");
            var allWords = GetAllWords(dictionaryFilename);

            Console.WriteLine("Building word graph (this may take a few moments)...");
            var wordGraph = new WordGraph(allWords);

            Transformer = new WordTransformer(wordGraph);
        }
Example #6
0
 public ThotInteractiveTranslationSession(ThotSmtEngine engine, int n, IReadOnlyList <string> sourceSegment,
                                          WordGraph wordGraph)
 {
     _engine             = engine;
     _sourceSegment      = sourceSegment;
     _n                  = n;
     _prefix             = new List <string>();
     _isLastWordComplete = true;
     _wordGraphProcessor = new ErrorCorrectionWordGraphProcessor(_engine.ErrorCorrectionModel, _sourceSegment,
                                                                 wordGraph);
     UpdateInteractiveResults();
 }
Example #7
0
        public static void Main(string[] args)
        {
            DebugForm debugWindow = new DebugForm();

            WordGraph graph = new WordGraph();

            ImportXml.XmlToWordGraph("vocals.xml", out graph);

            List <Vocable> vocs = graph.getContent();

            foreach (Vocable v in vocs)
            {
                System.Console.WriteLine(v);
            }

            debugWindow.ShowItems(vocs);


            Application.EnableVisualStyles();
            Application.Run(debugWindow);
        }
Example #8
0
        /// <summary>
        /// 填充所有字符轮廓
        /// </summary>
        /// <param name="x">起始X</param>
        /// <param name="y">Y值</param>
        /// <param name="spacing">字符间隔</param>
        private void FillWordOutlines(int x, int y, int spacing, Font font)
        {
            string text = tbWords.Text;

            if (!string.IsNullOrEmpty(text))
            {
                foreach (char t in text)
                {
                    TreeNode wordNode = new TreeNode(t.ToString());
                    tvList.Nodes.Add(wordNode);
                    //获取字符编码
                    uint ch = GetGB2312Coding(t);

                    //获取轮廓数据
                    DOutline outline = WordGraph.GetOutline(ch, font);
                    //构建轮廓实例
                    WordOutlineDrawing word = BuildWordOutline(outline, new PointF(x, y), wordNode);
                    wordOutlines.Add(word);

                    //下个字的起始位置=当前起始位置+宽度+间隔
                    x += (int)outline.Width + spacing;
                }
            }
        }
Example #9
0
 public HybridInteractiveTranslationResult(WordGraph smtWordGraph, TranslationResult ruleResult)
 {
     SmtWordGraph = smtWordGraph;
     RuleResult   = ruleResult;
 }
Example #10
0
        public static bool XmlToWordGraph(string pathToXml, out WordGraph graph)
        {
            string exeLoc = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            try {
                XmlTextReader reader = new XmlTextReader(Path.Combine(exeLoc, "..\\..\\xml\\vocals.xml"));
                graph = new WordGraph();
                Vocable voc = default (Vocable);
                Alphabet type = default (Alphabet);
                while (reader.Read()) {
                    if (reader.NodeType == XmlNodeType.Element) {
                        switch (reader.Name) {
                            case "Vocal":
                                voc = new Vocable(null);
                                continue;
                            case "Romaji":
                                if (reader.Read())
                                    if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                        voc.Romaji = reader.Value.Trim();
                                continue;
                            case "Type":
                                if (reader.Read())
                                    if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "") {
                                        type = (Alphabet)int.Parse(reader.Value.Trim());
                                    }
                                continue;
                            case "Japanese":
                                if (reader.Read())
                                    if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                        foreach (char s in reader.Value.Trim())
                                            voc.Word.Add(new Syllable(""+s, type));
                                continue;
                            case "Kanji":
                                if (reader.Read())
                                    if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                        foreach (string obj in reader.Value.Split('\n'))
                                            if (!obj.Trim().Equals(""))
                                                voc.Kanji.AddLast(obj.Trim());
                                continue;
                            case "English":
                                if (reader.Read())
                                    if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                        foreach (string obj in reader.Value.Split('\n'))
                                            if (!obj.Trim().Equals(""))
                                                voc.EnWords.AddLast(obj.Trim());
                                continue;
                            case "German":
                                if (reader.Read())
                                    if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                        foreach (string obj in reader.Value.Split('\n'))
                                            if (!obj.Trim().Equals(""))
                                                voc.DeWords.AddLast(obj.Trim());
                                continue;
                            default:
                                continue;
                        }
                    }
                    if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Vocal")
                        graph.Add(voc);
                }
            } catch (FileNotFoundException ex) {
                Console.WriteLine(ex.Message);
                graph = null;
                return false;
            }

            return true;
        }
Example #11
0
 public Result <WordGraph.T> BuildWordGraph(string s)
 {
     return(Try(() => WordGraph.Build(s, dict.Root)));
 }
Example #12
0
 public WordGraphPath(WordGraph firstWordGraph, WordGraph secondWordGraph)
 {
     _firstWordGraph  = firstWordGraph;
     _secondWordGraph = secondWordGraph;
     _stringBuilder   = new StringBuilder();
 }
Example #13
0
        public static List <string> GraphCompute(string[] words)
        {
            // Requested length = 8? We'll only keep of words of length 2-6
            var wordGraphsPerLength = new WordGraph[requestedLength - 3];

            for (int i = 0; i < wordGraphsPerLength.Length; i++)
            {
                var length = i + 2;
                wordGraphsPerLength[i] = new WordGraph(length, ' ');
            }

            WordGraph GetWordGraph(int length)
            {
                return(wordGraphsPerLength[length - 2]);
            }

            var wordsWithRequestedLength = new List <string>();

            for (int wordIndex = 0; wordIndex < words.Length; wordIndex++)
            {
                var word       = words[wordIndex].ToLower();
                var wordLength = word.Length;
                if (wordLength == requestedLength)
                {
                    wordsWithRequestedLength.Add(word);
                    continue;
                }

                if (wordLength < 2 || wordLength > requestedLength - 2)
                {
                    continue;
                }

                var wordGraph = GetWordGraph(wordLength);
                for (var characterIndex = 0; characterIndex < wordLength; characterIndex++)
                {
                    var character = word[characterIndex];
                    wordGraph = wordGraph.GetOrCreateNode(character);
                }
            }

            //stopwatch.Reset();
            //stopwatch.Start();

            var paths = new List <WordGraphPath>(8);

            for (int lengthOfFirstWord = 2; lengthOfFirstWord < requestedLength - 1; lengthOfFirstWord++)
            {
                var lengthOfSecondWord = requestedLength - lengthOfFirstWord;
                var firstWordGraph     = GetWordGraph(lengthOfFirstWord);
                var secondWordGraph    = GetWordGraph(lengthOfSecondWord);
                var path = new WordGraphPath(firstWordGraph, secondWordGraph);
                paths.Add(path);
            }

            var combinations = new List <string>(512);

            for (var w = 0; w < wordsWithRequestedLength.Count; w++)
            {
                var word = wordsWithRequestedLength[w];
                for (var p = 0; p < paths.Count; p++)
                {
                    var path            = paths[p];
                    var wordCombination = path.Walk(word);
                    if (wordCombination != null)
                    {
                        combinations.Add(wordCombination);
                    }
                }
            }

            return(combinations);
        }
Example #14
0
        public static bool XmlToWordGraph(string pathToXml, out WordGraph graph)
        {
            string exeLoc = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            try {
                XmlTextReader reader = new XmlTextReader(Path.Combine(exeLoc, "..\\..\\xml\\vocals.xml"));
                graph = new WordGraph();
                Vocable  voc  = default(Vocable);
                Alphabet type = default(Alphabet);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                        case "Vocal":
                            voc = new Vocable(null);
                            continue;

                        case "Romaji":
                            if (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                {
                                    voc.Romaji = reader.Value.Trim();
                                }
                            }
                            continue;

                        case "Type":
                            if (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                {
                                    type = (Alphabet)int.Parse(reader.Value.Trim());
                                }
                            }
                            continue;

                        case "Japanese":
                            if (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                {
                                    foreach (char s in reader.Value.Trim())
                                    {
                                        voc.Word.Add(new Syllable("" + s, type));
                                    }
                                }
                            }
                            continue;

                        case "Kanji":
                            if (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                {
                                    foreach (string obj in reader.Value.Split('\n'))
                                    {
                                        if (!obj.Trim().Equals(""))
                                        {
                                            voc.Kanji.AddLast(obj.Trim());
                                        }
                                    }
                                }
                            }
                            continue;

                        case "English":
                            if (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                {
                                    foreach (string obj in reader.Value.Split('\n'))
                                    {
                                        if (!obj.Trim().Equals(""))
                                        {
                                            voc.EnWords.AddLast(obj.Trim());
                                        }
                                    }
                                }
                            }
                            continue;

                        case "German":
                            if (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Text && reader.Value.Trim() != "")
                                {
                                    foreach (string obj in reader.Value.Split('\n'))
                                    {
                                        if (!obj.Trim().Equals(""))
                                        {
                                            voc.DeWords.AddLast(obj.Trim());
                                        }
                                    }
                                }
                            }
                            continue;

                        default:
                            continue;
                        }
                    }
                    if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Vocal")
                    {
                        graph.Add(voc);
                    }
                }
            } catch (FileNotFoundException ex) {
                Console.WriteLine(ex.Message);
                graph = null;
                return(false);
            }

            return(true);
        }