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 re
import word_utils as words
SEPARATORS = [' ', '\n', 's']
@ -12,13 +11,31 @@ def get_args():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--macros", default=["macros"], action="append", 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("-m", "--macros", default=["macros"], action="append",
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("output", help="The location of the output")
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):
"""
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']:
return word + 'es'
if word[-1] == 'y':
if words.is_consonant(word[-2]):
if is_consonant(word[-2]):
return word[:-1] + 'ies'
if word[-1] == 'o':
if words.is_consonant(word[-2]):
if is_consonant(word[-2]):
return word + 'es'
if word[-1] == 'f':
return word[:-1] + 'ves'
return word + 's'
def upper_check(token, word):
lowercase = False
all_caps = True
@ -58,6 +76,7 @@ def upper_check(token, word):
return word
def process(tokens, macros):
output = tokens
@ -71,33 +90,27 @@ def process(tokens, macros):
# TODO add better end stripping
full_stopped = False
if token[-1] == '.':
full_stopped = True
full_stopped = True
token = token[:-1]
match = False
plural = False
# if no macro is found (or if it is not a macro at all, the value
# if no macro is found (or if it is not a macro at all, the value
# will not be changed
value = token
for macro in macros:
if macro[0].lower() == token.lower():
match = True
value = macro[1]
break
elif macro[0].lower() + 's' == token.lower():
match = True
plural = True
value = pluralize(macro[1], macro=macro)
break
output[line_number][token_number] = upper_check(token, value)
# re-adding the full stop/period
if full_stopped:
output[line_number][token_number] += '.'
for line_number, line in enumerate(output):
output[line_number] = ' '.join(line)
@ -105,6 +118,7 @@ def process(tokens, macros):
return output
def tokenize(input):
"""
Return of list of tokens from string (convert file contents to format to be
@ -130,6 +144,7 @@ def get_macros(input):
return macros
def main(args):
""" 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'