Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
78 lines (70 loc) · 2.35 KB

File metadata and controls

78 lines (70 loc) · 2.35 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
namespace DataStructures;
/// <summary>
/// Inverted index is the simplest form of document indexing,
/// allowing performing boolean queries on text data.
///
/// This realization is just simplified for better understanding the process of indexing
/// and working on straightforward string inputs.
/// </summary>
public class InvertedIndex
{
private readonly Dictionary<string, List<string>> invertedIndex = [];
/// <summary>
/// Build inverted index with source name and source content.
/// </summary>
/// <param name="sourceName">Name of the source.</param>
/// <param name="sourceContent">Content of the source.</param>
public void AddToIndex(string sourceName, string sourceContent)
{
var context = sourceContent.Split(' ').Distinct();
foreach (var word in context)
{
if (!invertedIndex.ContainsKey(word))
{
invertedIndex.Add(word, [sourceName]);
}
else
{
invertedIndex[word].Add(sourceName);
}
}
}
/// <summary>
/// Returns the source names contains ALL terms inside at same time.
/// </summary>
/// <param name="terms">List of terms.</param>
/// <returns>Source names.</returns>
public IEnumerable<string> And(IEnumerable<string> terms)
{
var entries = terms
.Select(term => invertedIndex
.Where(x => x.Key.Equals(term))
.SelectMany(x => x.Value))
.ToList();
var intersection = entries
.Skip(1)
.Aggregate(new HashSet<string>(entries.First()), (hashSet, enumerable) =>
{
hashSet.IntersectWith(enumerable);
return hashSet;
});
return intersection;
}
/// <summary>
/// Returns the source names contains AT LEAST ONE from terms inside.
/// </summary>
/// <param name="terms">List of terms.</param>
/// <returns>Source names.</returns>
public IEnumerable<string> Or(IEnumerable<string> terms)
{
var sources = new List<string>();
foreach (var term in terms)
{
var source = invertedIndex
.Where(x => x.Key.Equals(term))
.SelectMany(x => x.Value);
sources.AddRange(source);
}
return sources.Distinct();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.