public virtual List <string> Cut(string sentence) { Route route = RouteGenerator.GetRoutes(sentence); var result = new List <string>(); var sb = new StringBuilder(); for (var start = 0; start < sentence.Length; start = route.Segments[start].To + 1) { string word = sentence.Sub(start, route.Segments[start].To); if (start == route.Segments[start].To) { sb.Append(word); } else { ResolveGapWords(result, sb); result.Add(word); } } ResolveGapWords(result, sb); return(result); }
public void TestGetBestRoute() { WordDictionary.Instance.LoadDictWords(@"..\..\test_files\dict2.txt"); const string sentence = @"他来到了浙江省网易杭研大厦"; var result = _routeGeneratorgenerator.GetRoutes(sentence); Assert.IsNotNull(result); var routes = result.Segments; Assert.AreEqual(14, routes.Length); Assert.AreEqual(0, routes[0].To); Assert.AreEqual(2, routes[1].To); Assert.AreEqual(2, routes[2].To); Assert.AreEqual(3, routes[3].To); Assert.AreEqual(6, routes[4].To); Assert.AreEqual(5, routes[5].To); Assert.AreEqual(6, routes[6].To); Assert.AreEqual(8, routes[7].To); Assert.AreEqual(8, routes[8].To); Assert.AreEqual(9, routes[9].To); Assert.AreEqual(10, routes[10].To); Assert.AreEqual(12, routes[11].To); Assert.AreEqual(12, routes[12].To); }