Add initial inverted index implementation. It's untested though.
This commit is contained in:
parent
c46a789c28
commit
8cd7156023
2 changed files with 25 additions and 2 deletions
|
@ -31,6 +31,7 @@ namespace SearchBox
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Index(string inSource, IndexOptions options)
|
public Index(string inSource, IndexOptions options)
|
||||||
: this(inSource, EmbeddedFiles.EnumerateLines("SearchBox.EmbeddedFiles.Stopwords.txt"), options)
|
: this(inSource, EmbeddedFiles.EnumerateLines("SearchBox.EmbeddedFiles.Stopwords.txt"), options)
|
||||||
{
|
{
|
||||||
|
@ -55,6 +56,10 @@ namespace SearchBox
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public IEnumerable<string> Tokens()
|
||||||
|
{
|
||||||
|
return index.Keys;
|
||||||
|
}
|
||||||
public IEnumerable<KeyValuePair<string, List<int>>> IterateItems()
|
public IEnumerable<KeyValuePair<string, List<int>>> IterateItems()
|
||||||
{
|
{
|
||||||
foreach(KeyValuePair<string, List<int>> item in index)
|
foreach(KeyValuePair<string, List<int>> item in index)
|
||||||
|
|
|
@ -1,23 +1,41 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SearchBox
|
namespace SearchBox
|
||||||
{
|
{
|
||||||
public class InvertedIndex
|
public class InvertedIndex
|
||||||
{
|
{
|
||||||
private Dictionary<string, Dictionary<int, List<int>>> invertedIndex = new Dictionary<string, Dictionary<int, List<int>>>();
|
private ConcurrentDictionary<string, ConcurrentDictionary<int, List<int>>> invertedIndex = new Dictionary<string, Dictionary<int, List<int>>>();
|
||||||
|
|
||||||
public InvertedIndex()
|
public InvertedIndex()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool AddIndex(Index newIndex)
|
public bool AddIndex(int pageId, Index newIndex)
|
||||||
{
|
{
|
||||||
foreach (KeyValuePair<string, List<int>> token in newIndex)
|
foreach (KeyValuePair<string, List<int>> token in newIndex)
|
||||||
{
|
{
|
||||||
|
if (!invertedIndex.ContainsKey(token.Key) &&
|
||||||
|
!invertedIndex.TryAdd(token.Key, new ConcurrentDictionary<int, List<int>>()))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!invertedIndex[token.Key].TryAdd(pageId, token.Value))
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool RemoveIndex(int pageId, Index newIndex)
|
||||||
|
{
|
||||||
|
foreach (string token in newIndex.Tokens())
|
||||||
|
{
|
||||||
|
if (!invertedIndex.ContainsKey(token) || !invertedIndex[token].ContainsKey(pageId)) continue;
|
||||||
|
|
||||||
|
if (!invertedIndex[token].TryRemove(pageId, out List<int> noop))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue