Remove word_utils.py

This commit is contained in:
Akbar Rahman 2019-01-18 18:28:09 +00:00
parent efe903b8b1
commit ad848e47ec
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
2 changed files with 31 additions and 30 deletions

View File

@ -2,7 +2,6 @@
import sys import sys
import re import re
import word_utils as words
SEPARATORS = [' ', '\n', 's'] SEPARATORS = [' ', '\n', 's']
@ -12,13 +11,31 @@ def get_args():
import argparse import argparse
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-m", "--macros", default=["macros"], action="append", help="Extra files where macros are stored") parser.add_argument("-m", "--macros", default=["macros"], action="append",
parser.add_argument("-s", "--silent", default=False, action="store_true", help="Do not print processed file") help="Extra files where macros are stored")
parser.add_argument("-s", "--silent", default=False, action="store_true",
help="Do not print processed file")
parser.add_argument("input", help="The file to be processed") parser.add_argument("input", help="The file to be processed")
parser.add_argument("output", help="The location of the output") parser.add_argument("output", help="The location of the output")
return parser.parse_args() return parser.parse_args()
def is_consonant(letter):
if not isinstance(letter, str):
raise ValueError("Argument 'letter' must be type str")
if len(letter) != 1:
raise ValueError("Argument 'letter' must be 1 long")
return not is_vowel(letter)
def is_vowel(letter):
if not isinstance(letter, str):
raise ValueError("Argument 'letter' must be type str")
if len(letter) != 1:
raise ValueError("Argument 'letter' must be 1 long")
return letter in 'aeiou'
def pluralize(word, macro=None): def pluralize(word, macro=None):
""" """
Returns the plural form of a word. Returns the plural form of a word.
@ -32,15 +49,16 @@ def pluralize(word, macro=None):
if word[-1] in 'sxz' or word[-2:] in ['ch', 'sh']: if word[-1] in 'sxz' or word[-2:] in ['ch', 'sh']:
return word + 'es' return word + 'es'
if word[-1] == 'y': if word[-1] == 'y':
if words.is_consonant(word[-2]): if is_consonant(word[-2]):
return word[:-1] + 'ies' return word[:-1] + 'ies'
if word[-1] == 'o': if word[-1] == 'o':
if words.is_consonant(word[-2]): if is_consonant(word[-2]):
return word + 'es' return word + 'es'
if word[-1] == 'f': if word[-1] == 'f':
return word[:-1] + 'ves' return word[:-1] + 'ves'
return word + 's' return word + 's'
def upper_check(token, word): def upper_check(token, word):
lowercase = False lowercase = False
all_caps = True all_caps = True
@ -58,6 +76,7 @@ def upper_check(token, word):
return word return word
def process(tokens, macros): def process(tokens, macros):
output = tokens output = tokens
@ -71,33 +90,27 @@ def process(tokens, macros):
# TODO add better end stripping # TODO add better end stripping
full_stopped = False full_stopped = False
if token[-1] == '.': if token[-1] == '.':
full_stopped = True full_stopped = True
token = token[:-1] token = token[:-1]
match = False # if no macro is found (or if it is not a macro at all, the value
plural = False
# if no macro is found (or if it is not a macro at all, the value
# will not be changed # will not be changed
value = token value = token
for macro in macros: for macro in macros:
if macro[0].lower() == token.lower(): if macro[0].lower() == token.lower():
match = True
value = macro[1] value = macro[1]
break break
elif macro[0].lower() + 's' == token.lower(): elif macro[0].lower() + 's' == token.lower():
match = True
plural = True
value = pluralize(macro[1], macro=macro) value = pluralize(macro[1], macro=macro)
break break
output[line_number][token_number] = upper_check(token, value) output[line_number][token_number] = upper_check(token, value)
# re-adding the full stop/period # re-adding the full stop/period
if full_stopped: if full_stopped:
output[line_number][token_number] += '.' output[line_number][token_number] += '.'
for line_number, line in enumerate(output): for line_number, line in enumerate(output):
output[line_number] = ' '.join(line) output[line_number] = ' '.join(line)
@ -105,6 +118,7 @@ def process(tokens, macros):
return output return output
def tokenize(input): def tokenize(input):
""" """
Return of list of tokens from string (convert file contents to format to be Return of list of tokens from string (convert file contents to format to be
@ -130,6 +144,7 @@ def get_macros(input):
return macros return macros
def main(args): def main(args):
""" Entry point for script """ """ Entry point for script """

View File

@ -1,14 +0,0 @@
def is_consonant(letter):
if not isinstance(letter, str):
raise ValueError("Argument 'letter' must be type str")
if len(letter) != 1:
raise ValueError("Argument 'letter' must be 1 long")
return not is_vowel(letter)
def is_vowel(letter):
if not isinstance(letter, str):
raise ValueError("Argument 'letter' must be type str")
if len(letter) != 1:
raise ValueError("Argument 'letter' must be 1 long")
return letter in 'aeiou'