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() }); }
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); } }
public WordGraph GetOrCreateNode(char character) { if (!Nodes.TryGetValue(character, out WordGraph wordGraph)) { Nodes[character] = wordGraph = new WordGraph(Length - 1, character); } return(wordGraph); }
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); }
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(); }
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); }
/// <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; } } }
public HybridInteractiveTranslationResult(WordGraph smtWordGraph, TranslationResult ruleResult) { SmtWordGraph = smtWordGraph; RuleResult = ruleResult; }
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; }
public Result <WordGraph.T> BuildWordGraph(string s) { return(Try(() => WordGraph.Build(s, dict.Root))); }
public WordGraphPath(WordGraph firstWordGraph, WordGraph secondWordGraph) { _firstWordGraph = firstWordGraph; _secondWordGraph = secondWordGraph; _stringBuilder = new StringBuilder(); }
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); }
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); }