add query types resource parsing

This commit is contained in:
Daniel Tsvetkov 2020-05-11 20:29:41 +02:00
parent 287820464c
commit 6681f7dfd5
5 changed files with 60 additions and 19 deletions

View File

@ -46,3 +46,33 @@ in,6.9833333,171.6999969
ind,20.0,77.0 ind,20.0,77.0
indi,20.0,77.0 indi,20.0,77.0
local,37.6666667,-1.7 local,37.6666667,-1.7
+02:,49.453285449999996,3.606899003594057
it,33.6366667,42.8224983
G,32.3293809,-83.1137366
D,10.0,-67.166667
dub,-32.25,148.6166687
D,10.0,-67.166667
dubli,53.3330556,-6.248889
dublin,53.3330556,-6.248889
Du,25.0750095,55.18876088183319
dublin ,53.3330556,-6.248889
S,35.7724185,127.79654346305617
sof,42.6975135,23.3241463
S,35.7724185,127.79654346305617
sofia ,42.6975135,23.3241463
S,35.7724185,127.79654346305617
S,35.7724185,127.79654346305617
du,-32.25,148.6166687
D,10.0,-67.166667
S,35.7724185,127.79654346305617
S,35.7724185,127.79654346305617
S,35.7724185,127.79654346305617
S,35.7724185,127.79654346305617
D,10.0,-67.166667
D,10.0,-67.166667
new y,6.9833333,171.6999969
S,35.7724185,127.79654346305617
S,35.7724185,127.79654346305617
D,10.0,-67.166667
dubl,53.3330556,-6.248889
D,10.0,-67.166667

1 sofia 42.6975135 23.3241463
46 ind 20.0 77.0
47 indi 20.0 77.0
48 local 37.6666667 -1.7
49 +02: 49.453285449999996 3.606899003594057
50 it 33.6366667 42.8224983
51 G 32.3293809 -83.1137366
52 D 10.0 -67.166667
53 dub -32.25 148.6166687
54 D 10.0 -67.166667
55 dubli 53.3330556 -6.248889
56 dublin 53.3330556 -6.248889
57 Du 25.0750095 55.18876088183319
58 dublin 53.3330556 -6.248889
59 S 35.7724185 127.79654346305617
60 sof 42.6975135 23.3241463
61 S 35.7724185 127.79654346305617
62 sofia 42.6975135 23.3241463
63 S 35.7724185 127.79654346305617
64 S 35.7724185 127.79654346305617
65 du -32.25 148.6166687
66 D 10.0 -67.166667
67 S 35.7724185 127.79654346305617
68 S 35.7724185 127.79654346305617
69 S 35.7724185 127.79654346305617
70 S 35.7724185 127.79654346305617
71 D 10.0 -67.166667
72 D 10.0 -67.166667
73 new y 6.9833333 171.6999969
74 S 35.7724185 127.79654346305617
75 S 35.7724185 127.79654346305617
76 D 10.0 -67.166667
77 dubl 53.3330556 -6.248889
78 D 10.0 -67.166667

View File

@ -33,12 +33,14 @@ from word2number import w2n
from tww.common import logger from tww.common import logger
FUZZ_THRESHOLD = 70 FUZZ_THRESHOLD = 70
ISO_Z_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
ISO_FORMAT = '%Y-%m-%dT%H:%M:%S%z' ISO_FORMAT = '%Y-%m-%dT%H:%M:%S%z'
ISO_FORMAT_NO_TZ = '%Y-%m-%dT%H:%M:%S'
DEFAULT_FORMAT = '%Y-%m-%d %H:%M:%S%z' DEFAULT_FORMAT = '%Y-%m-%d %H:%M:%S%z'
basepath = os.path.dirname(os.path.abspath(__file__)) basepath = os.path.dirname(os.path.abspath(__file__))
SKIP_DATE_TOKENS = ['on', 'to', 'set', 'be'] SKIP_DATE_TOKENS = ['on', 'to', 'set', 'be', 'an', "me"]
def parse_to_iso(dt, dt_hint=None): def parse_to_iso(dt, dt_hint=None):
@ -52,7 +54,7 @@ def parse_to_iso(dt, dt_hint=None):
def find_dates_in(text): def find_dates_in(text):
rv = [] rv = []
dateparser_dates = search_dates(text, settings={"SKIP_TOKENS": SKIP_DATE_TOKENS}) dateparser_dates = search_dates(text)
if dateparser_dates: if dateparser_dates:
rv += list(filter(_dateparser_search_filter, dateparser_dates)) rv += list(filter(_dateparser_search_filter, dateparser_dates))
try: try:
@ -604,15 +606,17 @@ def get_local_now(tzaware: bool = True):
return datetime.utcnow() return datetime.utcnow()
def split_offset(offset):
if ':' in offset:
to_shh, to_mm = offset.split(':')
else:
to_shh, to_mm = offset[:-2], offset[-2:]
return int(to_shh), int(to_mm)
def dt_tz_translation(dt: datetime, to_tz_offset: str, from_tz_offset: str = "+00:00") -> datetime: def dt_tz_translation(dt: datetime, to_tz_offset: str, from_tz_offset: str = "+00:00") -> datetime:
if ':' in to_tz_offset: to_shh, to_mm = split_offset(to_tz_offset)
to_shh, to_mm = to_tz_offset.split(':') from_shh, from_mm = split_offset(from_tz_offset)
else:
to_shh, to_mm = to_tz_offset[:-2], to_tz_offset[-2:]
if ':' in from_tz_offset:
from_shh, from_mm = from_tz_offset.split(':')
else:
from_shh, from_mm = from_tz_offset[:-2], to_tz_offset[-2:]
tzinfo = tzinfo_from_offset(to_tz_offset)[0] tzinfo = tzinfo_from_offset(to_tz_offset)[0]
if dt.tzinfo: if dt.tzinfo:
return dt.astimezone(tzinfo) return dt.astimezone(tzinfo)

View File

@ -1,12 +1,12 @@
<input type="text" class="datetime" name="datetime" disabled <input type="text" class="datetime" name="datetime" disabled
value="{{ dst_dt.iso8601_full }}" style="width: 320px;"/> value="{{ dt.iso8601_full }}" style="width: 320px;"/>
</br></br> </br></br>
<input type="text" class="datetime__date_txt" name="datetime__date_txt" disabled <input type="text" class="datetime__date_txt" name="datetime__date_txt" disabled
value="{{ dst_dt.iso8601_date }}"/> value="{{ dt.iso8601_date }}"/>
<input type="text" class="datetime__time_txt" name="datetime__time_txt" disabled <input type="text" class="datetime__time_txt" name="datetime__time_txt" disabled
value="{{ dst_dt.iso8601_time }}"/> value="{{ dt.iso8601_time }}"/>
<input type="text" class="datetime__tz_offset" name="datetime__tz_offset" disabled <input type="text" class="datetime__tz_offset" name="datetime__tz_offset" disabled
value="{{ dst_dt.tz_offset }}" size="8"/> value="{{ dt.tz_offset }}" size="8"/>
<select class="datetime__tz_name" name="datetime__tz"> <select class="datetime__tz_name" name="datetime__tz">
<option disabled selected value> -- select timezone --</option> <option disabled selected value> -- select timezone --</option>
{% for tz in all_tz %} {% for tz in all_tz %}

View File

@ -9,7 +9,8 @@ from pygments import highlight, lexers, formatters
from scalpl import Cut from scalpl import Cut
from tww.localization import setlocale, resolve_locale from tww.localization import setlocale, resolve_locale
from tww.lib import ISO_FORMAT, time_to_emoji, time_ago, workday_diff, workhours_diff, td_remainders, td_totals, td_iso8601 from tww.lib import ISO_FORMAT, time_to_emoji, time_ago, workday_diff, workhours_diff, td_remainders, td_totals, \
td_iso8601, ISO_Z_FORMAT
from tww.lib import resolve_timezone, dateparser_parse_dt, get_utcnow, get_s_since_epoch, get_ms_since_epoch, \ from tww.lib import resolve_timezone, dateparser_parse_dt, get_utcnow, get_s_since_epoch, get_ms_since_epoch, \
dt_tz_translation, get_local_now, query_to_format_result dt_tz_translation, get_local_now, query_to_format_result
from tww.common import logger from tww.common import logger
@ -129,7 +130,7 @@ h_unix_s = 'dt->unix_s'
h_unix_ms = 'dt->unix_ms' h_unix_ms = 'dt->unix_ms'
h_tz_offset = 'tz->tz_offset' h_tz_offset = 'tz->tz_offset'
h_time_in = 'dt->hh:mm' h_time_in = 'dt->hh:mm'
h_translation = 'dst_dt->iso8601_full' h_translation = 'dt->iso8601_full'
h_default_dt = 'dt->iso8601_full' h_default_dt = 'dt->iso8601_full'
h_default_td = 'timedelta->diff->duration_iso8601' h_default_td = 'timedelta->diff->duration_iso8601'
@ -207,6 +208,8 @@ def dt_pretty(dt):
rv = {} rv = {}
global custom_locale global custom_locale
rv["iso8601_full"] = dt.strftime(ISO_FORMAT) rv["iso8601_full"] = dt.strftime(ISO_FORMAT)
rv["iso8601_utc"] = dt_tz_translation(dt, to_tz_offset='+00:00',
from_tz_offset=dt.strftime('%z')).strftime(ISO_Z_FORMAT)
rv["iso8601_date"] = dt.strftime('%Y-%m-%d') rv["iso8601_date"] = dt.strftime('%Y-%m-%d')
rv["iso8601_time"] = dt.strftime('%H:%M:%S') rv["iso8601_time"] = dt.strftime('%H:%M:%S')
rv["locale_dt"] = dt.strftime("%c") rv["locale_dt"] = dt.strftime("%c")
@ -247,12 +250,14 @@ def resolve_query_type(query):
return solutions return solutions
def resolve_query(query): def resolve_query(query, allowed_queries=None):
rv = { rv = {
"query": query, "query": query,
"solutions": [], "solutions": [],
} }
solutions = resolve_query_type(query) solutions = resolve_query_type(query)
if not allowed_queries:
allowed_queries = [QUERY_TYPE_DT, QUERY_TYPE_DT_TR, QUERY_TYPE_TD, QUERY_TYPE_TZ]
for sol_id, solution in enumerate(solutions): for sol_id, solution in enumerate(solutions):
element = {} element = {}
handler, results, query_type, hi = solution handler, results, query_type, hi = solution
@ -261,11 +266,13 @@ def resolve_query(query):
element["highlight"] = hi element["highlight"] = hi
element["solution_id"] = sol_id element["solution_id"] = sol_id
try: try:
if query_type not in allowed_queries:
continue
if query_type == QUERY_TYPE_DT: if query_type == QUERY_TYPE_DT:
element["dt"] = dt_pretty(results) element["dt"] = dt_pretty(results)
elif query_type == QUERY_TYPE_DT_TR: elif query_type == QUERY_TYPE_DT_TR:
element["src_dt"] = dt_pretty(results[0]) element["src_dt"] = dt_pretty(results[0])
element["dst_dt"] = dt_pretty(results[1]) element["dt"] = dt_pretty(results[1])
element["tz"] = results[2] element["tz"] = results[2]
elif query_type == QUERY_TYPE_TZ: elif query_type == QUERY_TYPE_TZ:
element["tz"] = results element["tz"] = results

View File

@ -6,7 +6,7 @@ from tokenizer import resolve_query, QUERY_TYPE_DT, QUERY_TYPE_DT_TR, QUERY_TYPE
app = Flask(__name__) app = Flask(__name__)
import dateparser import dateparser
from tww import resolve_timezone from tww.lib import resolve_timezone
IN_KW = " in " IN_KW = " in "
TO_KW = " to " TO_KW = " to "