fork(1) 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 = new Dictionary<int, int>();
  18. foreach (var term in query)
  19. {
  20. HashSet<int> documents;
  21. if (!index.TryGetValue(term, out documents))
  22. {
  23. continue;
  24. }
  25.  
  26. foreach (var document in documents)
  27. {
  28. int score;
  29. scores.TryGetValue(document, out score);
  30. scores[document] = score + 1;
  31. }
  32. }
  33.  
  34. foreach (var kvp in scores.OrderByDescending(x => x.Value).Take(3))
  35. {
  36. Console.WriteLine("Document {0} scored {1}", kvp.Key, kvp.Value);
  37. }
  38. }
  39. }
Success #stdin #stdout 0.06s 24608KB
stdin
Standard input is empty
stdout
Document 1 scored 2
Document 0 scored 1
Document 5 scored 1