fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var index = new Dictionary<string, HashSet<int>>
  10. {
  11. { "hello", new HashSet<int> { 0, 5, 1 } },
  12. { "world", new HashSet<int> { 1, 23 } },
  13. { "onenote", new HashSet<int> { 47, 23, 1 } }
  14. };
  15.  
  16. var query = "hello world xyz".Split();
  17. var scores = query.Where(term => index.ContainsKey(term))
  18. .SelectMany(term => index[term])
  19. .GroupBy(x => x)
  20. .ToDictionary(group => group.Key, group => group.Count());
  21.  
  22. foreach (var kvp in scores.OrderByDescending(x => x.Value).Take(3))
  23. {
  24. Console.WriteLine("Document {0} scored {1}", kvp.Key, kvp.Value);
  25. }
  26. }
  27. }
Success #stdin #stdout 0.07s 24752KB
stdin
Standard input is empty
stdout
Document 1 scored 2
Document 0 scored 1
Document 5 scored 1