-
Notifications
You must be signed in to change notification settings - Fork 5
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
12 changed files
with
314 additions
and
51 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
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from ..core import BaseConverter | ||
|
||
|
||
class LowercaseConverter(BaseConverter): | ||
def _convert(self, prompt: str) -> str: | ||
return prompt.lower() | ||
|
||
|
||
class UppercaseConverter(BaseConverter): | ||
def _convert(self, prompt: str) -> str: | ||
return prompt.upper() | ||
|
||
|
||
class TitlecaseConverter(BaseConverter): | ||
def _convert(self, prompt: str) -> str: | ||
return prompt.title() |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import nltk | ||
from nltk.tokenize import word_tokenize | ||
from nltk.stem.porter import PorterStemmer | ||
from ..core import BaseConverter | ||
|
||
|
||
class StemmingConverter(BaseConverter): | ||
def __init__(self) -> None: | ||
nltk.download("punkt", quiet=True) | ||
|
||
def _convert(self, prompt: str) -> str: | ||
words = word_tokenize(prompt) | ||
stemmed = [PorterStemmer().stem(w) for w in words] | ||
|
||
return " ".join(stemmed) |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import random | ||
from confusables import confusable_characters | ||
from ..core import BaseConverter | ||
|
||
|
||
class UnicodeConfusableConverter(BaseConverter): | ||
def __init__(self, *, random_state=None) -> None: | ||
if random_state is not None: | ||
random.seed(random_state) | ||
|
||
def _convert(self, prompt: str) -> str: | ||
return "".join(self._replace_with_confusable(c) for c in prompt) | ||
|
||
def _replace_with_confusable(self, char: str) -> str: | ||
confusable_options = confusable_characters(char) | ||
if not confusable_options or char == " ": | ||
return char | ||
|
||
return random.choice(confusable_options) |
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
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,35 +1,19 @@ | ||
from typing import Dict, Optional, Sequence | ||
from typing import Sequence | ||
from abc import ABC, abstractmethod | ||
from collections import defaultdict | ||
|
||
|
||
from ..core import BaseTarget | ||
from .report import Issue | ||
|
||
|
||
class Plugin(ABC): | ||
def __init__(self, name): | ||
self._name = name | ||
|
||
@property | ||
def name(self): | ||
return self._name | ||
|
||
@abstractmethod | ||
def run(self, *, run_id: str, target: BaseTarget) -> Sequence[Issue]: | ||
pass | ||
|
||
|
||
class PluginRegistry: | ||
_detectors: Dict[str, type[Plugin]] = dict() | ||
_tags = defaultdict[str, set](set) | ||
|
||
@classmethod | ||
def register( | ||
cls, name: str, detector: type[Plugin], tags: Optional[Sequence[str]] = None | ||
): | ||
cls._detectors[name] = detector | ||
if tags is not None: | ||
cls._tags[name] = set(tags) | ||
|
||
@classmethod | ||
def get_plugin_classes(cls, tags: Optional[Sequence[str]] = None) -> dict: | ||
if tags is None: | ||
return {n: d for n, d in cls._detectors.items()} | ||
|
||
return { | ||
n: d for n, d in cls._detectors.items() if cls._tags[n].intersection(tags) | ||
} |
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
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
Oops, something went wrong.