diff --git a/mycroft/util/format.py b/mycroft/util/format.py index 6c655ffda0..acac1eb5a0 100755 --- a/mycroft/util/format.py +++ b/mycroft/util/format.py @@ -14,7 +14,6 @@ # from mycroft.util.lang.format_en import * -from mycroft.util.lang.format_es import * from mycroft.util.lang.format_pt import * diff --git a/mycroft/util/lang/parse_en.py b/mycroft/util/lang/parse_en.py index fef772f9e7..deabc1dcc1 100644 --- a/mycroft/util/lang/parse_en.py +++ b/mycroft/util/lang/parse_en.py @@ -14,9 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from datetime import datetime, timedelta +from datetime import datetime from dateutil.relativedelta import relativedelta -from mycroft.util.lang.parse_common import * +from mycroft.util.lang.parse_common import is_numeric, look_for_fractions def extractnumber_en(text): @@ -31,7 +31,7 @@ def extractnumber_en(text): """ aWords = text.split() aWords = [word for word in aWords if word not in ["the", "a", "an"]] - andPass = False + and_pass = False valPreAnd = False val = False count = 0 @@ -86,7 +86,7 @@ def extractnumber_en(text): # and is_numeric(aPieces[1])): if look_for_fractions(aPieces): val = float(aPieces[0]) / float(aPieces[1]) - elif andPass: + elif and_pass: # added to value, quit here val = valPreAnd break @@ -96,17 +96,17 @@ def extractnumber_en(text): aWords[count] = "" - if (andPass): + if and_pass: aWords[count - 1] = '' # remove "and" val += valPreAnd elif count + 1 < len(aWords) and aWords[count + 1] == 'and': - andPass = True + and_pass = True valPreAnd = val val = False count += 2 continue elif count + 2 < len(aWords) and aWords[count + 2] == 'and': - andPass = True + and_pass = True valPreAnd = val val = False count += 3 @@ -118,7 +118,7 @@ def extractnumber_en(text): if not val: return False - # Return the $str with the number related words removed + # Return the string with the number related words removed # (now empty strings, so strlen == 0) aWords = [word for word in aWords if len(word) > 0] text = ' '.join(aWords) @@ -126,21 +126,23 @@ def extractnumber_en(text): return val -def extract_datetime_en(str, currentDate=None): - def clean_string(str): - # cleans the input string of unneeded punctuation and capitalization - # among other things - str = str.lower().replace('?', '').replace('.', '').replace(',', '') \ +def extract_datetime_en(string, currentDate=None): + def clean_string(s): + """ + cleans the input string of unneeded punctuation and capitalization + among other things. + """ + s = s.lower().replace('?', '').replace('.', '').replace(',', '') \ .replace(' the ', ' ').replace(' a ', ' ').replace(' an ', ' ') - wordList = str.split() + wordList = s.split() for idx, word in enumerate(wordList): word = word.replace("'s", "") ordinals = ["rd", "st", "nd", "th"] if word[0].isdigit(): - for ord in ordinals: - if ord in word: - word = word.replace(ord, "") + for ordinal in ordinals: + if ordinal in word: + word = word.replace(ordinal, "") wordList[idx] = word return wordList @@ -155,7 +157,7 @@ def extract_datetime_en(str, currentDate=None): minAbs != 0 or secOffset != 0 ) - if str == "": + if string == "": return None if currentDate is None: currentDate = datetime.now() @@ -183,7 +185,7 @@ def extract_datetime_en(str, currentDate=None): monthsShort = ['jan', 'feb', 'mar', 'apr', 'may', 'june', 'july', 'aug', 'sept', 'oct', 'nov', 'dec'] - words = clean_string(str) + words = clean_string(string) for idx, word in enumerate(words): if word == "": @@ -355,7 +357,7 @@ def extract_datetime_en(str, currentDate=None): for i in range(0, used): words[i + start] = "" - if (start - 1 >= 0 and words[start - 1] in markers): + if start - 1 >= 0 and words[start - 1] in markers: words[start - 1] = "" found = True daySpecified = True @@ -456,7 +458,7 @@ def extract_datetime_en(str, currentDate=None): used += 1 elif wordNext == "in" and wordNextNext == "the" and \ words[idx + 3] == "morning": - reaminder = "am" + remainder = "am" used += 3 elif wordNext == "in" and wordNextNext == "the" and \ words[idx + 3] == "afternoon": @@ -531,7 +533,7 @@ def extract_datetime_en(str, currentDate=None): else: if wordNext == "pm" or wordNext == "p.m.": strHH = strNum - reaminder = "pm" + remainder = "pm" used = 1 elif wordNext == "am" or wordNext == "a.m.": strHH = strNum @@ -745,7 +747,7 @@ def isFractional_en(input_str): This function takes the given text and checks if it is a fraction. Args: - text (str): the string to check if fractional + input_str (str): the string to check if fractional Returns: (bool) or (float): False if not a fraction, otherwise the fraction diff --git a/mycroft/util/lang/parse_es.py b/mycroft/util/lang/parse_es.py index 221af2c8f5..2eaafac1a8 100644 --- a/mycroft/util/lang/parse_es.py +++ b/mycroft/util/lang/parse_es.py @@ -14,14 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from datetime import datetime, timedelta -from dateutil.relativedelta import relativedelta -from mycroft.util.lang.parse_common import * """ Parse functions for spanish (es) TODO: numbers greater than 999999 """ + # Undefined articles ["un", "una", "unos", "unas"] can not be supressed, # in Spanish, "un caballo" means "a horse" or "one horse". es_articles = ["el", "la", "los", "las"] @@ -32,7 +30,7 @@ es_numbers_xlat = { "una": 1, "dos": 2, "tres": 3, - u"tr�s": 3, + u"trés": 3, "cuatro": 4, "cinco": 5, "seis": 6, @@ -114,7 +112,7 @@ def es_parse(words, i): v1, i1 = r1 r2 = es_cte(i1, "y") if r2: - v2, i2 = r2 + i2 = r2[1] r3 = es_number_word(i2, 1, 9) if r3: v3, i3 = r3 @@ -153,7 +151,7 @@ def es_parse(words, i): v1, i1 = r1 r2 = es_cte(i1, "mil") if r2: - v2, i2 = r2 + i2 = r2[1] r3 = es_number_1_999(i2) if r3: v3, i3 = r3 diff --git a/mycroft/util/lang/parse_pt.py b/mycroft/util/lang/parse_pt.py index 7ee731ae65..a7df7ca8c4 100644 --- a/mycroft/util/lang/parse_pt.py +++ b/mycroft/util/lang/parse_pt.py @@ -14,9 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from datetime import datetime, timedelta -from dateutil.relativedelta import relativedelta -from mycroft.util.lang.parse_common import * """ Parse functions for Portuguese (PT-PT) @@ -24,6 +21,11 @@ from mycroft.util.lang.parse_common import * TODO: date time pt """ +from datetime import datetime +from dateutil.relativedelta import relativedelta +from mycroft.util.lang.parse_common import is_numeric, look_for_fractions + + # Undefined articles ["um", "uma", "uns", "umas"] can not be supressed, # in PT, "um cavalo" means "a horse" or "one horse". pt_articles = ["o", "a", "os", "as"] @@ -200,7 +202,7 @@ def extractnumber_pt(text): zeros += 1 else: break - for i in range(0, zeros): + for _ in range(0, zeros): afterAndVal = afterAndVal / 10.0 result += afterAndVal break @@ -274,7 +276,7 @@ def pt_number_parse(words, i): v1, i1 = r1 r2 = pt_cte(i1, "e") if r2: - v2, i2 = r2 + i2 = r2[1] r3 = pt_number_word(i2, 1, 9) if r3: v3, i3 = r3 @@ -313,7 +315,7 @@ def pt_number_parse(words, i): v1, i1 = r1 r2 = pt_cte(i1, "mil") if r2: - v2, i2 = r2 + i2 = r2[1] r3 = pt_number_1_999(i2) if r3: v3, i3 = r3 @@ -366,7 +368,7 @@ def normalize_pt(text, remove_articles): def extract_datetime_pt(input_str, currentDate=None): - def clean_string(str): + def clean_string(s): # cleans the input string of unneeded punctuation and capitalization # among other things symbols = [".", ",", ";", "?", "!", u"º", u"ª"] @@ -374,10 +376,10 @@ def extract_datetime_pt(input_str, currentDate=None): "ao", "aos"] for word in symbols: - str = str.replace(word, "") + s = s.replace(word, "") for word in noise_words: - str = str.replace(" " + word + " ", " ") - str = str.lower().replace( + s = s.replace(" " + word + " ", " ") + s = s.lower().replace( u"á", "a").replace( u"ç", @@ -408,15 +410,15 @@ def extract_datetime_pt(input_str, currentDate=None): "em": ["do", "da", "dos", "das", "de"]} for syn in synonims: for word in synonims[syn]: - str = str.replace(" " + word + " ", " " + syn + " ") + s = s.replace(" " + word + " ", " " + syn + " ") # relevant plurals, cant just extract all s in pt wordlist = ["manhas", "noites", "tardes", "dias", "semanas", "anos", "minutos", "segundos", "nas", "nos", "proximas", "seguintes", "horas"] - for idx, word in enumerate(wordlist): - str = str.replace(word, word.rstrip('s')) - str = str.replace("meses", "mes").replace("anteriores", "anterior") - return str + for _, word in enumerate(wordlist): + s = s.replace(word, word.rstrip('s')) + s = s.replace("meses", "mes").replace("anteriores", "anterior") + return s def date_found(): return found or \ @@ -472,7 +474,6 @@ def extract_datetime_pt(input_str, currentDate=None): for idx, word in enumerate(words): if word == "": continue - wordPrevPrevPrev = words[idx - 3] if idx > 2 else "" wordPrevPrev = words[idx - 2] if idx > 1 else "" wordPrev = words[idx - 1] if idx > 0 else "" wordNext = words[idx + 1] if idx + 1 < len(words) else "" @@ -773,7 +774,7 @@ def extract_datetime_pt(input_str, currentDate=None): for i in range(0, used): words[i + start] = "" - if (start - 1 >= 0 and words[start - 1] in lists): + if start - 1 >= 0 and words[start - 1] in lists: words[start - 1] = "" found = True daySpecified = True