Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace PocketGoogle
  8. {
  9. public class Indexer : IIndexer
  10. {
  11. private readonly Dictionary<string, HashSet<int>> wordWithIdDocs =
  12. new Dictionary<string, HashSet<int>>();
  13. private readonly Dictionary<int, Dictionary<string, HashSet<int>>> idDocWithWordsAndHisPos =
  14. new Dictionary<int, Dictionary<string, HashSet<int>>>();
  15.  
  16. public void Add(int id, string documentText)
  17. {
  18. var words = documentText.Split(' ', '.', ',', '!', '?', ':', '-', '\r', '\n');
  19. idDocWithWordsAndHisPos.Add(id, new Dictionary<string, HashSet<int>>());
  20.  
  21. var currentPosition = 0;
  22. var wordsLegth = words.Length;
  23. for (var i = 0; i < wordsLegth; i++)
  24. {
  25. if (!wordWithIdDocs.ContainsKey(words[i]))
  26. {
  27. wordWithIdDocs.Add(words[i], new HashSet<int>());
  28. }
  29. wordWithIdDocs[words[i]].Add(id);
  30.  
  31. if (!idDocWithWordsAndHisPos[id].ContainsKey(words[i]))
  32. {
  33. idDocWithWordsAndHisPos[id].Add(words[i], new HashSet<int>());
  34. }
  35. idDocWithWordsAndHisPos[id][words[i]].Add(currentPosition);
  36. currentPosition += words[i].Length + 1;
  37. }
  38. }
  39.  
  40. public List<int> GetIds(string word)
  41. {
  42. if (wordWithIdDocs.ContainsKey(word))
  43. {
  44. return wordWithIdDocs[word].ToList<int>();
  45. }
  46. return new List<int>();
  47. }
  48.  
  49. public List<int> GetPositions(int id, string word)
  50. {
  51. if (idDocWithWordsAndHisPos.ContainsKey(id) &&
  52. idDocWithWordsAndHisPos[id].ContainsKey(word))
  53. {
  54. return idDocWithWordsAndHisPos[id][word].ToList<int>();
  55. }
  56. return new List<int>();
  57. }
  58.  
  59. public void Remove(int id)
  60. {
  61. foreach (var word in wordWithIdDocs.Keys)
  62. {
  63. wordWithIdDocs[word].Remove(id);
  64. }
  65. idDocWithWordsAndHisPos.Remove(id);
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement