Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 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. public Dictionary<int, List<string>> dictionary2 = new Dictionary<int, List<string>>();
  12. public Dictionary<string, Dictionary<int, List<int>>> wordsWithIndex = new Dictionary<string, Dictionary<int, List<int>>>();
  13. public void Add(int id, string documentText)
  14. {
  15. var splitedText = documentText.Split(new char[] { ' ', '.', ',', '!', '?', ':', '-', '\r', '\n' });
  16. var index = 0;
  17. foreach (var word in splitedText)
  18. {
  19. if (wordsWithIndex.ContainsKey(word))
  20. {
  21. if (wordsWithIndex[word].ContainsKey(id))
  22. {
  23. wordsWithIndex[word][id].Add(index);
  24. }
  25. else
  26. {
  27. var listWithIndex = new List<int>() { index };
  28. wordsWithIndex[word].Add(id, listWithIndex);
  29. }
  30. }
  31. else
  32. {
  33. var listWithIndex = new List<int>() { index };
  34. wordsWithIndex.Add(word, new Dictionary<int, List<int>>() { { id, listWithIndex } });
  35. }
  36. index += word.Length + 1;
  37. }
  38. dictionary2.Add(id, splitedText.ToList());
  39. }
  40.  
  41. public List<int> GetIds(string word)
  42. {
  43. var list = new List<int>();
  44. if (wordsWithIndex.ContainsKey(word))
  45. {
  46. return wordsWithIndex[word].Keys.ToList();
  47. }
  48.  
  49. return new List<int>();
  50. }
  51.  
  52. public List<int> GetPositions(int id, string word)
  53. {
  54. if (wordsWithIndex.ContainsKey(word))
  55. if (wordsWithIndex[word].ContainsKey(id))
  56. return wordsWithIndex[word][id];
  57. return new List<int>();
  58. }
  59.  
  60. public void Remove(int id)
  61. {
  62. foreach (var e in dictionary2[id])
  63. {
  64. if (wordsWithIndex.ContainsKey(e))
  65. {
  66. wordsWithIndex[e].Remove(id);
  67. }
  68. }
  69. dictionary2.Remove(id);
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement