public static IList <string> SegmentToWord(string text, bool ignoreCase) { if (s_HasSegmentInit == false) { lock (s_SyncSegmentObj) { if (s_HasSegmentInit == false) { string configPath = ConfigurationManager.AppSettings["PanGu_ConfigFilePath"]; if (string.IsNullOrWhiteSpace(configPath) == false) { string p = Path.GetPathRoot(configPath); if (p == null || p.Trim().Length <= 0) // 说明是相对路径 { configPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, configPath); } } else { configPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "pangu.xml"); } PanGu.Segment.Init(configPath); s_HasSegmentInit = true; } } } List <string> dicts = new List <string>(s_Dicts.Length); foreach (var w in s_Dicts) { if (text.IndexOf(w, StringComparison.CurrentCultureIgnoreCase) >= 0) { dicts.Add(w); text = text.Replace(w, string.Empty); } } PanGu.Segment segment = new PanGu.Segment(); var list = segment.DoSegment(text); List <string> rstList = new List <string>(list.Count + dicts.Count); rstList.AddRange(dicts); foreach (var w in list) { if (rstList.Contains(w.Word, ignoreCase ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture) == false) { rstList.Add(w.Word); } } return(rstList); }
/// <summary> /// Analysises the word. /// </summary> /// <returns>The word.</returns> /// <param name="text">Text.</param> public Dictionary <string, int> AnalysisWord(string text) { PanGu.Segment segment = new PanGu.Segment(); ICollection <PanGu.WordInfo> words = segment.DoSegment(text); Dictionary <string, int> result = new Dictionary <string, int>(); foreach (PanGu.WordInfo w in words) { if (w == null) { continue; } result[w.Word] = w.Rank; } return(result); }