Skip to content

Commit

Permalink
Merge pull request #29 from fabaindaiz/main
Browse files Browse the repository at this point in the history
Dev: merged @yilmazcabuk pull request
  • Loading branch information
fabaindaiz authored Aug 15, 2023
2 parents afc6cd4 + f4e3005 commit 93c0118
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 29 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ WordValidate successfully initialized (elapsed time: 25.05 seconds)
- Add some spellsolver tests to avoid accidentally introducing new bugs
- Add some heuristics to reduce the load and query time of double swap mode

### Notices for contributors
- Thank you for your interest in contributing to spellsolver, any improvement will be welcome
- Please keep using typing types to maintain compatibility with python 3.6


## instructions for use

Expand Down
89 changes: 62 additions & 27 deletions src/modules/wordlist.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,84 @@
import os
from io import TextIOWrapper
from typing import Generator, Set
from src.utils.utils import valid_word
from typing import Generator, Set, TextIO
from src.config import SOURCES, WORDLIST
from src.utils.utils import valid_word


class WordList:
"""Represents a class that can generate and load a wordlist file for Spellsolver"""
"""
Represents a class that can generate and load a wordlist file for Spellsolver.
Attributes:
source_path (str): The path to the folder containing source files.
dest_path (str): The path to the destination wordlist file.
"""

def __init__(self) -> None:
self.source_path: str = SOURCES
self.dest_path: str = WORDLIST
def __init__(self):
"""
Initialize a WordList object with source and destination paths.
"""
self.source_path = SOURCES
self.dest_path = WORDLIST

def open_file(self) -> TextIOWrapper:
"""Load a wordlist file, if it doesn't exist it is generated"""
def open_file(self) -> TextIO:
"""
Load a wordlist file, generate it if it doesn't exist.
Returns:
TextIO: A file object representing the opened wordlist file.
"""
if not os.path.isfile(self.dest_path):
self.generate_wordlist()
print("Wordlist file successfully generated from sources")

return open(self.dest_path)

def generate_wordlist(self) -> None:
"""Generate a wordlist files from multiples files in a source folder"""
files = (
os.path.join(self.source_path, file)
for file in os.listdir(self.source_path)
)
"""
Generate a wordlist file from multiple files in a source folder.
"""
words = self.fetch_words_from_files()
self.write_words_to_file(words, path=self.dest_path)

def fetch_words_from_files(self) -> Set[str]:
"""
Fetch valid words from a list of source files.
Returns:
Set[str]: A set containing valid words from source files.
"""
words = set()
for file in os.listdir(self.source_path):
words.update(
self.read_source_file(path=os.path.join(self.source_path, file))
)
return words

for file in files:
words.update(self.read_source_file(path=file))
@staticmethod
def read_source_file(path: str) -> Generator[str, None, None]:
"""
Read valid words from a source file.
self.write_dest_file(words=words, path=self.dest_path)
Args:
path (str): The path to the source file.
def read_source_file(self, path: str) -> Generator[str, None, None]:
"""Read all valid words from a source file"""
Yields:
Generator[str, None, None]: A generator yielding valid words from the source file.
"""
with open(path) as file:
return (
word
for word in (line[:-1].lower() for line in file.readlines())
if valid_word(word)
)
for line in file.readlines():
word = line.strip().lower()
if valid_word(word):
yield word

@staticmethod
def write_words_to_file(words: Set[str], path: str) -> None:
"""
Sort valid words and write them to a destination file.
def write_dest_file(self, words: Set[str], path: str) -> None:
"""Sorts valid words and writes them to a destination file"""
Args:
words (Set[str]): A set of valid words to be written to the file.
path (str): The path to the destination file.
"""
words = sorted(words)
with open(path, "w") as f:
for word in words:
Expand Down
6 changes: 4 additions & 2 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Tuple

CHARS = set("abcdefghijklmnopqrstuvwxyz")

POINTS = {
Expand Down Expand Up @@ -30,15 +32,15 @@
}


def get_coordinate(aux_cord: int) -> tuple[int, int]:
def get_coordinate(aux_cord: int) -> Tuple[int, int]:
"""
Converts an auxiliary coordinate to a tuple of row and column indices.
Args:
aux_cord (int): The auxiliary coordinate to convert.
Returns:
tuple[int, int]: A tuple containing row and column indices.
Tuple[int, int]: A tuple containing row and column indices.
"""
aux_cord = aux_cord % 25
return aux_cord % 5, aux_cord // 5
Expand Down

0 comments on commit 93c0118

Please sign in to comment.