我正在阅读文档,并分割单词以获取字典中的每个单词,但我怎么能排除一些单词(如“/ a / an”).
这是我的功能:
private void Splitter(string[] file) { try { tempDict = file .SelectMany(i => File.ReadAllLines(i) .SelectMany(line => line.Split(new[] { ' ',','.','?','!',},StringSplitOptions.RemoveEmptyEntries)) .AsParallel() .Distinct()) .GroupBy(word => word) .ToDictionary(g => g.Key,g => g.Count()); } catch (Exception ex) { Ex(ex); } }
此外,在这种情况下,添加.ToLower()的正确位置在哪里调用以小写形式从文件中生成所有单词?我在(temp = file ..)之前考虑过这样的事情:
file.ToList().ConvertAll(d => d.ToLower());
解决方法
你想过滤停用词吗?
HashSet<String> StopWords = new HashSet<String> { "a","an","the" }; ... tempDict = file .SelectMany(i => File.ReadAllLines(i) .SelectMany(line => line.Split(new[] { ' ',StringSplitOptions.RemoveEmptyEntries)) .AsParallel() .Select(word => word.ToLower()) // <- To Lower case .Where(word => !StopWords.Contains(word)) // <- No stop words .Distinct() .GroupBy(word => word) .ToDictionary(g => g.Key,g => g.Count());
然而,这段代码是一个部分解决方案:像柏林这样的专有名称将被转换成小写:berlin以及首字母缩略词:KISS(Keep It Simple,Stupid)将变成一个吻,一些数字将是不正确的.