Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PocketGoogle
- {
- public class Indexer : IIndexer
- {
- private readonly Dictionary<string, HashSet<int>> wordWithIdDocs =
- new Dictionary<string, HashSet<int>>();
- private readonly Dictionary<int, Dictionary<string, HashSet<int>>> idDocWithWordsAndHisPos =
- new Dictionary<int, Dictionary<string, HashSet<int>>>();
- public void Add(int id, string documentText)
- {
- var words = documentText.Split(' ', '.', ',', '!', '?', ':', '-', '\r', '\n');
- idDocWithWordsAndHisPos.Add(id, new Dictionary<string, HashSet<int>>());
- var currentPosition = 0;
- var wordsLegth = words.Length;
- for (var i = 0; i < wordsLegth; i++)
- {
- if (!wordWithIdDocs.ContainsKey(words[i]))
- {
- wordWithIdDocs.Add(words[i], new HashSet<int>());
- }
- wordWithIdDocs[words[i]].Add(id);
- if (!idDocWithWordsAndHisPos[id].ContainsKey(words[i]))
- {
- idDocWithWordsAndHisPos[id].Add(words[i], new HashSet<int>());
- }
- idDocWithWordsAndHisPos[id][words[i]].Add(currentPosition);
- currentPosition += words[i].Length + 1;
- }
- }
- public List<int> GetIds(string word)
- {
- if (wordWithIdDocs.ContainsKey(word))
- {
- return wordWithIdDocs[word].ToList<int>();
- }
- return new List<int>();
- }
- public List<int> GetPositions(int id, string word)
- {
- if (idDocWithWordsAndHisPos.ContainsKey(id) &&
- idDocWithWordsAndHisPos[id].ContainsKey(word))
- {
- return idDocWithWordsAndHisPos[id][word].ToList<int>();
- }
- return new List<int>();
- }
- public void Remove(int id)
- {
- foreach (var word in wordWithIdDocs.Keys)
- {
- wordWithIdDocs[word].Remove(id);
- }
- idDocWithWordsAndHisPos.Remove(id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement