Skip to content

Commit e5bd9ce

Browse files
committed
Finishing AlphaEngine.cs
1 parent 8a66c74 commit e5bd9ce

File tree

9 files changed

+134
-63
lines changed

9 files changed

+134
-63
lines changed

AlphaInvertedSearcher/AlphaInvertedSearcher.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
9+
</ItemGroup>
10+
711
</Project>

AlphaInvertedSearcher/Engine/AlphaEngine.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using AlphaInvertedSearcher.Engine.Query;
45
using AlphaInvertedSearcher.InvertedMap;
56

67
namespace AlphaInvertedSearcher.Engine
78
{
8-
public class AlphaEngine
9+
public class AlphaEngine : IClone<AlphaEngine>
910
{
1011
private MapExtractor _mapExtractor;
11-
private ResultSet _resultSet;
1212
private SearchQuery _query;
1313

1414
public AlphaEngine(Map map)
1515
{
1616
_mapExtractor = new MapExtractor(map);
17-
_resultSet = new ResultSet(this._mapExtractor);
1817
_query = new SearchQuery();
1918
}
2019

@@ -23,44 +22,59 @@ public virtual string GetDocByID(string docID)
2322
return _mapExtractor.GetDocById(docID);
2423
}
2524

26-
public AlphaEngine AddNorms(params string[] norms)
25+
public AlphaEngine AddMustIncludes(params string[] norms)
2726
{
28-
AlphaEngine alphaEngine = Cloner.DeepClone(this);
29-
alphaEngine._query.norms.UnionWith(norms);
27+
AlphaEngine alphaEngine = Clone();
28+
alphaEngine._query.norms.UnionWith(ArrayToLowerHashSet(norms));
3029
return alphaEngine;
3130
}
3231

33-
public AlphaEngine AddPoss(params string[] poss)
32+
public AlphaEngine AddLeastIncludes(params string[] poss)
3433
{
35-
AlphaEngine alphaEngine = Cloner.DeepClone(this);
36-
alphaEngine._query.poss.UnionWith(poss);
34+
AlphaEngine alphaEngine = Clone();
35+
alphaEngine._query.poss.UnionWith(ArrayToLowerHashSet(poss));
3736
return alphaEngine;
3837
}
3938

40-
public AlphaEngine AddNegs(params string[] negs)
39+
public AlphaEngine AddExcludes(params string[] negs)
4140
{
42-
AlphaEngine alphaEngine = Cloner.DeepClone(this);
43-
alphaEngine._query.negs.UnionWith(negs);
41+
AlphaEngine alphaEngine = Clone();
42+
alphaEngine._query.negs.UnionWith(ArrayToLowerHashSet(negs));
4443
return alphaEngine;
4544
}
4645

4746
public AlphaEngine ReconstructMap(Map map)
4847
{
49-
AlphaEngine alphaEngine = Cloner.DeepClone(this);
48+
AlphaEngine alphaEngine = Clone();
5049
alphaEngine._mapExtractor = new MapExtractor(map);
5150
return alphaEngine;
5251
}
5352

5453
public List<string> ExecuteQuery()
5554
{
56-
var result = _resultSet.ExecuteQuery(_query).ToList();
57-
_resultSet = new ResultSet(_mapExtractor);
55+
var result = new ResultSet(_mapExtractor, _query).ExecuteQuery().ToList();
5856
_query = new SearchQuery();
5957
return result;
6058
}
61-
62-
6359

60+
private HashSet<string> ArrayToLowerHashSet(string[] strings)
61+
{
62+
var stringsHashSet = new HashSet<string>();
63+
foreach (var str in strings)
64+
{
65+
stringsHashSet.Add(str.ToLower());
66+
}
67+
68+
return stringsHashSet;
69+
}
70+
71+
public AlphaEngine Clone()
72+
{
73+
AlphaEngine alphaEngine = new AlphaEngine(null);
74+
alphaEngine._mapExtractor = _mapExtractor.Clone();
75+
alphaEngine._query = _query.Clone();
76+
return alphaEngine;
77+
}
6478
}
6579

6680

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AlphaInvertedSearcher.Engine
2+
{
3+
public interface IClone<T>
4+
{
5+
T Clone();
6+
}
7+
}

AlphaInvertedSearcher/Engine/Query/Utils/ResultSet.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ namespace AlphaInvertedSearcher.Engine.Query
44
{
55
public class ResultSet
66
{
7-
public HashSet<string> Result { get; set; }
8-
public SearchQuery Query { get; set; }
7+
public HashSet<string> Result { get; } = new HashSet<string>();
8+
public SearchQuery Query { get; }
99
public MapExtractor MapExtractor { get; }
1010

1111
private ISearch _searcher = new NegsSearcher();
1212

13-
public ResultSet(MapExtractor mapExtractor)
13+
public ResultSet(MapExtractor mapExtractor, SearchQuery query)
1414
{
1515
MapExtractor = mapExtractor;
16-
16+
Query = query;
1717
}
1818

19-
public HashSet<string> ExecuteQuery(SearchQuery query)
19+
public HashSet<string> ExecuteQuery()
2020
{
21-
Query = query;
22-
Result = new HashSet<string>();
2321
_searcher.Search(this, MapExtractor);
2422
return Result;
2523
}

AlphaInvertedSearcher/Engine/Query/Utils/SearchQuery.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
namespace AlphaInvertedSearcher.Engine.Query
44
{
5-
public class SearchQuery
5+
public class SearchQuery : IClone<SearchQuery>
66
{
7-
public HashSet<string> norms { get; set; } = new HashSet<string>();
8-
public HashSet<string> poss { get; set; } = new HashSet<string>();
9-
public HashSet<string> negs { get; set; } = new HashSet<string>();
7+
public HashSet<string> norms { get; } = new HashSet<string>();
8+
public HashSet<string> poss { get; } = new HashSet<string>();
9+
public HashSet<string> negs { get; } = new HashSet<string>();
1010

1111
public SearchQuery()
1212
{
1313
}
14-
14+
15+
public SearchQuery Clone()
16+
{
17+
SearchQuery query = new SearchQuery();
18+
query.norms.UnionWith( norms);
19+
query.poss.UnionWith(poss);
20+
query.negs.UnionWith(negs);
21+
return query;
22+
}
1523
}
1624
}

AlphaInvertedSearcher/Engine/Utils/Cloner.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

AlphaInvertedSearcher/Engine/Utils/MapExtractor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace AlphaInvertedSearcher.Engine
77
{
8-
public class MapExtractor
8+
public class MapExtractor : IClone<MapExtractor>
99
{
1010
private Map _map;
1111

@@ -30,6 +30,11 @@ public List<string> GetDocsByKeyword(string keyword)
3030
return _map.InvertedMap[keyword].ToList();
3131
return new List<string>();
3232
}
33+
34+
public MapExtractor Clone()
35+
{
36+
return new MapExtractor(_map);
37+
}
3338
}
3439

3540
public class DocumentNotFoundException : Exception
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using AlphaInvertedSearcher.Engine;
5+
using AlphaInvertedSearcher.Engine.Query;
6+
using AlphaInvertedSearcher.InvertedMap;
7+
using AlphaInvertedSearcherTest.InvertedMapTest;
8+
using Xunit;
9+
10+
namespace AlphaInvertedSearcherTest.EngineTest
11+
{
12+
public class AlphaEngineTest
13+
{
14+
private Map _map = MapTest.InitMapper();
15+
private AlphaEngine _alphaEngine;
16+
17+
[Fact]
18+
public void AddKeyword()
19+
{
20+
_alphaEngine = new AlphaEngine(_map);
21+
AlphaEngine alphaEngine = _alphaEngine
22+
.AddMustIncludes("hello", "hi", "hMM")
23+
.AddExcludes("he", "She", "mEn", "WOmen")
24+
.AddMustIncludes("AhA")
25+
.AddLeastIncludes("Ok", "There")
26+
.AddExcludes("How", "wheRe", "some");
27+
SearchQuery query = (SearchQuery)GetInstanceField(typeof(AlphaEngine), alphaEngine, "_query");
28+
Assert.Equal(new HashSet<string>(){"hello", "hi", "hmm", "aha"}, query.norms);
29+
Assert.Equal(new HashSet<string>(){"ok", "there"}, query.poss);
30+
Assert.Equal(new HashSet<string>(){"he", "she", "men", "women", "how", "where", "some"}, query.negs);
31+
}
32+
33+
static object GetInstanceField(Type type, object instance, string fieldName)
34+
{
35+
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
36+
| BindingFlags.Static;
37+
FieldInfo field = type.GetField(fieldName, bindFlags);
38+
return field.GetValue(instance);
39+
}
40+
41+
[Fact]
42+
public void ExecuteQueryTest()
43+
{
44+
_alphaEngine = new AlphaEngine(_map);
45+
AlphaEngine engine = _alphaEngine.AddMustIncludes("The", "WitcheR", "A").AddLeastIncludes("HunTeR", "Bank");
46+
AlphaEngine copyEngine = engine.Clone();
47+
Assert.Equal(new List<string>(){"1.txt", "2.txt", "3.txt"}, engine.ExecuteQuery());
48+
Assert.Equal(new List<string>(){"2.txt"}, copyEngine.AddExcludes("role-playing").ExecuteQuery());
49+
}
50+
51+
}
52+
}

AlphaInvertedSearcherTest/InvertedMapTest/MapHolderTest.cs renamed to AlphaInvertedSearcherTest/InvertedMapTest/MapTest.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace AlphaInvertedSearcherTest.InvertedMapTest
88
{
9-
public class MapHolderTest
9+
public class MapTest
1010
{
1111
private static readonly string DocsDirectory = "InvertedMapTest\\TestDocs\\";
1212
private static List<string> GetFileNames()
@@ -35,12 +35,23 @@ private static string GetTestDir()
3535
return path + "\\";
3636
}
3737

38+
public static Map InitMapper()
39+
{
40+
Map map = new Map();
41+
foreach (string fileName in GetFileNames())
42+
{
43+
map.AddDoc(fileName, GetFileContextByName(fileName));
44+
}
45+
46+
return map;
47+
}
48+
3849
private Map _map = new Map();
3950

4051
[Fact]
4152
public void AddDocToMapTest()
4253
{
43-
InitMapper();
54+
_map = InitMapper();
4455
Assert.Equal(GetAddDocToMapTestSupply(), _map.InvertedMap);
4556
}
4657

@@ -63,7 +74,7 @@ private Dictionary<string, HashSet<string>> GetAddDocToMapTestSupply()
6374
[Fact]
6475
public void RemoveDocsFromMapTest()
6576
{
66-
InitMapper();
77+
_map = InitMapper();
6778
dynamic supply = new
6879
{
6980
Item1 = _map.Docs, Item2 = _map.InvertedMap
@@ -96,14 +107,6 @@ private Tuple<Dictionary<string, string>, Dictionary<string, HashSet<string>>>R
96107
}
97108
);
98109
}
99-
100-
private void InitMapper()
101-
{
102-
foreach (string fileName in GetFileNames())
103-
{
104-
_map.AddDoc(fileName, GetFileContextByName(fileName));
105-
}
106-
}
107-
110+
108111
}
109112
}

0 commit comments

Comments
 (0)