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

Clustering #80

Merged
merged 3 commits into from
Jun 9, 2024
Merged
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
3 changes: 2 additions & 1 deletion genrex/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"genrexsimpleguid",
"genrexthex",
"genrexhex8",
"genrexexem",
"genrexurl",
"genrexphone",
]


Expand Down
19 changes: 10 additions & 9 deletions genrex/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from dataclasses import dataclass, field
from statistics import mean, median

from genrex.enums import InputType
from genrex.logging import logger
from genrex.misc import filter_ngrams, ready_to_print, string2ngrams

Expand Down Expand Up @@ -105,6 +104,7 @@ def __init__(self, store_original_strings: bool = False):
self.unique_ngrams: dict = defaultdict(dict)
self.index: dict = defaultdict(dict)
self.min_ngram = 4
self.max_ngram = 270
self.input_type: str = ""

def add_resource(
Expand All @@ -120,9 +120,13 @@ def add_resource(
self.input_type = input_type
for source, strings in data.items():
for string in strings:
if len(string) < self.min_ngram:
check_len = len(string)
if check_len < self.min_ngram:
logger.warning(f"String {string} is too short")
continue
elif check_len > self.max_ngram:
logger.warning(f"String {string} is too long")
continue

prep_string = string

Expand All @@ -137,14 +141,14 @@ def add_resource(

self.add(string, prep_string, source)

self.extract_ngrams(self.input_type)
self.extract_ngrams()

def filter_short_strings(self):
self.samples = dict(
filter(lambda x: (len(x[0]) >= self.ngrams), self.samples.items())
)

def extract_ngrams(self, input_type):
def extract_ngrams(self):
splited_list = {}
for prep_string in self.samples:
self.unique_ngrams[prep_string] = []
Expand All @@ -166,11 +170,8 @@ def extract_ngrams(self, input_type):
return

self.ngrams = int((median(splited) + mean(splited)) // 2)
if input_type not in [
InputType.FILE_ACCESS,
InputType.KEY_ACCESS,
]:
self.ngrams = self.ngrams // 2

self.ngrams = self.ngrams // 2

self.ngrams = max(self.ngrams, self.min_ngram)

Expand Down
14 changes: 7 additions & 7 deletions poetry.lock

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

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ disable = [
"too-many-nested-blocks",
"too-many-branches",
"too-many-locals",
"too-many-function-args"
"too-many-function-args",
"too-many-instance-attributes"
]

[build-system]
Expand Down
Loading