Skip to content

Commit

Permalink
compoe I/F を導入してみた
Browse files Browse the repository at this point in the history
  • Loading branch information
p-baleine committed Feb 20, 2021
1 parent 1d98e2d commit d8b3ce3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 23 deletions.
13 changes: 9 additions & 4 deletions naivesearch/indexer/chunker.py
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)
19 changes: 13 additions & 6 deletions naivesearch/indexer/formatter.py
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()
39 changes: 28 additions & 11 deletions naivesearch/main.py
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()
19 changes: 17 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d8b3ce3

Please sign in to comment.