Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/phase06 #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using FullTextSearch.Core;

namespace FullTextSearch.Controller.DocumentController.Abstraction;

public interface IDocumentBuilder
{

void BuildName(string name);
void BuildPath(string path);
void BuildText(string text);
void DeleteExtraSpace();
void BuildWords();
Document GetDocument();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FullTextSearch.Controller.DocumentController.Abstraction;

public interface IDocumentDirector
{
void Construct(string name, string path, string text, IDocumentBuilder documentBuilder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace InvertedIndex.Controller.Document;

public interface IDocumentFormatter
{
string ToUpper(string text);
IEnumerable<string> Split(string queryText, string regex);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Text.RegularExpressions;
using FullTextSearch.Controller.DocumentController.Abstraction;
using FullTextSearch.Core;
using InvertedIndex.Controller.Document;

namespace FullTextSearch.Controller.DocumentController;

public class DocumentBuilder : IDocumentBuilder
{
private readonly Document _document;
private readonly IDocumentFormatter _documentFormatter;

public DocumentBuilder(IDocumentFormatter documentFormatter)
{
_documentFormatter = documentFormatter ?? throw new ArgumentNullException(nameof(documentFormatter));
_document = new Document();
}

public void BuildName(string name)
{
_document.Name = name;
}

public void BuildPath(string path)
{
_document.Path = path;
}

public void BuildText(string text)
{
_document.Text = text;
}

public void DeleteExtraSpace()
{
_document.Text = Regex.Replace( _document.Text, @"\s+", " ");
}
public void BuildWords()
{
_document.Words = _documentFormatter.Split(_documentFormatter.ToUpper(_document.Text), " ");
}

public Document GetDocument()
{
return _document;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using FullTextSearch.Controller.DocumentController.Abstraction;

namespace FullTextSearch.Controller.DocumentController;

public class DocumentDirector : IDocumentDirector
{
public void Construct(string name, string path, string text, IDocumentBuilder documentBuilder)
{
documentBuilder.BuildName(name);
documentBuilder.BuildPath(path);
documentBuilder.BuildText(text);
documentBuilder.DeleteExtraSpace();
documentBuilder.BuildWords();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.RegularExpressions;
using InvertedIndex.Controller.Document;

namespace FullTextSearch.Controller.DocumentController;

public class DocumentFormatter : IDocumentFormatter{

public string ToUpper(string text)
{
return text.ToUpper();
}

public IEnumerable<string> Split(string queryText, string regex)
{
return Regex.Split(queryText, regex);
}

}
22 changes: 22 additions & 0 deletions phase05/FullTextSearch.Controller/FullTextSearch.Controller.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FullTextSearch.Model\FullTextSearch.Model.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NSubstitute.Analyzers.VisualBasic" Version="1.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections;
using FullTextSearch.Core;

namespace FullTextSearch.Controller.InvertedIndexController;

public interface IInvertedIndexMapper {
Dictionary<string, IEnumerable<Document>> Map(IEnumerable<Document> documents);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FullTextSearch.Core;

namespace FullTextSearch.Controller.InvertedIndexController;

public class InvertedIndexMapper : IInvertedIndexMapper
{
public Dictionary<string, IEnumerable<Document>> Map(IEnumerable<Document> documents)
{
var invertedIndex = new Dictionary<string, List<Document>>();

foreach (var document in documents)
{
var words = document.Words.ToList();

for (int startIndex = 0; startIndex < words.Count; startIndex++)
{
for (int endIndex = startIndex + 1; endIndex <= Math.Min(words.Count, startIndex + 5); endIndex++)
{
var phrase = string.Join(" ", words.Skip(startIndex).Take(endIndex - startIndex));
if (!invertedIndex.ContainsKey(phrase))
invertedIndex[phrase] = new List<Document>();
invertedIndex[phrase].Add(document);
}
}
}
return invertedIndex.ToDictionary(pair => pair.Key, pair => (IEnumerable<Document>)pair.Value);
}
}
3 changes: 3 additions & 0 deletions phase05/FullTextSearch.Controller/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// See https://aka.ms/new-console-template for more information

Console.WriteLine("Hello, World!");
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using FullTextSearch.Core;

namespace FullTextSearch.Controller.QueryController.Abstraction;

public interface IQueryBuilder
{
void BuildText(string text);
void BuildWordsBySign(IEnumerable<char> signs);
Query GetQuery();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FullTextSearch.Controller.QueryController.Abstraction;

public interface IQueryDirector
{
void Construct(IQueryBuilder queryBuilder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FullTextSearch.Controller.QueryController.Abstraction;

public interface IQueryFormatter
{
string ToUpper(string text);
IEnumerable<string> Split(string queryText, string regex);
IEnumerable<string> CollectBySign(IEnumerable<string> queryWords, char sign);
IEnumerable<string> RemovePrefix(IEnumerable<string> querySameSignWords);
}
66 changes: 66 additions & 0 deletions phase05/FullTextSearch.Controller/QueryController/QueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using FullTextSearch.Controller.QueryController.Abstraction;
using FullTextSearch.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using FullTextSearch.Controller.TextFormatter.Abstraction;
using InvertedIndex.Controller.Document;

namespace FullTextSearch.Controller.QueryController;

public class QueryBuilder : IQueryBuilder
{
private readonly Query _query;
private readonly IQueryFormatter _queryFormatter;
private readonly ITextFormatter _textFormatter;
public QueryBuilder(IQueryFormatter queryFormatter, ITextFormatter textFormatter)
{
_query = new Query();
_queryFormatter = queryFormatter ?? throw new ArgumentNullException(nameof(queryFormatter));
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
}

public void BuildText(string text)
{
_query.Text = text;
}

public void BuildWordsBySign(IEnumerable<char> signs)
{
_query.WordsBySign.Clear();

var splittedText = _queryFormatter.Split(_queryFormatter.ToUpper(_query.Text), " ").ToList();
var quoteIndices = _textFormatter.GetQuoteIndices(splittedText);
var indicesToRemove = _textFormatter.GetIndicesToRemove(quoteIndices);
var filteredWords = _textFormatter.FilterOutIndices(splittedText, indicesToRemove);
var concatenatedQuotes = _textFormatter.ConcatenateQuotedWords(splittedText, quoteIndices);

ProcessWordsBySign(filteredWords, signs);
ProcessWordsBySign(concatenatedQuotes, signs);
}
private void ProcessWordsBySign(IEnumerable<string> words, IEnumerable<char> signs)
{

foreach (var sign in signs)
{
if (!_query.WordsBySign.ContainsKey(sign))
{
_query.WordsBySign[sign] = new List<string>();
}
var queryWords = words.ToList();
var texts = _queryFormatter.CollectBySign(queryWords, sign);
words = queryWords.Except(texts).ToList();
_query.WordsBySign[sign] = _query.WordsBySign[sign].Concat(_queryFormatter.RemovePrefix(texts)).ToList();
}
if (!_query.WordsBySign.ContainsKey(' '))
{
_query.WordsBySign[' '] = new List<string>();
}
_query.WordsBySign[' '] = _query.WordsBySign[' '].Concat(_queryFormatter.RemovePrefix(words)).ToList();
}

public Query GetQuery()
{
return _query;
}
}
12 changes: 12 additions & 0 deletions phase05/FullTextSearch.Controller/QueryController/QueryDirector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FullTextSearch.Controller.QueryController.Abstraction;

namespace FullTextSearch.Controller.QueryController;

public class QueryDirector
{
public void Construct(string text, IEnumerable<char> signs, IQueryBuilder queryBuilder)
{
queryBuilder.BuildText(text);
queryBuilder.BuildWordsBySign(signs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Text.RegularExpressions;
using FullTextSearch.Controller.QueryController.Abstraction;

namespace FullTextSearch.Controller.QueryController;

public class QueryFormatter : IQueryFormatter
{
public string ToUpper(string text)
{
return text.ToUpper();
}

public IEnumerable<string> Split(string queryText, string regex)
{
return Regex.Split(queryText, regex);
}

public IEnumerable<string> CollectBySign(IEnumerable<string> queryWords, char sign)
{
return queryWords.Where(w => w[0].Equals(sign));
}

public IEnumerable<string> RemovePrefix(IEnumerable<string> querySameSignWords)
{
return querySameSignWords.Select(word =>
{
if (word.StartsWith("+") || word.StartsWith("-"))
{
word = word.Substring(1);
}
if (word.StartsWith("\"") && word.EndsWith("\""))
{
word = word.Substring(1, word.Length - 2);
}
return word;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FullTextSearch.Controller.Read.Abstraction;

public interface IDirectory
{
List<string> GetFiles(string dirPath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace InvertedIndex.Abstraction.Read;

public interface IFileReader
{
Task<string> ReadAsync(string path);
}
6 changes: 6 additions & 0 deletions phase05/FullTextSearch.Controller/Read/Abstraction/IPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FullTextSearch.Controller.Read.Abstraction;

public interface IPath
{
string GetFileName(string path);
}
11 changes: 11 additions & 0 deletions phase05/FullTextSearch.Controller/Read/Directory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FullTextSearch.Controller.Read.Abstraction;

namespace FullTextSearch.Controller.Read;

public class Directory : IDirectory
{
public List<string> GetFiles(string dirPath)
{
return System.IO.Directory.GetFiles(dirPath).ToList();
}
}
11 changes: 11 additions & 0 deletions phase05/FullTextSearch.Controller/Read/Path.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FullTextSearch.Controller.Read.Abstraction;

namespace FullTextSearch.Controller.Read;

public class Path : IPath
{
public string GetFileName(string path)
{
return System.IO.Path.GetFileName(path);
}
}
Loading