-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
from typing import Callable, Protocol, List | ||
from typing import Callable, Protocol, List, Optional | ||
|
||
from .formatter import Formatter | ||
|
||
|
||
Chunker = Callable[[str], List[str]] | ||
class Chunker(Protocol): | ||
def __init__(self, formatter: Optional[Formatter]): | ||
... | ||
|
||
def __call__(self, x: str) -> List[str]: | ||
... | ||
|
||
|
||
class CharacterChunker: | ||
def __init__(self, formatter: Formatter): | ||
def __init__(self, formatter: Optional[Formatter]): | ||
self.formatter = formatter | ||
|
||
def __call__(self, x: str) -> List[str]: | ||
return list(self.formatter(x)) | ||
return list(self.formatter(x) if self.formatter else x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
import unicodedata | ||
from typing import Callable, Optional | ||
from typing import Callable, Optional, Protocol | ||
|
||
Formatter = Callable[[str], str] | ||
|
||
class Formatter(Protocol): | ||
def __init__(self, other: Optional['Formatter'] = None, **kwargs): | ||
... | ||
|
||
def __call__(self, x: str) -> str: | ||
... | ||
|
||
|
||
class UnicodeNormalizer: | ||
def __init__(self, other: Optional[Formatter] = None): | ||
def __init__(self, other: Optional[Formatter] = None, form: str = 'NFKC'): | ||
self.other = other | ||
self.form = form | ||
|
||
def __call__(self, x: str): | ||
def __call__(self, x: str) -> str: | ||
x = self.other(x) if self.other else x | ||
return unicodedata.normalize('NFKC', x) | ||
return unicodedata.normalize(self.form, x) | ||
|
||
|
||
class LowerCaseNormalizer: | ||
def __init__(self, other: Optional[Formatter] = None): | ||
self.other = other | ||
|
||
def __call__(self, x: str): | ||
def __call__(self, x: str) -> str: | ||
x = self.other(x) if self.other else x | ||
return x.lower() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,39 @@ | ||
from returns.curry import partial | ||
from typing import Callable, Type | ||
|
||
from naivesearch.indexer import InvertedIndex | ||
from naivesearch.indexer.formatter import UnicodeNormalizer, LowerCaseNormalizer | ||
from naivesearch.indexer.formatter import UnicodeNormalizer, LowerCaseNormalizer, Formatter | ||
from naivesearch.indexer.converter import BigramConverter | ||
from naivesearch.indexer.chunker import CharacterChunker | ||
from naivesearch.indexer.chunker import CharacterChunker, Chunker | ||
|
||
|
||
def naivesearch(filepath: str): | ||
|
||
def file_reader(filepath): | ||
with open(filepath) as f: | ||
for line in f.readlines(): | ||
yield line.strip() | ||
|
||
index = InvertedIndex( | ||
file_reader(filepath), | ||
[ | ||
BigramConverter(CharacterChunker( | ||
UnicodeNormalizer(LowerCaseNormalizer()), | ||
)) | ||
composed( | ||
BigramConverter, | ||
CharacterChunker, | ||
LowerCaseNormalizer, | ||
partial(UnicodeNormalizer, form='NFKC'), | ||
) | ||
] | ||
) | ||
return index | ||
|
||
|
||
def composed( | ||
converter: Type[BigramConverter], | ||
chunker: Type[Chunker], | ||
*formatters: Callable[..., Formatter], | ||
): | ||
result = None | ||
for x in reversed(formatters): | ||
result = x(result) | ||
return converter(chunker(result)) | ||
|
||
|
||
def file_reader(filepath): | ||
with open(filepath) as f: | ||
for line in f.readlines(): | ||
yield line.strip() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ authors = ["Your Name <[email protected]>"] | |
|
||
[tool.poetry.dependencies] | ||
python = "3.8.6" | ||
returns = "^0.15.0" | ||
|
||
[tool.poetry.dev-dependencies] | ||
mypy = "^0.790" | ||
|