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
- {
- public Dictionary<int, List<string>> dictionary2 = new Dictionary<int, List<string>>();
- public Dictionary<string, Dictionary<int, List<int>>> wordsWithIndex = new Dictionary<string, Dictionary<int, List<int>>>();
- public void Add(int id, string documentText)
- {
- var splitedText = documentText.Split(new char[] { ' ', '.', ',', '!', '?', ':', '-', '\r', '\n' });
- var index = 0;
- foreach (var word in splitedText)
- {
- if (wordsWithIndex.ContainsKey(word))
- {
- if (wordsWithIndex[word].ContainsKey(id))
- {
- wordsWithIndex[word][id].Add(index);
- }
- else
- {
- var listWithIndex = new List<int>() { index };
- wordsWithIndex[word].Add(id, listWithIndex);
- }
- }
- else
- {
- var listWithIndex = new List<int>() { index };
- wordsWithIndex.Add(word, new Dictionary<int, List<int>>() { { id, listWithIndex } });
- }
- index += word.Length + 1;
- }
- dictionary2.Add(id, splitedText.ToList());
- }
- public List<int> GetIds(string word)
- {
- var list = new List<int>();
- if (wordsWithIndex.ContainsKey(word))
- {
- return wordsWithIndex[word].Keys.ToList();
- }
- return new List<int>();
- }
- public List<int> GetPositions(int id, string word)
- {
- if (wordsWithIndex.ContainsKey(word))
- if (wordsWithIndex[word].ContainsKey(id))
- return wordsWithIndex[word][id];
- return new List<int>();
- }
- public void Remove(int id)
- {
- foreach (var e in dictionary2[id])
- {
- if (wordsWithIndex.ContainsKey(e))
- {
- wordsWithIndex[e].Remove(id);
- }
- }
- dictionary2.Remove(id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement