From 038ec101a197840f92ba492a064a7d452b0ed501 Mon Sep 17 00:00:00 2001 From: Marc Verhagen Date: Tue, 15 Dec 2020 11:42:16 -0500 Subject: [PATCH] Changing import system (issue #79). --- components/__init__.py | 23 +- components/blinker/__init__.py | 3 +- components/blinker/compare.py | 1239 +++++++++-------- components/blinker/main.py | 1 + components/blinker/wrapper.py | 1 + components/btime/btime-wrapper.py | 1 + components/classifier/vectors.py | 1 + components/classifier/wrapper.py | 1 + components/common_modules/chunks.py | 1 + components/common_modules/component.py | 1 + components/common_modules/constituent.py | 1 + components/common_modules/sentence.py | 1 + components/common_modules/tags.py | 3 +- components/common_modules/tokens.py | 1 + components/common_modules/tree.py | 1 + components/common_modules/utils.py | 1 + components/evita/bayes.py | 1 + components/evita/event.py | 1 + components/evita/features.py | 1 + components/evita/main.py | 1 + components/evita/wordnet.py | 1 + components/evita/wrapper.py | 1 + components/gutime/wrapper.py | 1 + components/merging/sputlink/graph.py | 13 +- components/merging/sputlink/main.py | 11 +- components/merging/sputlink/objects.py | 7 +- components/merging/sputlink/rules/closure.py | 1 + .../merging/sputlink/rules/generateRules.py | 1 + components/merging/wrapper.py | 1 + components/preprocessing/chunker.py | 1 + components/preprocessing/tokenizer.py | 7 +- components/preprocessing/wrapper.py | 1 + components/s2t/__init__.py | 3 +- components/s2t/main.py | 1 + components/s2t/wrapper.py | 1 + components/simpletime/main.py | 1 + components/slinket/main.py | 1 + components/slinket/wrapper.py | 1 + deprecated/demo/display.py | 1 + deprecated/get_lexes.py | 1 + deprecated/get_tags.py | 1 + deprecated/gui.py | 3 +- deprecated/sputlink/rule_creation/closure.py | 1 + .../sputlink/rule_creation/generateRules.py | 1 + deprecated/xml_parser.py | 1 + docmodel/docstructure_parser.py | 1 + docmodel/document.py | 1 + docmodel/main.py | 1 + docmodel/metadata_parser.py | 1 + docmodel/source_parser.py | 1 + docs/notes/python3.md | 60 +- library/blinker/blinker_rule_loader.py | 1 + library/classifier/create_model.py | 3 +- library/classifier/create_vectors.py | 5 +- library/classifier/path.py | 1 + library/classifier/root.py | 1 + library/evita/build_event_nominals1.py | 1 + library/evita/build_event_nominals2.py | 1 + library/evita/compile_patterns.py | 1 + library/evita/multi_chunk_patterns.py | 1 + library/evita/nominal_trainer.py | 1 + library/evita/patterns/feature_rules.py | 1 + library/forms.py | 1 + library/main.py | 1 + library/patterns.py | 1 + library/s2t/s2t_rule_loader.py | 1 + library/slinket/alinkPredicates.py | 21 +- library/slinket/create_dicts.py | 5 +- library/slinket/main.py | 1 + library/slinket/slinkPredicates.py | 25 +- library/slinket/slinketPatterns.py | 3 +- library/slinket/timeMLspec.py | 1 + root.py | 1 + tarsqi.py | 1 + testing/create_slinket_cases.py | 1 + testing/path.py | 1 + testing/regression.py | 3 +- testing/run_tests.py | 17 +- testing/scripts/regression/evita/compare.py | 1 + testing/scripts/regression/slinket/compare.py | 1 + utilities/FSA-org.py | 1 + utilities/FSA.py | 3 +- utilities/binsearch.py | 1 + utilities/code-stats.py | 1 + utilities/file.py | 1 + utilities/get_issues.py | 1 + utilities/lif.py | 1 + utilities/logger.py | 1 + utilities/make_documentation.py | 3 +- utilities/mallet.py | 3 +- utilities/modules.py | 3 +- utilities/path.py | 1 + utilities/printing.py | 1 + utilities/seeProfile.py | 1 + utilities/stemmer.py | 3 +- utilities/wntools.py | 5 +- utilities/wordnet.py | 3 +- 97 files changed, 839 insertions(+), 709 deletions(-) diff --git a/components/__init__.py b/components/__init__.py index ab7b775..db29317 100644 --- a/components/__init__.py +++ b/components/__init__.py @@ -1,19 +1,20 @@ +from __future__ import absolute_import from library.tarsqi_constants import PREPROCESSOR, TOKENIZER, TAGGER, CHUNKER from library.tarsqi_constants import GUTIME, EVITA, SLINKET from library.tarsqi_constants import S2T, CLASSIFIER, BLINKER, LINK_MERGER -from preprocessing.wrapper import PreprocessorWrapper -from preprocessing.wrapper import TokenizerWrapper -from preprocessing.wrapper import TaggerWrapper -from preprocessing.wrapper import ChunkerWrapper -from gutime.wrapper import GUTimeWrapper -from evita.wrapper import EvitaWrapper -from slinket.wrapper import SlinketWrapper -from s2t.wrapper import S2tWrapper -from blinker.wrapper import BlinkerWrapper -from classifier.wrapper import ClassifierWrapper -from merging.wrapper import MergerWrapper +from .preprocessing.wrapper import PreprocessorWrapper +from .preprocessing.wrapper import TokenizerWrapper +from .preprocessing.wrapper import TaggerWrapper +from .preprocessing.wrapper import ChunkerWrapper +from .gutime.wrapper import GUTimeWrapper +from .evita.wrapper import EvitaWrapper +from .slinket.wrapper import SlinketWrapper +from .s2t.wrapper import S2tWrapper +from .blinker.wrapper import BlinkerWrapper +from .classifier.wrapper import ClassifierWrapper +from .merging.wrapper import MergerWrapper COMPONENTS = { diff --git a/components/blinker/__init__.py b/components/blinker/__init__.py index 8ee9bae..02c5215 100644 --- a/components/blinker/__init__.py +++ b/components/blinker/__init__.py @@ -1 +1,2 @@ -import main +from __future__ import absolute_import +from . import main diff --git a/components/blinker/compare.py b/components/blinker/compare.py index 75a5ddc..23bec14 100644 --- a/components/blinker/compare.py +++ b/components/blinker/compare.py @@ -1,619 +1,620 @@ -import re -import time -import datetime -import calendar - - -month_days = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, - 9: 30, 10: 31, 11: 30, 12: 31} -seasons = ["SP", "SU", "FA", "WI"] -season_index = {"SP": 0, "SU": 1, "FA": 2, "WI": 3} - - -def test_compare_date(test_string): - (date1, date2, creation_year_int) = test_string.split() - return compare_date(date1, date2, creation_year_int) - - -def compare_date(date1, date2, creation_year_int): - - pattern = re.compile(r'(\w{1,4})(?:-(\w{1,3})(?:-(\w{1,2}))?)?') - global granularity1, granularity2, year1, year2, month1, month2, day1, day2 - - granularity1 = date1.count('-') # 0:year 1:month 2:day - granularity2 = date2.count('-') - - found1 = pattern.search(date1) - found2 = pattern.search(date2) - - year1 = found1.group(1) - year2 = found2.group(1) - - if (granularity1 >= 1): - month1 = found1.group(2) - else: - month1 = 'X' - if (granularity2 >= 1): - month2 = found2.group(2) - else: - month2 = 'X' - - if (granularity1 == 2): - day1 = found1.group(3) - else: - day1 = '0' - if (granularity2 == 2): - day2 = found2.group(3) - else: - day2 = '0' - - link = compare_year(year1, year2, creation_year_int) - if (link == "VAGUE"): - return "VAGUE" - - if (link == "BEFORE" or - link == "AFTER" or - link == "INCLUDES" or - link == "IS_INCLUDED" or - link == "IDENTITY"): - return link - - elif (link == "NEED_COMPARE_IN_YEAR"): - - if (granularity1 == 0 and granularity2 == 0): - link = "IDENTITY" - return link - elif (granularity1 == 0): - link = "INCLUDES" - return link - elif (granularity2 == 0): - link = "IS_INCLUDED" - return link - else: - link = compare_in_year() - return link - - -def year_alpha_to_digit(year, creation_year_int): - year_int = None - if(year.isdigit()): - year_int = int(year) - elif(re.match("BC", year)): - year_int = 0 - int(year[2:]) - elif(re.match("KA", year)): - year_int = creation_year_int - int(year[2:])*1000 - elif(re.match("MA", year)): - year_int = creation_year_int - int(year[2:])*1000000 - elif(re.match("GA", year)): - year_int = creation_year_int - int(year[2:])*1000000000 - return year_int - - -def compare_year(year1, year2, creation_year_int): - '''year1 and year2 both are of string type. - return: the link between two years''' - - if (year1[0] == "X" or year2[0] == "X"): - return "VAGUE" - - if (year1.isdigit() and year2.isdigit()): - if (len(year1) == len(year2)): - year1_int = int(year1) - year2_int = int(year2) - if (need_compare_at_year_boundary(year1, year2, month1, month2, day1, day2)): - if (re.match(r'W', month2)): - link = compare_month_week_at_year_boundary( - int(year1), int(year2), - int(month1), int(strip_week(month2)), - int(day1), int(day2)) - return link - elif(re.match(r'W', month1)): - link = compare_month_week_at_year_boundary( - int(year2), int(year1), - int(month2), int(strip_week(month1)), - int(day2), int(day1)) - return reverse_link(link) - - if (year1_int < year2_int): # and - # (year2_int - year1_int)!= 1 and - # (month2 != "01" and month1 != "W52") or - # (month2 != "W1" and month1 != "12")) ): - return "BEFORE" - elif (year1_int > year2_int): # and - # (year1_int - year2_int)!=1 and - # ( ( month1 != "01" and month2 != "W52") or - # ( month1 != "W1" and month2 != "12")) ): - return "AFTER" - else: - return "NEED_COMPARE_IN_YEAR" - - else: - return compare_short_year(year1, year2) - - elif(re.match("BC", year1) and re.match("BC", year2)): - if (len(year1[2:]) == len(year2[2:])): - year1_int = year_alpha_to_digit(year1, creation_year_int) - year2_int = year_alpha_to_digit(year2, creation_year_int) - if (year1_int < year2_int): - return "BEFORE" - elif(year1_int > year2_int): - return "AFTER" - else: - return "NEED_COMPARE_IN_YEAR" - else: - link_temp = compare_short_year(year1[2:], year2[2:]) - link = reverse_link(link_temp) - return link - else: - year1_int = year_alpha_to_digit(year1, creation_year_int) - year2_int = year_alpha_to_digit(year2, creation_year_int) - if (year1_int < year2_int): - return "BEFORE" - elif (year1_int > year2_int): - return "AFTER" - else: - return "NEED_COMPARE_IN_YEAR" - - # elif(year1.isdigit() or year2.isdigit()): - # return "ONLY_ONE_IS_PURE_DIGITAL" - - # else: pass - - -def compare_short_year(year1, year2): - '''year1 and year2 both are of string type''' - if(len(year1) < len(year2)): - year1_temp = year1 - year2_temp = year2[0:len(year1)] - shorter_year = 1 - else: - year1_temp = year1[0:len(year2)] - year2_temp = year2 - shorter_year = 2 - - year1_int = int(year1_temp) - year2_int = int(year2_temp) - - if (year1_int < year2_int): - return "BEFORE" - elif (year1_int > year2_int): - return "AFTER" - else: - if (shorter_year == 1): - return "INCLUDES" - elif (shorter_year == 2): - return "IS_INCLUDED" - - -def compare_in_year(): - - month1_temp = month1 - month2_temp = month2 - month1_type = type_of_month(month1_temp) - month2_type = type_of_month(month2_temp) - - if(month1_type == "VAGUE" or month2_type == "VAGUE"): - link = "VAGUE" - - if(month1_type == "SEASON" or month2_type == "SEASON"): - link = "VAGUE" - - elif(month1_type == "MONTH" and month2_type == "MONTH"): - link = compare_month(month1_temp, month2_temp) - - elif((month1_type == "MONTH" and month2_type == "WEEK") or - (month1_type == "WEEK" and month2_type == "MONTH")): - link = compare_week_month(month1_temp, month2_temp) - -# elif( (month1_type == "MONTH" and month2_type == "SEASON") -# or -# (month1_type == "SEASON" and month2_type == "MONTH")): -# link = compare_season_month(month1_temp, month2_temp) - - elif(month1_type == "WEEK" and month2_type == "WEEK"): - link = compare_week(month1_temp, month2_temp) - -# elif(month1_type == "SEASON" and month2_type == "SEASON"): -# link = compare_season(month1_temp, month2_temp) - -# elif( (month1_type == "SEASON" and month2_type == "WEEK") -# or -# (month1_type == "WEEK" and month2_type == "SEASON") ): -# link = compare_week_season(month1_temp, month2_temp) - - if (link == "BEFORE" or - link == "AFTER" or - link == "IDENTITY" or - link == "INCLUDES" or - link == "IS_INCLUDED"): - return link - elif (link == "NEED_COMPARE_DAY"): - if (granularity1 == 1 and granularity2 == 1): - link = "IDENTITY" - return link - elif (granularity1 == 1): - link = "INCLUDES" - return link - elif (granularity2 == 1): - link = "IS_INCLUDED" - return link - else: - link = compare_day(day1, day2) - return link - else: - return "VAGUE" - - -def compare_week(week1, week2): - '''week1 and week2 are strings containing 'W' indicating the type is week. - Also suppose weeks do not contain 'X' ''' - return simple_compare(week1, week2) - - -def compare_month(month1, month2): - return simple_compare(month1, month2) - - -def simple_compare(time1, time2): - '''used only in month section''' - if(time1 < time2): - return "BEFORE" - elif(time1 > time2): - return "AFTER" - else: - return "NEED_COMPARE_DAY" - -# def compare_week_season(month1, month2): -# month1_type = type_of_month(month1) -# if(month1_type == "WEEK"): -# #month1 is week and month2 is season -# link = compare_week_season_aux(month1, month2) -# return link -# else: #othewise month1 is season, month2 is week -# link = compare_week_season_aux(month2, month1) -# return reverse_link(link) - - -# def compare_week_season_aux(week, season): - # according to the AHP rule, this comparison function is better - # written after compare(season, month) and compare(month, week) - -# week = strip_week(week) -# week_float = float(week) - -# if(season == "WI"): -# return "VAGUE" -# elif(season == "SP"): -# adjust = 1.0 -# else: -# adjust = 1.5 - -# season_to_week_begin = (season_index[season]*3+2)*4.35 -# season_to_week_end = season_to_week_begin + 2*4.35 - -# if(week_float >= (season_to_week_begin + adjust) and -# week_float <= (season_to_week_end)): -# return "IS_INCLUDED" -# elif(week_float > (season_to_week_end + adjust + 4.35)):#add 4.35 to be confident -# return "AFTER" -# elif(week_float < (season_to_week_begin - 4.35)): #minus 4.35 to be confident -# return "BEFORE" -# else: return "VAGUE" - - -def compare_week_month(month1, month2): - month1_type = type_of_month(month1) - if(month1_type == "WEEK"): - # month1 is week, then month2 is month - return compare_week_month_aux(month1, month2, day1, day2) - else: - # month2 is week and month1 is month - link = compare_week_month_aux(month2, month1, day2, day1) - return reverse_link(link) - - -def compare_week_month_aux(week, month, week_day, month_day): - # year is an integer, week is string, month is string, week_day is string, - # month_day is string - week = strip_week(week) - week_int = int(week) - month_int = int(month) - month_day_int = int(month_day) - - if(week_day == "WE"): - week_day_int = 6 - link6 = compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int) - week_day_int = 7 - link7 = compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int) - if ((link6 == "IDENTITY" and link7 == "AFTER") or - (link7 == "IDENTITY" and link6 == "BEFORE")): - return "INCLUDES" - elif (link6 == link7): - return link6 - else: - week_day_int = int(week_day) - - link = compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int) - return link - - -def compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int): - if (month_day_int == 0): - # no month day - month_date_begin = datetime.date(int(year1), month_int, 1) - if (calendar.isleap(int(year1)) and month_int == 2): - month_date_end = datetime.date(int(year1), month_int, month_days[month_int] + 1) - else: - month_date_end = datetime.date(int(year1), month_int, month_days[month_int]) - - month_to_week_begin = month_date_begin.isocalendar() - month_to_week_end = month_date_end.isocalendar() - - # Here should be very careful as a month date when converted to week, it - # may fall into the week in the year before or it may also fall into the - # week in the year after - - # if(int(year1) > month_to_week_begin[0] and int(year1)): - # return "AFTER" - if(week_int < month_to_week_begin[1] and (int(year1) == month_to_week_begin[0])): - return "BEFORE" - # elif(int(year1) < month_to_week_begin[0] ): - # return "BEFORE" - elif (week_int > month_to_week_end[1] and (int(year1) == month_to_week_end[0])): - return "AFTER" - elif (((week_int == month_to_week_begin[1]) and (int(year1) == month_to_week_begin[0])) or - ((week_int == month_to_week_end[1]) and (int(year1) == month_to_week_end[0]))): - if (week_day_int == 0): - return "VAGUE" - elif (week_day_int < month_to_week_begin[2]): - return "BEFORE" - elif (week_day_int > month_to_week_end[2]): - return "AFTER" - else: - return "IS_INCLUDED" - - else: - month_date = datetime.date(int(year1), month_int, month_day_int) - month_to_week = month_date.isocalendar() - if (week_int < month_to_week[1] and (int(year1) == month_to_week[0])): - return "BEFORE" - elif (int(year1) > month_to_week[0]): - return "AFTER" - elif (week_int > month_to_week[1] and (int(year1) == month_to_week[0])): - return "AFTER" - elif (int(year1) < month_to_week[0]): - return "BEFORE" - elif (week_int == month_to_week[1]): - if (week_day_int == 0): - return "INCLUDES" - elif (week_day_int > month_to_week[2]): - return "AFTER" - elif (week_day_int < month_to_week[2]): - return "BEFORE" - else: - return "IDENTITY" - - -def compare_month_week_at_year_boundary(month_year, week_year, month, week, month_day, week_day): - "compare month to week, parameters are all integers" - if (month_day != 0): - month_date = datetime.date(month_year, month, month_day) - month_week = month_date.isocalendar() - # put the compare year and week at place where it is called - if (week_day != 0): - if (month_week[2] < week_day): - return "BEFORE" - if (month_week[2] > week_day): - return "AFTER" - else: - return "IDENTITY" - else: - return "IS_INCLUDED" - else: - if (week_day == 0): - return "VAGUE" - else: - month_date = datetime.date(month_year, month, 31) - month_week = month_date.isocalendar() - if (month_week[2] < week_day): - return "BEFORE" - if (month_week[2] > week_day or month_week[2] == week_day): - return "INCLUDES" - - -def need_compare_at_year_boundary(year1, year2, month1, month2, day1, day2): - if ((re.match(r'W', month1) and re.match(r'[^WX]', month2))): - if (int(month2) == 12): - month_date2 = datetime.date(int(year2), int(month2), 31) - month_week2 = month_date2.isocalendar() - if (int(month2) == 1): - month_date2 = datetime.date(int(year2), int(month2), 1) - month_week2 = month_date2.isocalendar() - else: - return False - if (int(year1) == month_week2[0] and int(strip_week(month1)) == month_week2[1]): - return True - else: - return False - elif ((re.match(r'W', month2) and re.match(r'[^WX]', month1))): - if (int(month1) == 12): - month_date1 = datetime.date(int(year1), int(month1), 31) - month_week1 = month_date1.isocalendar() - if (int(month1) == 1): - month_date1 = datetime.date(int(year1), int(month1), 1) - month_week1 = month_date1.isocalendar() - else: - return False - if (int(year2) == month_week1[0] and int(strip_week(month2)) == month_week1[1]): - return True - else: - return False - else: - return False - - -# def compare_week_month_aux(week, month, day): -# -# week = strip_week(week) -# -# week_float = float(week) -# month_float = float(month) -# -# month_to_week_begin = (month_float-1) * 4.35 #4.35 ~ [30 or 31]/7 -# month_to_week_end = month_to_week_begin + 4.35 -# -# if(month_float <= 6): -# week_adjust = 1.0 #1.4 is the allowed error range -# #as the months increases, allow larger error range 1.8 -# else: week_adjust = 1.5 -# -# if(day==0): -# if(week_float >= (month_to_week_begin + week_adjust) and -# week_float <= month_to_week_end ): -# return "IS_INCLUDED" -# elif(week_float > (month_to_week_end + week_adjust) ): -# return "AFTER" -# elif(week_float < (month_to_week_begin)): -# return "BEFORE" -# else: return "VAGUE" -# else: -# if(week_float < (month_to_week_begin - week_adjust + day/7) ): -# return "BEFORE" -# elif(week_float > (month_to_week_begin + day/7) ): -# return "AFTER" -# else: return "VAGUE" - - -# def compare_season_month(month1, month2): -# -# month1_type = type_of_month(month1) -# if(month1_type == "MONTH"): -# #month1 is month, month2 is season -# return compare_season_month_aux(month1, month2) -# else: #otherwise month1 is season, month2 is month -# link = compare_season_month_aux(month2, month1) -# return reverse_link(link) - - -# def compare_season_month_aux(month, season): -# '''month and season are both strings''' -# -# month_int = int(month) -# #not suitable for winter -# season_to_month_begin = season_index[season]*3+3 -# season_to_month_end = season_to_month_begin + 1 -# -# if(season == "WI"): -# #as winter can be in the beginning of a year -# #and also can be in the end of a year -# #it cannot be accurately decided -# return "VAGUE" -# elif(month_int >= season_to_month_begin and -# month_int <= season_to_month_end ): -# return "IS_INCLUDED" -# elif(month_int < (season_to_month_begin - 1) ): #minus 1 to be confident -# return "BEFORE" -# elif(month_int > season_to_month_end + 1): #add 1 to be confident -# return "AFTER" -# else: return "VAGUE" #leave the two months out as vague to be confident - - -# def compare_season(season1, season2): -# '''season1 and season2 are strings indicating season''' -# season1_temp = season1 -# season2_temp = season2 -# -# if(season1_temp == "FA"): -# season1_temp = "UT" #aUTumn -# if(season2_temp == "FA"): -# season2_temp = "UT" #aUTumn -# -# if(season1_temp == "WI" or season2_temp == "WI"): -# return "VAGUE" -# -# #string comparison, a lazy way -# if(season1_tempseason2_temp): -# return "AFTER" -# else: return "IDENTITY" - - -def type_of_month(month): - '''types include: "MONTH", "WEEK", "SEASON", "VAGUE" ''' - # the order of the following judgement is important as - # week and day can both have "X" inside - if (month.find('X') >= 0): - return "VAGUE" - if (month.isdigit()): - return "MONTH" - if (month[0] == "W" and month[1:].isdigit()): - return "WEEK" - if (month in seasons): - return "SEASON" - - -def type_of_year(year): - if (year.find('X') >= 0): - return "VAGUE" - elif (year.isdigit()): - return "YEAR" - elif (year.find("BC") >= 0): - return "BC" - elif (year.find("KA") >= 0): - return "KA" - elif (year.find("MA") >= 0): - return "MA" - elif (year.find("GA") >= 0): - return "GA" - - -def type_of_day(day): - if (day.find('X') >= 0): - return "VAGUE" - else: - return "DAY" - - -def compare_day(day1, day2): - '''day1 and day2 are strings indicating day''' - day1_type = type_of_day(day1) - day2_type = type_of_day(day2) - if (day1_type == "VAGUE" or day2_type == "VAGUE"): - return "VAGUE" - else: - if (day1 < day2): - return "BEFORE" - elif (day1 > day2): - return "AFTER" - else: - return "IDENTITY" - - -def reverse_link(link): - if (link == "VAGUE"): - return "VAGUE" - elif (link == "INCLUDES"): - return "IS_INCLUDED" - elif (link == "IS_INCLUDED"): - return "INCLUDES" - elif (link == "AFTER"): - return "BEFORE" - elif (link == "BEFORE"): - return "AFTER" - elif (link == "IDENTITY"): - return "INDENTITY" - - -def get_current_year(): - '''input: none - output: integer''' - time_obj = time.time() - current_time = time.ctime(time_obj) - year_string = current_time[-4:] - year_int = int(year_string) - return year_int - - -def strip_week(week): - return week[1:] +from __future__ import absolute_import +import re +import time +import datetime +import calendar + + +month_days = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, + 9: 30, 10: 31, 11: 30, 12: 31} +seasons = ["SP", "SU", "FA", "WI"] +season_index = {"SP": 0, "SU": 1, "FA": 2, "WI": 3} + + +def test_compare_date(test_string): + (date1, date2, creation_year_int) = test_string.split() + return compare_date(date1, date2, creation_year_int) + + +def compare_date(date1, date2, creation_year_int): + + pattern = re.compile(r'(\w{1,4})(?:-(\w{1,3})(?:-(\w{1,2}))?)?') + global granularity1, granularity2, year1, year2, month1, month2, day1, day2 + + granularity1 = date1.count('-') # 0:year 1:month 2:day + granularity2 = date2.count('-') + + found1 = pattern.search(date1) + found2 = pattern.search(date2) + + year1 = found1.group(1) + year2 = found2.group(1) + + if (granularity1 >= 1): + month1 = found1.group(2) + else: + month1 = 'X' + if (granularity2 >= 1): + month2 = found2.group(2) + else: + month2 = 'X' + + if (granularity1 == 2): + day1 = found1.group(3) + else: + day1 = '0' + if (granularity2 == 2): + day2 = found2.group(3) + else: + day2 = '0' + + link = compare_year(year1, year2, creation_year_int) + if (link == "VAGUE"): + return "VAGUE" + + if (link == "BEFORE" or + link == "AFTER" or + link == "INCLUDES" or + link == "IS_INCLUDED" or + link == "IDENTITY"): + return link + + elif (link == "NEED_COMPARE_IN_YEAR"): + + if (granularity1 == 0 and granularity2 == 0): + link = "IDENTITY" + return link + elif (granularity1 == 0): + link = "INCLUDES" + return link + elif (granularity2 == 0): + link = "IS_INCLUDED" + return link + else: + link = compare_in_year() + return link + + +def year_alpha_to_digit(year, creation_year_int): + year_int = None + if(year.isdigit()): + year_int = int(year) + elif(re.match("BC", year)): + year_int = 0 - int(year[2:]) + elif(re.match("KA", year)): + year_int = creation_year_int - int(year[2:])*1000 + elif(re.match("MA", year)): + year_int = creation_year_int - int(year[2:])*1000000 + elif(re.match("GA", year)): + year_int = creation_year_int - int(year[2:])*1000000000 + return year_int + + +def compare_year(year1, year2, creation_year_int): + '''year1 and year2 both are of string type. + return: the link between two years''' + + if (year1[0] == "X" or year2[0] == "X"): + return "VAGUE" + + if (year1.isdigit() and year2.isdigit()): + if (len(year1) == len(year2)): + year1_int = int(year1) + year2_int = int(year2) + if (need_compare_at_year_boundary(year1, year2, month1, month2, day1, day2)): + if (re.match(r'W', month2)): + link = compare_month_week_at_year_boundary( + int(year1), int(year2), + int(month1), int(strip_week(month2)), + int(day1), int(day2)) + return link + elif(re.match(r'W', month1)): + link = compare_month_week_at_year_boundary( + int(year2), int(year1), + int(month2), int(strip_week(month1)), + int(day2), int(day1)) + return reverse_link(link) + + if (year1_int < year2_int): # and + # (year2_int - year1_int)!= 1 and + # (month2 != "01" and month1 != "W52") or + # (month2 != "W1" and month1 != "12")) ): + return "BEFORE" + elif (year1_int > year2_int): # and + # (year1_int - year2_int)!=1 and + # ( ( month1 != "01" and month2 != "W52") or + # ( month1 != "W1" and month2 != "12")) ): + return "AFTER" + else: + return "NEED_COMPARE_IN_YEAR" + + else: + return compare_short_year(year1, year2) + + elif(re.match("BC", year1) and re.match("BC", year2)): + if (len(year1[2:]) == len(year2[2:])): + year1_int = year_alpha_to_digit(year1, creation_year_int) + year2_int = year_alpha_to_digit(year2, creation_year_int) + if (year1_int < year2_int): + return "BEFORE" + elif(year1_int > year2_int): + return "AFTER" + else: + return "NEED_COMPARE_IN_YEAR" + else: + link_temp = compare_short_year(year1[2:], year2[2:]) + link = reverse_link(link_temp) + return link + else: + year1_int = year_alpha_to_digit(year1, creation_year_int) + year2_int = year_alpha_to_digit(year2, creation_year_int) + if (year1_int < year2_int): + return "BEFORE" + elif (year1_int > year2_int): + return "AFTER" + else: + return "NEED_COMPARE_IN_YEAR" + + # elif(year1.isdigit() or year2.isdigit()): + # return "ONLY_ONE_IS_PURE_DIGITAL" + + # else: pass + + +def compare_short_year(year1, year2): + '''year1 and year2 both are of string type''' + if(len(year1) < len(year2)): + year1_temp = year1 + year2_temp = year2[0:len(year1)] + shorter_year = 1 + else: + year1_temp = year1[0:len(year2)] + year2_temp = year2 + shorter_year = 2 + + year1_int = int(year1_temp) + year2_int = int(year2_temp) + + if (year1_int < year2_int): + return "BEFORE" + elif (year1_int > year2_int): + return "AFTER" + else: + if (shorter_year == 1): + return "INCLUDES" + elif (shorter_year == 2): + return "IS_INCLUDED" + + +def compare_in_year(): + + month1_temp = month1 + month2_temp = month2 + month1_type = type_of_month(month1_temp) + month2_type = type_of_month(month2_temp) + + if(month1_type == "VAGUE" or month2_type == "VAGUE"): + link = "VAGUE" + + if(month1_type == "SEASON" or month2_type == "SEASON"): + link = "VAGUE" + + elif(month1_type == "MONTH" and month2_type == "MONTH"): + link = compare_month(month1_temp, month2_temp) + + elif((month1_type == "MONTH" and month2_type == "WEEK") or + (month1_type == "WEEK" and month2_type == "MONTH")): + link = compare_week_month(month1_temp, month2_temp) + +# elif( (month1_type == "MONTH" and month2_type == "SEASON") +# or +# (month1_type == "SEASON" and month2_type == "MONTH")): +# link = compare_season_month(month1_temp, month2_temp) + + elif(month1_type == "WEEK" and month2_type == "WEEK"): + link = compare_week(month1_temp, month2_temp) + +# elif(month1_type == "SEASON" and month2_type == "SEASON"): +# link = compare_season(month1_temp, month2_temp) + +# elif( (month1_type == "SEASON" and month2_type == "WEEK") +# or +# (month1_type == "WEEK" and month2_type == "SEASON") ): +# link = compare_week_season(month1_temp, month2_temp) + + if (link == "BEFORE" or + link == "AFTER" or + link == "IDENTITY" or + link == "INCLUDES" or + link == "IS_INCLUDED"): + return link + elif (link == "NEED_COMPARE_DAY"): + if (granularity1 == 1 and granularity2 == 1): + link = "IDENTITY" + return link + elif (granularity1 == 1): + link = "INCLUDES" + return link + elif (granularity2 == 1): + link = "IS_INCLUDED" + return link + else: + link = compare_day(day1, day2) + return link + else: + return "VAGUE" + + +def compare_week(week1, week2): + '''week1 and week2 are strings containing 'W' indicating the type is week. + Also suppose weeks do not contain 'X' ''' + return simple_compare(week1, week2) + + +def compare_month(month1, month2): + return simple_compare(month1, month2) + + +def simple_compare(time1, time2): + '''used only in month section''' + if(time1 < time2): + return "BEFORE" + elif(time1 > time2): + return "AFTER" + else: + return "NEED_COMPARE_DAY" + +# def compare_week_season(month1, month2): +# month1_type = type_of_month(month1) +# if(month1_type == "WEEK"): +# #month1 is week and month2 is season +# link = compare_week_season_aux(month1, month2) +# return link +# else: #othewise month1 is season, month2 is week +# link = compare_week_season_aux(month2, month1) +# return reverse_link(link) + + +# def compare_week_season_aux(week, season): + # according to the AHP rule, this comparison function is better + # written after compare(season, month) and compare(month, week) + +# week = strip_week(week) +# week_float = float(week) + +# if(season == "WI"): +# return "VAGUE" +# elif(season == "SP"): +# adjust = 1.0 +# else: +# adjust = 1.5 + +# season_to_week_begin = (season_index[season]*3+2)*4.35 +# season_to_week_end = season_to_week_begin + 2*4.35 + +# if(week_float >= (season_to_week_begin + adjust) and +# week_float <= (season_to_week_end)): +# return "IS_INCLUDED" +# elif(week_float > (season_to_week_end + adjust + 4.35)):#add 4.35 to be confident +# return "AFTER" +# elif(week_float < (season_to_week_begin - 4.35)): #minus 4.35 to be confident +# return "BEFORE" +# else: return "VAGUE" + + +def compare_week_month(month1, month2): + month1_type = type_of_month(month1) + if(month1_type == "WEEK"): + # month1 is week, then month2 is month + return compare_week_month_aux(month1, month2, day1, day2) + else: + # month2 is week and month1 is month + link = compare_week_month_aux(month2, month1, day2, day1) + return reverse_link(link) + + +def compare_week_month_aux(week, month, week_day, month_day): + # year is an integer, week is string, month is string, week_day is string, + # month_day is string + week = strip_week(week) + week_int = int(week) + month_int = int(month) + month_day_int = int(month_day) + + if(week_day == "WE"): + week_day_int = 6 + link6 = compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int) + week_day_int = 7 + link7 = compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int) + if ((link6 == "IDENTITY" and link7 == "AFTER") or + (link7 == "IDENTITY" and link6 == "BEFORE")): + return "INCLUDES" + elif (link6 == link7): + return link6 + else: + week_day_int = int(week_day) + + link = compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int) + return link + + +def compare_week_month_aux_aux(week_int, month_int, week_day_int, month_day_int): + if (month_day_int == 0): + # no month day + month_date_begin = datetime.date(int(year1), month_int, 1) + if (calendar.isleap(int(year1)) and month_int == 2): + month_date_end = datetime.date(int(year1), month_int, month_days[month_int] + 1) + else: + month_date_end = datetime.date(int(year1), month_int, month_days[month_int]) + + month_to_week_begin = month_date_begin.isocalendar() + month_to_week_end = month_date_end.isocalendar() + + # Here should be very careful as a month date when converted to week, it + # may fall into the week in the year before or it may also fall into the + # week in the year after + + # if(int(year1) > month_to_week_begin[0] and int(year1)): + # return "AFTER" + if(week_int < month_to_week_begin[1] and (int(year1) == month_to_week_begin[0])): + return "BEFORE" + # elif(int(year1) < month_to_week_begin[0] ): + # return "BEFORE" + elif (week_int > month_to_week_end[1] and (int(year1) == month_to_week_end[0])): + return "AFTER" + elif (((week_int == month_to_week_begin[1]) and (int(year1) == month_to_week_begin[0])) or + ((week_int == month_to_week_end[1]) and (int(year1) == month_to_week_end[0]))): + if (week_day_int == 0): + return "VAGUE" + elif (week_day_int < month_to_week_begin[2]): + return "BEFORE" + elif (week_day_int > month_to_week_end[2]): + return "AFTER" + else: + return "IS_INCLUDED" + + else: + month_date = datetime.date(int(year1), month_int, month_day_int) + month_to_week = month_date.isocalendar() + if (week_int < month_to_week[1] and (int(year1) == month_to_week[0])): + return "BEFORE" + elif (int(year1) > month_to_week[0]): + return "AFTER" + elif (week_int > month_to_week[1] and (int(year1) == month_to_week[0])): + return "AFTER" + elif (int(year1) < month_to_week[0]): + return "BEFORE" + elif (week_int == month_to_week[1]): + if (week_day_int == 0): + return "INCLUDES" + elif (week_day_int > month_to_week[2]): + return "AFTER" + elif (week_day_int < month_to_week[2]): + return "BEFORE" + else: + return "IDENTITY" + + +def compare_month_week_at_year_boundary(month_year, week_year, month, week, month_day, week_day): + "compare month to week, parameters are all integers" + if (month_day != 0): + month_date = datetime.date(month_year, month, month_day) + month_week = month_date.isocalendar() + # put the compare year and week at place where it is called + if (week_day != 0): + if (month_week[2] < week_day): + return "BEFORE" + if (month_week[2] > week_day): + return "AFTER" + else: + return "IDENTITY" + else: + return "IS_INCLUDED" + else: + if (week_day == 0): + return "VAGUE" + else: + month_date = datetime.date(month_year, month, 31) + month_week = month_date.isocalendar() + if (month_week[2] < week_day): + return "BEFORE" + if (month_week[2] > week_day or month_week[2] == week_day): + return "INCLUDES" + + +def need_compare_at_year_boundary(year1, year2, month1, month2, day1, day2): + if ((re.match(r'W', month1) and re.match(r'[^WX]', month2))): + if (int(month2) == 12): + month_date2 = datetime.date(int(year2), int(month2), 31) + month_week2 = month_date2.isocalendar() + if (int(month2) == 1): + month_date2 = datetime.date(int(year2), int(month2), 1) + month_week2 = month_date2.isocalendar() + else: + return False + if (int(year1) == month_week2[0] and int(strip_week(month1)) == month_week2[1]): + return True + else: + return False + elif ((re.match(r'W', month2) and re.match(r'[^WX]', month1))): + if (int(month1) == 12): + month_date1 = datetime.date(int(year1), int(month1), 31) + month_week1 = month_date1.isocalendar() + if (int(month1) == 1): + month_date1 = datetime.date(int(year1), int(month1), 1) + month_week1 = month_date1.isocalendar() + else: + return False + if (int(year2) == month_week1[0] and int(strip_week(month2)) == month_week1[1]): + return True + else: + return False + else: + return False + + +# def compare_week_month_aux(week, month, day): +# +# week = strip_week(week) +# +# week_float = float(week) +# month_float = float(month) +# +# month_to_week_begin = (month_float-1) * 4.35 #4.35 ~ [30 or 31]/7 +# month_to_week_end = month_to_week_begin + 4.35 +# +# if(month_float <= 6): +# week_adjust = 1.0 #1.4 is the allowed error range +# #as the months increases, allow larger error range 1.8 +# else: week_adjust = 1.5 +# +# if(day==0): +# if(week_float >= (month_to_week_begin + week_adjust) and +# week_float <= month_to_week_end ): +# return "IS_INCLUDED" +# elif(week_float > (month_to_week_end + week_adjust) ): +# return "AFTER" +# elif(week_float < (month_to_week_begin)): +# return "BEFORE" +# else: return "VAGUE" +# else: +# if(week_float < (month_to_week_begin - week_adjust + day/7) ): +# return "BEFORE" +# elif(week_float > (month_to_week_begin + day/7) ): +# return "AFTER" +# else: return "VAGUE" + + +# def compare_season_month(month1, month2): +# +# month1_type = type_of_month(month1) +# if(month1_type == "MONTH"): +# #month1 is month, month2 is season +# return compare_season_month_aux(month1, month2) +# else: #otherwise month1 is season, month2 is month +# link = compare_season_month_aux(month2, month1) +# return reverse_link(link) + + +# def compare_season_month_aux(month, season): +# '''month and season are both strings''' +# +# month_int = int(month) +# #not suitable for winter +# season_to_month_begin = season_index[season]*3+3 +# season_to_month_end = season_to_month_begin + 1 +# +# if(season == "WI"): +# #as winter can be in the beginning of a year +# #and also can be in the end of a year +# #it cannot be accurately decided +# return "VAGUE" +# elif(month_int >= season_to_month_begin and +# month_int <= season_to_month_end ): +# return "IS_INCLUDED" +# elif(month_int < (season_to_month_begin - 1) ): #minus 1 to be confident +# return "BEFORE" +# elif(month_int > season_to_month_end + 1): #add 1 to be confident +# return "AFTER" +# else: return "VAGUE" #leave the two months out as vague to be confident + + +# def compare_season(season1, season2): +# '''season1 and season2 are strings indicating season''' +# season1_temp = season1 +# season2_temp = season2 +# +# if(season1_temp == "FA"): +# season1_temp = "UT" #aUTumn +# if(season2_temp == "FA"): +# season2_temp = "UT" #aUTumn +# +# if(season1_temp == "WI" or season2_temp == "WI"): +# return "VAGUE" +# +# #string comparison, a lazy way +# if(season1_tempseason2_temp): +# return "AFTER" +# else: return "IDENTITY" + + +def type_of_month(month): + '''types include: "MONTH", "WEEK", "SEASON", "VAGUE" ''' + # the order of the following judgement is important as + # week and day can both have "X" inside + if (month.find('X') >= 0): + return "VAGUE" + if (month.isdigit()): + return "MONTH" + if (month[0] == "W" and month[1:].isdigit()): + return "WEEK" + if (month in seasons): + return "SEASON" + + +def type_of_year(year): + if (year.find('X') >= 0): + return "VAGUE" + elif (year.isdigit()): + return "YEAR" + elif (year.find("BC") >= 0): + return "BC" + elif (year.find("KA") >= 0): + return "KA" + elif (year.find("MA") >= 0): + return "MA" + elif (year.find("GA") >= 0): + return "GA" + + +def type_of_day(day): + if (day.find('X') >= 0): + return "VAGUE" + else: + return "DAY" + + +def compare_day(day1, day2): + '''day1 and day2 are strings indicating day''' + day1_type = type_of_day(day1) + day2_type = type_of_day(day2) + if (day1_type == "VAGUE" or day2_type == "VAGUE"): + return "VAGUE" + else: + if (day1 < day2): + return "BEFORE" + elif (day1 > day2): + return "AFTER" + else: + return "IDENTITY" + + +def reverse_link(link): + if (link == "VAGUE"): + return "VAGUE" + elif (link == "INCLUDES"): + return "IS_INCLUDED" + elif (link == "IS_INCLUDED"): + return "INCLUDES" + elif (link == "AFTER"): + return "BEFORE" + elif (link == "BEFORE"): + return "AFTER" + elif (link == "IDENTITY"): + return "INDENTITY" + + +def get_current_year(): + '''input: none + output: integer''' + time_obj = time.time() + current_time = time.ctime(time_obj) + year_string = current_time[-4:] + year_int = int(year_string) + return year_int + + +def strip_week(week): + return week[1:] diff --git a/components/blinker/main.py b/components/blinker/main.py index 518863c..a1a87d9 100644 --- a/components/blinker/main.py +++ b/components/blinker/main.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import import re from utilities import logger diff --git a/components/blinker/wrapper.py b/components/blinker/wrapper.py index 379a05e..8cb1d4f 100644 --- a/components/blinker/wrapper.py +++ b/components/blinker/wrapper.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import from library.tarsqi_constants import BLINKER from components.blinker.main import Blinker diff --git a/components/btime/btime-wrapper.py b/components/btime/btime-wrapper.py index a274e19..181b0e2 100644 --- a/components/btime/btime-wrapper.py +++ b/components/btime/btime-wrapper.py @@ -24,6 +24,7 @@ class will at least have methods text(), tag(), begin(), end() and __unicode__() """ +from __future__ import absolute_import import sys, codecs diff --git a/components/classifier/vectors.py b/components/classifier/vectors.py index 64e32ab..2b6c858 100644 --- a/components/classifier/vectors.py +++ b/components/classifier/vectors.py @@ -21,6 +21,7 @@ """ +from __future__ import absolute_import import os, codecs, textwrap from components.common_modules.tree import create_tarsqi_tree diff --git a/components/classifier/wrapper.py b/components/classifier/wrapper.py index 89c4e5d..9a28aa3 100644 --- a/components/classifier/wrapper.py +++ b/components/classifier/wrapper.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import import os from library.tarsqi_constants import CLASSIFIER diff --git a/components/common_modules/chunks.py b/components/common_modules/chunks.py index e744806..a2f6b6a 100644 --- a/components/common_modules/chunks.py +++ b/components/common_modules/chunks.py @@ -7,6 +7,7 @@ """ +from __future__ import absolute_import import types from xml.sax.saxutils import quoteattr diff --git a/components/common_modules/component.py b/components/common_modules/component.py index 6bf3615..916acf7 100644 --- a/components/common_modules/component.py +++ b/components/common_modules/component.py @@ -5,6 +5,7 @@ """ +from __future__ import absolute_import from utilities import logger diff --git a/components/common_modules/constituent.py b/components/common_modules/constituent.py index 3733b61..d5f12d3 100644 --- a/components/common_modules/constituent.py +++ b/components/common_modules/constituent.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from types import ListType, TupleType from pprint import pprint diff --git a/components/common_modules/sentence.py b/components/common_modules/sentence.py index 76bf162..b4b3630 100644 --- a/components/common_modules/sentence.py +++ b/components/common_modules/sentence.py @@ -1,5 +1,6 @@ """Contains functionality specific to sentences in a tree.""" +from __future__ import absolute_import from utilities import logger from components.common_modules.constituent import Constituent diff --git a/components/common_modules/tags.py b/components/common_modules/tags.py index 61989a7..60bac62 100644 --- a/components/common_modules/tags.py +++ b/components/common_modules/tags.py @@ -5,10 +5,11 @@ """ +from __future__ import absolute_import from library.main import LIBRARY from components.common_modules.constituent import Constituent from utilities import logger -from utils import get_tokens +from .utils import get_tokens EVENT = LIBRARY.timeml.EVENT diff --git a/components/common_modules/tokens.py b/components/common_modules/tokens.py index ed363d3..c7c6be5 100644 --- a/components/common_modules/tokens.py +++ b/components/common_modules/tokens.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import from library import forms from library.main import LIBRARY from components.evita.event import Event diff --git a/components/common_modules/tree.py b/components/common_modules/tree.py index 5c57f7d..6614cd5 100644 --- a/components/common_modules/tree.py +++ b/components/common_modules/tree.py @@ -1,5 +1,6 @@ """Contains the TarsqiTree class.""" +from __future__ import absolute_import import sys import re from xml.sax.saxutils import escape, quoteattr diff --git a/components/common_modules/utils.py b/components/common_modules/utils.py index 803150e..0dac4cf 100644 --- a/components/common_modules/utils.py +++ b/components/common_modules/utils.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import import types from library import forms diff --git a/components/evita/bayes.py b/components/evita/bayes.py index 4c4b02a..6039afc 100644 --- a/components/evita/bayes.py +++ b/components/evita/bayes.py @@ -6,6 +6,7 @@ from __future__ import division +from __future__ import absolute_import from library import forms from utilities.file import open_pickle_file from utilities import logger diff --git a/components/evita/event.py b/components/evita/event.py index cf2541d..cb1ff5b 100644 --- a/components/evita/event.py +++ b/components/evita/event.py @@ -5,6 +5,7 @@ """ +from __future__ import absolute_import from library.main import LIBRARY from library.tarsqi_constants import EVITA diff --git a/components/evita/features.py b/components/evita/features.py index d70cbeb..81774c2 100644 --- a/components/evita/features.py +++ b/components/evita/features.py @@ -9,6 +9,7 @@ # TODO: check whether these are used by Slinket, if so move it to common_modules +from __future__ import absolute_import from types import ListType, InstanceType from pprint import pprint diff --git a/components/evita/main.py b/components/evita/main.py index 7a1c877..81c1cff 100644 --- a/components/evita/main.py +++ b/components/evita/main.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import from components.common_modules.utils import get_words_as_string from components.common_modules.component import TarsqiComponent from components.common_modules.tree import create_tarsqi_tree diff --git a/components/evita/wordnet.py b/components/evita/wordnet.py index 821f21a..faeb6de 100644 --- a/components/evita/wordnet.py +++ b/components/evita/wordnet.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import from library import forms import utilities.binsearch as binsearch import utilities.logger as logger diff --git a/components/evita/wrapper.py b/components/evita/wrapper.py index 5536a1b..4acb52a 100644 --- a/components/evita/wrapper.py +++ b/components/evita/wrapper.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import from library.tarsqi_constants import EVITA from library.main import LIBRARY from components.evita.main import Evita diff --git a/components/gutime/wrapper.py b/components/gutime/wrapper.py index a8816a7..aa0e827 100644 --- a/components/gutime/wrapper.py +++ b/components/gutime/wrapper.py @@ -25,6 +25,7 @@ """ +from __future__ import absolute_import import os, sys, subprocess, codecs, StringIO from xml.dom.minidom import parse, parseString diff --git a/components/merging/sputlink/graph.py b/components/merging/sputlink/graph.py index a43e41e..c5f5b26 100644 --- a/components/merging/sputlink/graph.py +++ b/components/merging/sputlink/graph.py @@ -10,12 +10,13 @@ # to be sufficient for our current purposes. -from objects import Node, Edge, Constraint -from utils import intersect_relations -from utils import compare_id -from utils import html_graph_prefix -from mappings import invert_interval_relation -from mappings import abbreviate_convex_relation +from __future__ import absolute_import +from .objects import Node, Edge, Constraint +from .utils import intersect_relations +from .utils import compare_id +from .utils import html_graph_prefix +from .mappings import invert_interval_relation +from .mappings import abbreviate_convex_relation from utilities import logger from library.main import LIBRARY diff --git a/components/merging/sputlink/main.py b/components/merging/sputlink/main.py index 9ac4bd6..3ba6a03 100644 --- a/components/merging/sputlink/main.py +++ b/components/merging/sputlink/main.py @@ -1,11 +1,12 @@ +from __future__ import absolute_import import os -from graph import Graph -from objects import Constraint -from mappings import translate_timeml_relation, invert_interval_relation -from utils import CompositionTable -from utils import html_graph_prefix +from .graph import Graph +from .objects import Constraint +from .mappings import translate_timeml_relation, invert_interval_relation +from .utils import CompositionTable +from .utils import html_graph_prefix from library.main import LIBRARY DEBUG = False diff --git a/components/merging/sputlink/objects.py b/components/merging/sputlink/objects.py index b1610b4..89320a6 100644 --- a/components/merging/sputlink/objects.py +++ b/components/merging/sputlink/objects.py @@ -1,6 +1,7 @@ -from utils import intersect_lists -from utils import intersect_relations -from utils import compare_id +from __future__ import absolute_import +from .utils import intersect_lists +from .utils import intersect_relations +from .utils import compare_id from library.main import LIBRARY TIMEX = LIBRARY.timeml.TIMEX diff --git a/components/merging/sputlink/rules/closure.py b/components/merging/sputlink/rules/closure.py index 2d7b219..d0a50bc 100644 --- a/components/merging/sputlink/rules/closure.py +++ b/components/merging/sputlink/rules/closure.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import from objects import TemporalObject, Link, PLink diff --git a/components/merging/sputlink/rules/generateRules.py b/components/merging/sputlink/rules/generateRules.py index c5cbca9..513910f 100644 --- a/components/merging/sputlink/rules/generateRules.py +++ b/components/merging/sputlink/rules/generateRules.py @@ -6,6 +6,7 @@ """ +from __future__ import absolute_import import sys from objects import \ diff --git a/components/merging/wrapper.py b/components/merging/wrapper.py index d65685c..0a80e00 100644 --- a/components/merging/wrapper.py +++ b/components/merging/wrapper.py @@ -16,6 +16,7 @@ """ +from __future__ import absolute_import import os from library.tarsqi_constants import LINK_MERGER, S2T, BLINKER, CLASSIFIER diff --git a/components/preprocessing/chunker.py b/components/preprocessing/chunker.py index dd07e55..a71972e 100644 --- a/components/preprocessing/chunker.py +++ b/components/preprocessing/chunker.py @@ -24,6 +24,7 @@ # For more details see https://github.com/tarsqi/ttk/issues/63. +from __future__ import absolute_import from types import StringType from utilities import logger diff --git a/components/preprocessing/tokenizer.py b/components/preprocessing/tokenizer.py index 3fcc956..7da8085 100644 --- a/components/preprocessing/tokenizer.py +++ b/components/preprocessing/tokenizer.py @@ -29,13 +29,14 @@ """ +from __future__ import absolute_import import re from StringIO import StringIO from xml.sax.saxutils import escape -from abbreviation import dict_abbrevs -from abbreviation import dict_end_abbrevs -from abbreviation import dict_initial_tokens +from .abbreviation import dict_abbrevs +from .abbreviation import dict_end_abbrevs +from .abbreviation import dict_initial_tokens abbrev_pattern = re.compile(r'^([A-Z]\.)+$') diff --git a/components/preprocessing/wrapper.py b/components/preprocessing/wrapper.py index e2078ce..8313b48 100644 --- a/components/preprocessing/wrapper.py +++ b/components/preprocessing/wrapper.py @@ -14,6 +14,7 @@ # about 40% so we want to keep the PreprocessorWrapper around. +from __future__ import absolute_import import os, sys, threading from time import time from subprocess import PIPE, Popen diff --git a/components/s2t/__init__.py b/components/s2t/__init__.py index 8ee9bae..02c5215 100644 --- a/components/s2t/__init__.py +++ b/components/s2t/__init__.py @@ -1 +1,2 @@ -import main +from __future__ import absolute_import +from . import main diff --git a/components/s2t/main.py b/components/s2t/main.py index 697d43b..14390ec 100644 --- a/components/s2t/main.py +++ b/components/s2t/main.py @@ -7,6 +7,7 @@ """ +from __future__ import absolute_import from utilities import logger from components.common_modules.component import TarsqiComponent from library.tarsqi_constants import S2T diff --git a/components/s2t/wrapper.py b/components/s2t/wrapper.py index c8b9eac..f3fb3ff 100644 --- a/components/s2t/wrapper.py +++ b/components/s2t/wrapper.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import from library.tarsqi_constants import S2T from components.s2t.main import Slink2Tlink from components.common_modules import tree diff --git a/components/simpletime/main.py b/components/simpletime/main.py index c90a8b7..bd81091 100644 --- a/components/simpletime/main.py +++ b/components/simpletime/main.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import os, sys diff --git a/components/slinket/main.py b/components/slinket/main.py index ee85a65..0f074b5 100644 --- a/components/slinket/main.py +++ b/components/slinket/main.py @@ -6,6 +6,7 @@ """ +from __future__ import absolute_import from components.common_modules.component import TarsqiComponent from components.common_modules.utils import get_events, get_words_as_string from library.slinket.main import SLINKET_DICTS diff --git a/components/slinket/wrapper.py b/components/slinket/wrapper.py index 3492d3c..e8870a8 100644 --- a/components/slinket/wrapper.py +++ b/components/slinket/wrapper.py @@ -4,6 +4,7 @@ """ +from __future__ import absolute_import from library.tarsqi_constants import SLINKET from components.slinket.main import Slinket from components.common_modules import tree diff --git a/deprecated/demo/display.py b/deprecated/demo/display.py index e38ab65..d5a7388 100644 --- a/deprecated/demo/display.py +++ b/deprecated/demo/display.py @@ -23,6 +23,7 @@ """ +from __future__ import absolute_import import os from docmodel.xml_parser import Parser, create_dct_element from library.tarsqi_constants import BLINKER, SLINKET, S2T, CLASSIFIER diff --git a/deprecated/get_lexes.py b/deprecated/get_lexes.py index fa1c707..9d3e8dd 100644 --- a/deprecated/get_lexes.py +++ b/deprecated/get_lexes.py @@ -15,6 +15,7 @@ """ +from __future__ import absolute_import import sys, os from xml.dom.minidom import parse diff --git a/deprecated/get_tags.py b/deprecated/get_tags.py index 5ba223e..01a7af3 100644 --- a/deprecated/get_tags.py +++ b/deprecated/get_tags.py @@ -8,6 +8,7 @@ """ +from __future__ import absolute_import import os, glob from xml.dom import minidom diff --git a/deprecated/gui.py b/deprecated/gui.py index 708f3dd..cef8f09 100644 --- a/deprecated/gui.py +++ b/deprecated/gui.py @@ -6,6 +6,7 @@ """ +from __future__ import absolute_import import os, sys, popen2, time, re import wx @@ -14,7 +15,7 @@ from xml.sax.saxutils import escape from tarsqi import Tarsqi -from demo.display import HtmlGenerator +from .demo.display import HtmlGenerator from docmodel.model import DocumentModel from docmodel.xml_parser import Parser from library.tarsqi_constants import PREPROCESSOR, GUTIME, EVITA, SLINKET, S2T diff --git a/deprecated/sputlink/rule_creation/closure.py b/deprecated/sputlink/rule_creation/closure.py index 9e46922..187883c 100755 --- a/deprecated/sputlink/rule_creation/closure.py +++ b/deprecated/sputlink/rule_creation/closure.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import from objects import TemporalObject, Link, PLink import string diff --git a/deprecated/sputlink/rule_creation/generateRules.py b/deprecated/sputlink/rule_creation/generateRules.py index 50d4835..aae620e 100755 --- a/deprecated/sputlink/rule_creation/generateRules.py +++ b/deprecated/sputlink/rule_creation/generateRules.py @@ -4,6 +4,7 @@ basic relations are used instead of TimeML relations. Also produces an extra short output for consumption of Perl scripts. """ +from __future__ import absolute_import import sys import string diff --git a/deprecated/xml_parser.py b/deprecated/xml_parser.py index 07aa5b6..8890a94 100644 --- a/deprecated/xml_parser.py +++ b/deprecated/xml_parser.py @@ -13,6 +13,7 @@ """ +from __future__ import absolute_import import sys from types import UnicodeType, IntType, StringType, NoneType import xml.parsers.expat diff --git a/docmodel/docstructure_parser.py b/docmodel/docstructure_parser.py index 34b0672..e2586e2 100644 --- a/docmodel/docstructure_parser.py +++ b/docmodel/docstructure_parser.py @@ -16,6 +16,7 @@ """ +from __future__ import absolute_import from library.tarsqi_constants import DOCSTRUCTURE diff --git a/docmodel/document.py b/docmodel/document.py index 7795616..9780beb 100644 --- a/docmodel/document.py +++ b/docmodel/document.py @@ -5,6 +5,7 @@ """ +from __future__ import absolute_import import os, sys, codecs, StringIO, itertools, time from xml.sax.saxutils import escape, quoteattr from subprocess import Popen, PIPE diff --git a/docmodel/main.py b/docmodel/main.py index b7e82d7..9d1149a 100644 --- a/docmodel/main.py +++ b/docmodel/main.py @@ -9,6 +9,7 @@ """ +from __future__ import absolute_import import os, xml from docmodel.source_parser import SourceParserXML, SourceParserText diff --git a/docmodel/metadata_parser.py b/docmodel/metadata_parser.py index 3d8b43c..6b24476 100644 --- a/docmodel/metadata_parser.py +++ b/docmodel/metadata_parser.py @@ -9,6 +9,7 @@ """ +from __future__ import absolute_import import re, time, os, sqlite3 import utilities.logger as logger diff --git a/docmodel/source_parser.py b/docmodel/source_parser.py index bd85292..cf0341a 100644 --- a/docmodel/source_parser.py +++ b/docmodel/source_parser.py @@ -36,6 +36,7 @@ """ +from __future__ import absolute_import import codecs import xml.parsers.expat from xml.dom import minidom diff --git a/docs/notes/python3.md b/docs/notes/python3.md index 8dd5442..f3c5c4f 100644 --- a/docs/notes/python3.md +++ b/docs/notes/python3.md @@ -1,9 +1,11 @@ # Porting to Python 3 +December 2020. + This document has notes on the effort to get a Python 3 version of TTK. Originally, the goal was to get a few steps along the way without any impact to TTK users, that is, no extra installations, not even something as simple as `pip install future` or `pip install builtins`. The goal was also to eventually change the code so that it supports both Python 2.7 and Python 3.5 and up. -This was all dropped. The goal is to get a Python3 version in TTK version 3.0.0. Period. Probably version 3.6. This might require extra installation, fine. And version 2.7 will not be supported anymore apart from now on expcept perhaps for requested bug fixes on version 2.2.0. +This was all dropped. The goal is to get a Python3 version in TTK version 3.0.0. Period. Probably version 3.6. This might require extra installation, fine. And version 2.7 will not be supported anymore from now on except perhaps for requested bug fixes on version 2.2.0. Having said that, the first steps of the process are all steps where the resulting code will still run on Python 2.7 @@ -13,20 +15,21 @@ This was all dropped. The goal is to get a Python3 version in TTK version 3.0.0. For now: - Started branch `79-python3`. - - Following the steps in [https://portingguide.readthedocs.io/en/latest/process.html](https://portingguide.readthedocs.io/en/latest/process.html) - After each step, review the changes, do not yet make any manual edits - After one or more steps, run the tests and if they pass put all changes in the git staging area. - Try to isolate automatic amd manual changes in separate commits. The only exception is that this file may be updated in a commit alongside automatic changes. - - Also looking at [http://python3porting.com/preparing.html](http://python3porting.com/preparing.html) to do those preparatory changes that allow you to still run under Python2 (division, strings, new classes, etcetera). There obviously is major overlap between this one and the previous one. - -- Using +- Commands that give information: - `python-modernize` to give hints on the idioms to change. - probably using individual fixers - `pylint --py3k` for more hints on Python3 compatibility. + - `python -3` when running tarsqi.py and the testing and regression scripts. +- Use `2to3` [http://python3porting.com/2to3.html](http://python3porting.com/2to3.html) for the final mechanichal steps, many of them probably already done in the previous steps. Will probably also want to get rid of all the future imports. Look at [https://adamj.eu/tech/2019/03/13/dropping-python-2-support/](https://adamj.eu/tech/2019/03/13/dropping-python-2-support/) for some notes on dropping Python 2 support. +- At the very end, test this on a new clone. There could be issues like updating libraries that have slipped through the cracks. + In the following, each section corresponds to one or more commits on the `79-python3` branch. @@ -116,13 +119,11 @@ Based on [http://python3porting.com/preparing.html](http://python3porting.com/pr Division of integers -See commit [047d9c28](https://github.com/tarsqi/ttk/commit/047d9c2850b5589e05641e182f69704f8787bb09). - -Using // when we really want to have integers as the result, using / in other cases. Sometimes using `from __future__ import division` and removing explicit conversions into floats. +Using // when we really want to have integers as the result, using / in other cases. Sometimes using `from __future__ import division` and removing explicit conversions into floats. See commit [047d9c28](https://github.com/tarsqi/ttk/commit/047d9c2850b5589e05641e182f69704f8787bb09). Using new style classes -Doing this all manually, but used the following code to find the classes. +Doing this all manually, but used the following code to find the classes. ```python import os @@ -150,7 +151,7 @@ if __name__ == '__main__': check_classes(os.path.join(root, name)) ``` -Luckily there was no mulitple inheritance anywhere so I did not need to worry about the MRO. The only thing that may need some time is to figure out how to do the FSA code since allegedly its error handling depends on old style classes. Well, it doesn't, it is just that there was a part in the code that relied on things like Constituent being of type types.InstanceType: +TTK does not use mulitple inheritance so I did not need to worry about the MRO. The only thing that needed some time was to figure out how to do the FSA code since allegedly its error handling depends on old style classes. Well, it doesn't, it is just that there was a part in the code that relied on things like *Constituent* being of type *types.InstanceType*: ```python if (type(input) is InstanceType and @@ -167,9 +168,9 @@ if getattr(input, '__class__').__name__ in [ 'Token', 'AdjectiveToken', 'EventTag', 'TimexTag']: ``` -There are issues however with raising errors in `FSA.py`, they will need to be dealt with in a later step. +WIth this fixed no errors were raised. There are issues however with raising errors in `FSA.py`, they will need to be dealt with in a later step. -More seriously, when `FSA.FSA` and `FSA.Sequence` are made new style classes, code starts crashing on loading pickle files. It turned out that we needed to re compile Evita and Slinket patterns: +More seriously, when `FSA.FSA` and `FSA.Sequence` are turned into new style classes, code starts crashing on loading pickle files. I needed to re compile Evita and Slinket patterns: ``` $ cd library/evita @@ -183,4 +184,37 @@ $ python2 create_dicts.py $ cp *.pickle dictionaries ``` -After this it all worked, but see the TODO comment in `create_dicts.py` which elaborates a bit on the ugly hack I used. \ No newline at end of file +After this it all worked, but see the TODO comment in `create_dicts.py` which elaborates a bit on the ugly hack I used. + +See commit [0ccd82d9](https://github.com/tarsqi/ttk/commit/0ccd82d9f11e72c75b7bcbb5e71044b87a818385). + +Absolute imports + +Changed made here are based on [https://portingguide.readthedocs.io/en/latest/imports.html](https://portingguide.readthedocs.io/en/latest/imports.html). + +``` +$ python-modernize -wnf libmodernize.fixes.fix_import . +``` + +This made many changes, importing from future and changing other imports. One weird change is that pretty much every line in `componets/blinker/compare.py ` is changed. Not sure what is going on there, but when getting the previous version and comparing it to the one after the patch, all I see is a difference in the line with the future import. + +There are no occurrences of "import *" in functions and no import cycles that I am aware of. + + + +``` +$ python regression.py --evita +Traceback (most recent call last): + File "regression.py", line 46, in + from . import path +ValueError: Attempted relative import in non-package +``` + +No idea what is going on here because there is an init file and the directory should be considered a package. Okay, got it, see [stackoverflow.com/questions/11536764](https://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-init-py) and [PEP 338](https://www.python.org/dev/peps/pep-0338/). When you run the regressions script it is the main script and then `__name__` is set to `__main__` and all the imports go awry. + +Could not figure out exactly how to use `__package__` to deal with this, but you can do the following from the parent directory: + +``` +$ python -m testing.regression --evita +``` + diff --git a/library/blinker/blinker_rule_loader.py b/library/blinker/blinker_rule_loader.py index db67c38..3e66883 100644 --- a/library/blinker/blinker_rule_loader.py +++ b/library/blinker/blinker_rule_loader.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import os import re diff --git a/library/classifier/create_model.py b/library/classifier/create_model.py index 0611f95..a086dc3 100644 --- a/library/classifier/create_model.py +++ b/library/classifier/create_model.py @@ -40,8 +40,9 @@ """ +from __future__ import absolute_import import os, sys, glob, getopt -import path +from . import path from utilities import mallet diff --git a/library/classifier/create_vectors.py b/library/classifier/create_vectors.py index 4fdd410..a3fbec2 100644 --- a/library/classifier/create_vectors.py +++ b/library/classifier/create_vectors.py @@ -93,10 +93,11 @@ """ +from __future__ import absolute_import import os, sys -import path -import root +from . import path +from . import root from tarsqi import Options from docmodel.document import TarsqiDocument diff --git a/library/classifier/path.py b/library/classifier/path.py index f5c1fe5..8ed3328 100644 --- a/library/classifier/path.py +++ b/library/classifier/path.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import sys sys.path.append('../..') diff --git a/library/classifier/root.py b/library/classifier/root.py index 216e3c4..2d2ace4 100644 --- a/library/classifier/root.py +++ b/library/classifier/root.py @@ -10,6 +10,7 @@ # TODO: would like to find a way to redo what components.__init__ does because # it is at the source of these troubles. +from __future__ import absolute_import import os os.environ['TTK_ROOT'] = '../..' diff --git a/library/evita/build_event_nominals1.py b/library/evita/build_event_nominals1.py index 725d507..e0f5e12 100644 --- a/library/evita/build_event_nominals1.py +++ b/library/evita/build_event_nominals1.py @@ -17,6 +17,7 @@ """ +from __future__ import absolute_import import os import sys import forms diff --git a/library/evita/build_event_nominals2.py b/library/evita/build_event_nominals2.py index 69f7c79..ef3c49a 100644 --- a/library/evita/build_event_nominals2.py +++ b/library/evita/build_event_nominals2.py @@ -20,6 +20,7 @@ """ +from __future__ import absolute_import import os import sys import anydbm diff --git a/library/evita/compile_patterns.py b/library/evita/compile_patterns.py index 49a8b33..dd5449e 100644 --- a/library/evita/compile_patterns.py +++ b/library/evita/compile_patterns.py @@ -21,6 +21,7 @@ """ +from __future__ import absolute_import import os, sys, cPickle sys.path.append('../..') diff --git a/library/evita/multi_chunk_patterns.py b/library/evita/multi_chunk_patterns.py index 78942e5..c238591 100644 --- a/library/evita/multi_chunk_patterns.py +++ b/library/evita/multi_chunk_patterns.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import from library import forms ######################## diff --git a/library/evita/nominal_trainer.py b/library/evita/nominal_trainer.py index a8cb1d6..0d601b7 100644 --- a/library/evita/nominal_trainer.py +++ b/library/evita/nominal_trainer.py @@ -17,6 +17,7 @@ from __future__ import division +from __future__ import absolute_import import os import sys import re diff --git a/library/evita/patterns/feature_rules.py b/library/evita/patterns/feature_rules.py index 2500923..59b203f 100644 --- a/library/evita/patterns/feature_rules.py +++ b/library/evita/patterns/feature_rules.py @@ -43,6 +43,7 @@ """ +from __future__ import absolute_import from library.forms import * diff --git a/library/forms.py b/library/forms.py index 24f735b..280536c 100644 --- a/library/forms.py +++ b/library/forms.py @@ -17,6 +17,7 @@ """ +from __future__ import absolute_import import os, sys, re, shelve, pickle TTK_ROOT = os.environ['TTK_ROOT'] diff --git a/library/main.py b/library/main.py index 9808eb6..7955090 100644 --- a/library/main.py +++ b/library/main.py @@ -10,6 +10,7 @@ """ +from __future__ import absolute_import import os TTK_ROOT = os.environ['TTK_ROOT'] diff --git a/library/patterns.py b/library/patterns.py index 89aaa1c..ea0f37e 100644 --- a/library/patterns.py +++ b/library/patterns.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import os import cPickle diff --git a/library/s2t/s2t_rule_loader.py b/library/s2t/s2t_rule_loader.py index 8658b98..e325a1b 100644 --- a/library/s2t/s2t_rule_loader.py +++ b/library/s2t/s2t_rule_loader.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import os import re diff --git a/library/slinket/alinkPredicates.py b/library/slinket/alinkPredicates.py index 5f8d1ae..c0c2694 100644 --- a/library/slinket/alinkPredicates.py +++ b/library/slinket/alinkPredicates.py @@ -29,7 +29,8 @@ #from library.timeMLspec import INIT, CULM, TERM, CONT, REINIT -from timeMLspec import INIT, CULM, TERM, CONT, REINIT +from __future__ import absolute_import +from .timeMLspec import INIT, CULM, TERM, CONT, REINIT # FORWARD patterns: @@ -39,26 +40,26 @@ #from slinketPatterns import THAT_clause_SIMPLE, THAT_clause_NOT_tensed #from slinketPatterns import THAT_clause_if, IND_INTERROG -from slinketPatterns import TO_clause1, TO_clause3, TO_clause5 -from slinketPatterns import TO_clause7, IND_INTERROG_nonfin +from .slinketPatterns import TO_clause1, TO_clause3, TO_clause5 +from .slinketPatterns import TO_clause7, IND_INTERROG_nonfin # TO_clause1, TO_clause2, TO_clause3, TO_clause4, TO_clause5 # TO_clause6, TO_clause7, IND_INTERROG_nonfin -from slinketPatterns import ING_clause +from .slinketPatterns import ING_clause # ABOUT_ING_clause, AGAINST_ING_clause, AT_ING_clause, FOR_ING_clause, # FROM_ING_clause, IN_ING_clause, OF_ING_clause, WITH_ING_clause -from slinketPatterns import ABOUT_NPev, AT_NPev, FOR_NPev, FROM_NPev, IN_NPev -from slinketPatterns import OF_NPev, ON_NPev, OVER_NPev, TO_NPev, WITH_NPev, PP_ABOUT +from .slinketPatterns import ABOUT_NPev, AT_NPev, FOR_NPev, FROM_NPev, IN_NPev +from .slinketPatterns import OF_NPev, ON_NPev, OVER_NPev, TO_NPev, WITH_NPev, PP_ABOUT -from slinketPatterns import NP_ev1, NP_ev2, NP_evAsSubj1, NP_evAsSubj2 +from .slinketPatterns import NP_ev1, NP_ev2, NP_evAsSubj1, NP_evAsSubj2 -from slinketPatterns import OBJCOMPL_pastPart, OBJCOMPL_adj +from .slinketPatterns import OBJCOMPL_pastPart, OBJCOMPL_adj # BACKWARD patterns: -from slinketPatterns import Passive1, RelClauseExplic, RelClauseExplicPerfect -from slinketPatterns import RelClauseRestric, RelClauseRestricPerfect +from .slinketPatterns import Passive1, RelClauseExplic, RelClauseExplicPerfect +from .slinketPatterns import RelClauseRestric, RelClauseRestricPerfect print "Creating Alink predicate dictionaries" diff --git a/library/slinket/create_dicts.py b/library/slinket/create_dicts.py index 7631e34..620b385 100644 --- a/library/slinket/create_dicts.py +++ b/library/slinket/create_dicts.py @@ -18,6 +18,7 @@ +from __future__ import absolute_import import os, sys, cPickle sys.path.append('../..') @@ -27,8 +28,8 @@ # variable is not used so we hand in a dummy value os.environ['TTK_ROOT'] = 'DUMMY' -import slinkPredicates -import alinkPredicates +from . import slinkPredicates +from . import alinkPredicates print "Pickling dictionaries..." diff --git a/library/slinket/main.py b/library/slinket/main.py index 5c718c0..ed4d516 100644 --- a/library/slinket/main.py +++ b/library/slinket/main.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import os import cPickle diff --git a/library/slinket/slinkPredicates.py b/library/slinket/slinkPredicates.py index 8e12129..2db0575 100644 --- a/library/slinket/slinkPredicates.py +++ b/library/slinket/slinkPredicates.py @@ -28,24 +28,25 @@ """ #from library.timeMLspec import * -from timeMLspec import * +from __future__ import absolute_import +from .timeMLspec import * # FORWARD patterns: -from slinketPatterns import THAT_clause_that, THAT_clause_NOT_that, THAT_clausePAST_that, THAT_clausePERFECTIVE_NEG_that, THAT_clausePAST_NOT_that, THAT_clausePERFECTIVE_NEG_NOT_that, THAT_clause_that_QUOTES, THAT_clause_NOT_that_QUOTES, THAT_clause_N_that_N_report, THAT_clause_that_NOT_report, THAT_clause_SIMPLE, THAT_clause_NOT_tensed, THAT_clause_if -from slinketPatterns import IND_INTERROG, OF_IND_INTERROG -from slinketPatterns import TO_clause1, TO_clause1_PERFECTIVE, TO_clause3, TO_clause3_PERFECTIVE, TO_clause5, TO_clause5_PERFECTIVE, TO_clause7 #TO_clause1, TO_clause2, TO_clause3, TO_clause4, TO_clause5, TO_clause6, TO_clause7 -from slinketPatterns import IND_INTERROG_nonfin, OF_IND_INTERROG_nonfin -from slinketPatterns import ING_clause, ABOUT_ING_clause, AGAINST_ING_clause, AT_ING_clause, FOR_ING_clause, FROM_ING_clause, IN_ING_clause, OF_ING_clause, ON_ING_clause, TO_ING_clause, WITH_ING_clause -from slinketPatterns import ABOUT_NPev, AT_NPev, FOR_NPev, FROM_NPev, IN_NPev, OF_NPev, ON_NPev, OVER_NPev, TO_NPev, WITH_NPev, WITHOUT_NPev, PP_ABOUT -from slinketPatterns import NP_ev1, NP_ev2 -from slinketPatterns import OBJCOMPL_pastPart, OBJCOMPL_adj +from .slinketPatterns import THAT_clause_that, THAT_clause_NOT_that, THAT_clausePAST_that, THAT_clausePERFECTIVE_NEG_that, THAT_clausePAST_NOT_that, THAT_clausePERFECTIVE_NEG_NOT_that, THAT_clause_that_QUOTES, THAT_clause_NOT_that_QUOTES, THAT_clause_N_that_N_report, THAT_clause_that_NOT_report, THAT_clause_SIMPLE, THAT_clause_NOT_tensed, THAT_clause_if +from .slinketPatterns import IND_INTERROG, OF_IND_INTERROG +from .slinketPatterns import TO_clause1, TO_clause1_PERFECTIVE, TO_clause3, TO_clause3_PERFECTIVE, TO_clause5, TO_clause5_PERFECTIVE, TO_clause7 #TO_clause1, TO_clause2, TO_clause3, TO_clause4, TO_clause5, TO_clause6, TO_clause7 +from .slinketPatterns import IND_INTERROG_nonfin, OF_IND_INTERROG_nonfin +from .slinketPatterns import ING_clause, ABOUT_ING_clause, AGAINST_ING_clause, AT_ING_clause, FOR_ING_clause, FROM_ING_clause, IN_ING_clause, OF_ING_clause, ON_ING_clause, TO_ING_clause, WITH_ING_clause +from .slinketPatterns import ABOUT_NPev, AT_NPev, FOR_NPev, FROM_NPev, IN_NPev, OF_NPev, ON_NPev, OVER_NPev, TO_NPev, WITH_NPev, WITHOUT_NPev, PP_ABOUT +from .slinketPatterns import NP_ev1, NP_ev2 +from .slinketPatterns import OBJCOMPL_pastPart, OBJCOMPL_adj # BACKWARD patterns: -from slinketPatterns import Passive1, RelClauseExplic, RelClauseExplicPerfect -from slinketPatterns import NP_evAsSubj1, NP_evAsSubj2, NP_evAsSubj3 +from .slinketPatterns import Passive1, RelClauseExplic, RelClauseExplicPerfect +from .slinketPatterns import NP_evAsSubj1, NP_evAsSubj2, NP_evAsSubj3 # REPORTING patterns: -from slinketPatterns import MAINsentence +from .slinketPatterns import MAINsentence print "Creating Slink predicate dictionaries" diff --git a/library/slinket/slinketPatterns.py b/library/slinket/slinketPatterns.py index a9a3ee5..e04ab7d 100644 --- a/library/slinket/slinketPatterns.py +++ b/library/slinket/slinketPatterns.py @@ -11,10 +11,11 @@ """ +from __future__ import absolute_import from library import forms #from library.timeMLspec import * -from timeMLspec import * +from .timeMLspec import * from utilities.FSA import compileOP diff --git a/library/slinket/timeMLspec.py b/library/slinket/timeMLspec.py index c87c41c..353c0d1 100644 --- a/library/slinket/timeMLspec.py +++ b/library/slinket/timeMLspec.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import re """============================================== diff --git a/root.py b/root.py index 2e2367e..a1e9108 100644 --- a/root.py +++ b/root.py @@ -11,6 +11,7 @@ # TODO: I do not like this, is there a better way to deal with this? +from __future__ import absolute_import import os scriptPath = os.path.abspath(__file__) diff --git a/tarsqi.py b/tarsqi.py index da1ca2b..8713f3e 100644 --- a/tarsqi.py +++ b/tarsqi.py @@ -105,6 +105,7 @@ """ +from __future__ import absolute_import import sys, os, time, getopt import root diff --git a/testing/create_slinket_cases.py b/testing/create_slinket_cases.py index 9922ce4..7426472 100644 --- a/testing/create_slinket_cases.py +++ b/testing/create_slinket_cases.py @@ -17,6 +17,7 @@ """ +from __future__ import absolute_import import os, sys, codecs from xml.dom.minidom import parse, parseString diff --git a/testing/path.py b/testing/path.py index edfbee2..42e77ca 100644 --- a/testing/path.py +++ b/testing/path.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import import os import sys diff --git a/testing/regression.py b/testing/regression.py index 7883fc9..83fd022 100644 --- a/testing/regression.py +++ b/testing/regression.py @@ -40,9 +40,10 @@ """ +from __future__ import absolute_import import os, sys, getopt, time, glob -import path +from . import path import tarsqi def load_cases(fname): diff --git a/testing/run_tests.py b/testing/run_tests.py index ce169da..3a326e3 100644 --- a/testing/run_tests.py +++ b/testing/run_tests.py @@ -48,19 +48,20 @@ """ +from __future__ import absolute_import import sys, getopt, traceback, types -import path +from . import path import tarsqi from components.merging.sputlink.main import ConstraintPropagator -from cases.test_cases_timex import TIMEX_TESTS -from cases.test_cases_event import EVENT_TESTS -from cases.test_cases_slink import ALINK_TESTS -from cases.test_cases_slink import SLINK_TESTS -from cases.test_cases_tlink import S2T_TESTS -from cases.test_cases_tlink import BLINKER_TESTS -from cases.test_cases_sputlink import SPUTLINK_TESTS +from .cases.test_cases_timex import TIMEX_TESTS +from .cases.test_cases_event import EVENT_TESTS +from .cases.test_cases_slink import ALINK_TESTS +from .cases.test_cases_slink import SLINK_TESTS +from .cases.test_cases_tlink import S2T_TESTS +from .cases.test_cases_tlink import BLINKER_TESTS +from .cases.test_cases_sputlink import SPUTLINK_TESTS PASS = 'ok' diff --git a/testing/scripts/regression/evita/compare.py b/testing/scripts/regression/evita/compare.py index ee79d84..4dc06ea 100644 --- a/testing/scripts/regression/evita/compare.py +++ b/testing/scripts/regression/evita/compare.py @@ -34,6 +34,7 @@ """ +from __future__ import absolute_import import sys from xml.dom.minidom import parse diff --git a/testing/scripts/regression/slinket/compare.py b/testing/scripts/regression/slinket/compare.py index 66c4d9d..c3b3622 100644 --- a/testing/scripts/regression/slinket/compare.py +++ b/testing/scripts/regression/slinket/compare.py @@ -41,6 +41,7 @@ # TODO: and how about highlighting the events in the fragment +from __future__ import absolute_import import sys from xml.dom.minidom import parse diff --git a/utilities/FSA-org.py b/utilities/FSA-org.py index 4d9a958..041adb8 100644 --- a/utilities/FSA-org.py +++ b/utilities/FSA-org.py @@ -339,6 +339,7 @@ """ +from __future__ import absolute_import import string, os, tempfile from types import InstanceType, ListType, TupleType, IntType, LongType, DictType, StringType IntegerTypes = (IntType, LongType) diff --git a/utilities/FSA.py b/utilities/FSA.py index 278387d..43fd5f7 100644 --- a/utilities/FSA.py +++ b/utilities/FSA.py @@ -332,12 +332,13 @@ """ +from __future__ import absolute_import import string, os, tempfile from types import InstanceType, ListType, TupleType, IntType, LongType, DictType, StringType IntegerTypes = (IntType, LongType) #from utilities import logger -import logger +from . import logger try: import NumFSAUtils diff --git a/utilities/binsearch.py b/utilities/binsearch.py index 6e6d263..be09769 100644 --- a/utilities/binsearch.py +++ b/utilities/binsearch.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import import os from stat import ST_SIZE diff --git a/utilities/code-stats.py b/utilities/code-stats.py index 0439629..926cf72 100644 --- a/utilities/code-stats.py +++ b/utilities/code-stats.py @@ -40,6 +40,7 @@ """ +from __future__ import absolute_import import os, sys, subprocess command = ['git', 'log', '-n', '1', '--date=short', '--pretty=format:"%ad %H"'] diff --git a/utilities/file.py b/utilities/file.py index f566507..aac8a65 100644 --- a/utilities/file.py +++ b/utilities/file.py @@ -1,5 +1,6 @@ """Module containing simple file utilities.""" +from __future__ import absolute_import import os import pickle diff --git a/utilities/get_issues.py b/utilities/get_issues.py index 6667808..6c40113 100644 --- a/utilities/get_issues.py +++ b/utilities/get_issues.py @@ -24,6 +24,7 @@ """ +from __future__ import absolute_import import os, sys, json, pprint, re from HTMLParser import HTMLParser diff --git a/utilities/lif.py b/utilities/lif.py index 5dfed05..1e2853c 100644 --- a/utilities/lif.py +++ b/utilities/lif.py @@ -29,6 +29,7 @@ """ +from __future__ import absolute_import import sys import codecs import json diff --git a/utilities/logger.py b/utilities/logger.py index 59b1ab1..df14f8b 100644 --- a/utilities/logger.py +++ b/utilities/logger.py @@ -51,6 +51,7 @@ # send an object with a print operation instead +from __future__ import absolute_import import os import sys import inspect diff --git a/utilities/make_documentation.py b/utilities/make_documentation.py index f8fdbd7..f02882b 100644 --- a/utilities/make_documentation.py +++ b/utilities/make_documentation.py @@ -22,12 +22,13 @@ """ +from __future__ import absolute_import import os import sys import inspect from types import ClassType, FunctionType, MethodType, TypeType -from modules import MODULES +from .modules import MODULES # Set this to True if you want the sources for all functions written to files # that are linked to from the module page, this slows down the code quite a bit diff --git a/utilities/mallet.py b/utilities/mallet.py index 0cdace1..1b12d74 100644 --- a/utilities/mallet.py +++ b/utilities/mallet.py @@ -20,10 +20,11 @@ """ +from __future__ import absolute_import import os, sys from subprocess import Popen, PIPE -import logger +from . import logger DEBUG = False diff --git a/utilities/modules.py b/utilities/modules.py index 0c74dca..a3560bf 100644 --- a/utilities/modules.py +++ b/utilities/modules.py @@ -7,7 +7,8 @@ """ -import path +from __future__ import absolute_import +from . import path import tarsqi import components.blinker.compare diff --git a/utilities/path.py b/utilities/path.py index 49a1e37..620e01d 100644 --- a/utilities/path.py +++ b/utilities/path.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import sys sys.path.insert(0, '..') diff --git a/utilities/printing.py b/utilities/printing.py index f477d9f..f717eb7 100644 --- a/utilities/printing.py +++ b/utilities/printing.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import pprint diff --git a/utilities/seeProfile.py b/utilities/seeProfile.py index 31cf901..a77efec 100644 --- a/utilities/seeProfile.py +++ b/utilities/seeProfile.py @@ -18,6 +18,7 @@ """ +from __future__ import absolute_import import pstats import sys diff --git a/utilities/stemmer.py b/utilities/stemmer.py index 33245c9..9cecc61 100644 --- a/utilities/stemmer.py +++ b/utilities/stemmer.py @@ -8,8 +8,9 @@ """ +from __future__ import absolute_import from library.forms import STEM_EXCEPTIONS_FILE -from binsearch import binarySearchFile +from .binsearch import binarySearchFile class Stemmer(object): diff --git a/utilities/wntools.py b/utilities/wntools.py index 4487204..02a96ef 100644 --- a/utilities/wntools.py +++ b/utilities/wntools.py @@ -29,10 +29,11 @@ [('raise' in {verb: raise, lift, elevate, get up, bring up}, 'lower' in {verb: lower, take down, let down, get down, bring down})] """ +from __future__ import absolute_import __author__ = "Oliver Steele " __version__ = "2.0" -from wordnet import * +from .wordnet import * # # Domain utilities @@ -295,7 +296,7 @@ def morphy(form, pos='noun', collect=0): 'abacus' >>> morphy('hardrock', 'adv') """ - from wordnet import _normalizePOS, _dictionaryFor + from .wordnet import _normalizePOS, _dictionaryFor pos = _normalizePOS(pos) excfile = open(os.path.join(WNSEARCHDIR, {NOUN: 'noun', VERB: 'verb', ADJECTIVE: 'adj', ADVERB: 'adv'}[pos] + '.exc')) substitutions = MORPHOLOGICAL_SUBSTITUTIONS[pos] diff --git a/utilities/wordnet.py b/utilities/wordnet.py index ad1a235..97af01d 100644 --- a/utilities/wordnet.py +++ b/utilities/wordnet.py @@ -36,6 +36,7 @@ [{noun: Canis, genus Canis}, {noun: pack}] """ +from __future__ import absolute_import __author__ = "Oliver Steele " __version__ = "1.4" @@ -245,7 +246,7 @@ def getSense(offset, pos=self.pos, form=self.form): # Deprecated. Present for backwards compatability. def senses(self): - import wordnet + from . import wordnet #warningKey = 'SENSE_DEPRECATION_WARNING' #if not wordnet.has_key(warningKey): # print 'Word.senses() has been deprecated. Use Word.sense() instead.'