【发布时间】:2011-05-06 01:31:50
【问题描述】:
我是第一次尝试使用 LINQ,并决定尝试基本的人类语言识别。输入文本将针对该语言中最常见的 10,000 个单词中的HashSets 进行测试并获得分数。
我的问题是,有没有更好的 LINQ 查询方法?也许我不知道的另一种形式?它有效,但我相信这里的专家将能够提供更清洁的解决方案!
public PolyAnalyzer() {
Dictionaries = new Dictionary<string, AbstractDictionary>();
Dictionaries.Add("Bulgarian", new BulgarianDictionary());
Dictionaries.Add("English", new EnglishDictionary());
Dictionaries.Add("German", new GermanDictionary());
Dictionaries.Values.Select(n => new Thread(() => n.LoadDictionaryAsync())).ToList().ForEach(n => n.Start());
}
public string getResults(string text) {
int total = 0;
return string.Join(" ",
Dictionaries.Select(n => new {
Language = n.Key,
Score = new Regex(@"\W+").Split(text).AsQueryable().Select(m => n.Value.getScore(m)).Sum()
}).
Select(n => { total += n.Score; return n; }).
ToList().AsQueryable(). // Force immediate evaluation
Select(n =>
"[" + n.Score * 100 / total + "% " + n.Language + "]").
ToArray());
}
附:我知道这是一种非常简单的语言识别方法,我只是对 LINQ 方面感兴趣。
【问题讨论】:
-
属于 codereview.SE,没有 SO。顺便说一句,字符级 n-gram 的语言检测往往更可靠。
标签: c# linq lambda functional-programming nlp