Skip to content

Commit

Permalink
feat: Add experimental temporal._month_hashtag_latlng base processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sieboldianus committed Nov 23, 2022
1 parent c395bcb commit 60da223
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/mappings.md
Expand Up @@ -297,6 +297,7 @@ Bases can be separated by comma and may include:
- `_emoji_latlng`
- `_month_latlng`
- `_month_hashtag`
- `_month_hashtag_latlng`


For example:
Expand Down
1 change: 1 addition & 0 deletions lbsntransform/config/config.py
Expand Up @@ -671,6 +671,7 @@ def get_arg_parser(
'* _term_latlng '
'* _emoji_latlng '
'* _month_hashtag '
'* _month_hashtag_latlng '
'* _month_latlng '
'* monthofyear '
'* month '
Expand Down
53 changes: 52 additions & 1 deletion lbsntransform/output/hll/base/temporal.py
Expand Up @@ -11,6 +11,7 @@

FACET = 'temporal'


class TimestampBase(hll.HllBase):
"""Extends Base Class"""
NAME = hll.HllBaseRef(facet=FACET, base='timestamp')
Expand Down Expand Up @@ -183,6 +184,7 @@ def __init__(self, record: lbsn.Post = None):
if post_date_time:
self.key['monthofyear'] = post_date_time.month


class MonthHashtagBase(hll.HllBase):
"""Composite Base (c-base) that extends from HLL base Class
Expand Down Expand Up @@ -215,6 +217,7 @@ def __init__(self, record: lbsn.Post = None, hashtag: str = None):
"Parsing of MonthHashtagBase only supported "
"from lbsn.Post")


class MonthLatLngBase(hll.HllBase):
"""Composite Base (c-base) that extends from HLL base Class
Expand Down Expand Up @@ -255,4 +258,52 @@ def __init__(self, record: lbsn.Post = None):
else:
raise ValueError(
"Parsing of MonthLatLngBase only supported "
"from lbsn.Post")
"from lbsn.Post")


class MonthHashtagLatLngBase(hll.HllBase):
"""Composite Base (c-base) that extends from hll.HllBase Class
Note: To distinguish c-bases which are composite bases combining
aspects from multiple facets, they're termed with a leading underscore
"""
NAME = hll.HllBaseRef(facet=FACET, base='_month_hashtag_latlng')

def __init__(self, record: lbsn.Post = None, hashtag: str = None):

super().__init__()
self.key['year'] = None
self.key['month'] = None
self.key['hashtag'] = None
self.key['latitude'] = None
self.key['longitude'] = None
self.attrs['latlng_geom'] = None
if hashtag is None:
# init empty
return
self.key['hashtag'] = hashtag.lower()

if record is None:
# init empty
return

if not isinstance(record, lbsn.Post):
raise ValueError(
"Parsing of MonthHashtagLatLngBase only supported "
"from lbsn.Post")
post_date_time = HLF.merge_dates_post(
record)
if post_date_time:
date = post_date_time.date()
self.key['year'] = date.year
self.key['month'] = date.month
coordinates_geom = record.post_latlng
coordinates = HF.get_coordinates_from_ewkt(
coordinates_geom
)
self.key['latitude'] = coordinates.lat
self.key['longitude'] = coordinates.lng
# additional (optional) attributes
# formatted ready for sql upsert
self.attrs['latlng_geom'] = HF.return_ewkb_from_geotext(
coordinates_geom)
8 changes: 8 additions & 0 deletions lbsntransform/output/hll/hll_bases.py
Expand Up @@ -115,6 +115,7 @@ def merge_base_metrics(base1, base2):
continue
base1.metrics[key] |= new_set


def append_baserecord(
base_records: List['HllBase'], base_record: 'HllBase'):
"""Append base_record to list, if all keys have valid values (not None)"""
Expand All @@ -124,6 +125,7 @@ def append_baserecord(
return
base_records.append(base_record)


def base_factory(facet=None, base=None, record: lbsn.Post = None):
"""Base is initialized based on facet-base tuple
and constructed by parsing lbsn records
Expand Down Expand Up @@ -194,6 +196,12 @@ def base_factory(facet=None, base=None, record: lbsn.Post = None):
for tag in tag_terms:
base_record = base_structure(record=record, hashtag=tag)
append_baserecord(records, base_record)
elif base == '_month_hashtag_latlng':
# any hashtag explicitly used
tag_terms = HF.filter_terms(record.hashtags)
for tag in tag_terms:
base_record = base_structure(record=record, hashtag=tag)
append_baserecord(records, base_record)
else:
# init for all other bases with single lbsn record
base_record = base_structure(record)
Expand Down
6 changes: 4 additions & 2 deletions lbsntransform/output/hll/shared_structure_proto_hlldb.py
Expand Up @@ -19,7 +19,8 @@
spatial.CityBase, spatial.CountryBase,
temporal.DateBase, temporal.MonthBase,
temporal.YearBase, temporal.MonthLatLngBase,
temporal.MonthHashtagBase, topical.TermBase,
temporal.MonthHashtagBase,
temporal.MonthHashtagLatLngBase, topical.TermBase,
topical.EmojiBase, topical.TermLatLngBase,
topical.HashtagLatLngBase, topical.EmojiLatLngBase,
social.CommunityBase]
Expand Down Expand Up @@ -136,7 +137,8 @@ def extract_hll_bases(
base_list.extend(base_records)
# Temporal Facet
temporal_bases = [
'date', 'month', 'year', '_month_latlng', '_month_hashtag']
'date', 'month', 'year', '_month_latlng', '_month_hashtag',
'_month_hashtag_latlng']
temporal_bases = self.filter_bases(
temporal_bases, include_lbsn_bases)
base_records = self.make_bases(
Expand Down
1 change: 1 addition & 0 deletions lbsntransform/output/submit_data.py
Expand Up @@ -87,6 +87,7 @@ def __init__(self, db_cursor=None,
temporal.YearBase.NAME: dict(),
temporal.MonthLatLngBase.NAME: dict(),
temporal.MonthHashtagBase.NAME: dict(),
temporal.MonthHashtagLatLngBase.NAME: dict(),
topical.TermBase.NAME: dict(),
topical.HashtagBase.NAME: dict(),
topical.EmojiBase.NAME: dict(),
Expand Down

0 comments on commit 60da223

Please sign in to comment.