Skip to content

Commit

Permalink
Refactored source code
Browse files Browse the repository at this point in the history
Created more explicit imports in ,  and  to reduce namespace pollution. Updated indentation accordingly.
  • Loading branch information
tomasvana10 committed Mar 16, 2024
1 parent 3cd48a9 commit ed1102f
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 175 deletions.
4 changes: 2 additions & 2 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Dict, List, Union
from pathlib import Path

import regex
from regex import compile as regex_compile


class Paths:
Expand Down Expand Up @@ -93,7 +93,7 @@ class CrosswordRestrictions:
'''Used in `definitions_parser.py` to remove all non-language characters from the words/keys of
a definitions dictionary.
'''
KEEP_LANGUAGES_PATTERN = regex.compile(r"\PL") # The opposite of \p{l} which matches characters from any language
KEEP_LANGUAGES_PATTERN = regex_compile(r"\PL") # The opposite of \p{l} which matches characters from any language


class DimensionsCalculation:
Expand Down
18 changes: 7 additions & 11 deletions src/definitions_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random
from random import sample
from typing import Dict

import regex # Similar to "re" module but with more functionality
from regex import sub # Similar to "re" module but with more functionality

from errors import (
EmptyDefinitions, InsufficientDefinitionsAndOrWordCount, ShorterDefinitionsThanWordCount,
Expand All @@ -19,8 +19,6 @@ def _parse_definitions(definitions: Dict[str, str],
This function also uses `_format_definitions` to randomly sample a specified amount of definitions
from the definitions dictionary, then format those definitions appropriately.
'''
definitions = definitions

# Required error checking
if not definitions:
raise EmptyDefinitions
Expand All @@ -30,27 +28,25 @@ def _parse_definitions(definitions: Dict[str, str],
raise ShorterDefinitionsThanWordCount
if any("\\" in word for word in definitions.keys()): # Escape character breaks regex filtering.
raise EscapeCharacterInWord

definitions = DefinitionsParser._format_definitions(definitions, word_count)

'''Removed for now; some translated crosswords have 1 letter words, and keeping this error checking
would be too inconvenient.
if not (all(len(k) >= 3 for k in definitions.keys())):
raise InsufficientWordLength
'''

return definitions
return DefinitionsParser._format_definitions(definitions, word_count)

@staticmethod
def _format_definitions(definitions: Dict[str, str],
word_count: int
) -> Dict[str, str]:
'''Randomly pick definitions from a larger sample, then remove all but language characters.'''
randomly_sampled_definitions = dict(random.sample(list(definitions.items()), word_count))
randomly_sampled_definitions = dict(sample(list(definitions.items()), word_count))

# Remove all non language chars from the keys of `randomly_sampled_definitions` (the words)
# and capitalise its values (the clues/definitions).
formatted_definitions = {regex.sub(CrosswordRestrictions.KEEP_LANGUAGES_PATTERN,
"", k).upper(): v for k, v in randomly_sampled_definitions.items()}
formatted_definitions = {sub(CrosswordRestrictions.KEEP_LANGUAGES_PATTERN,
"", k).upper(): v for k, v in randomly_sampled_definitions.items()}

return formatted_definitions
Loading

0 comments on commit ed1102f

Please sign in to comment.