Simpler hearts, with streaks!
This commit is contained in:
parent
93de36ca8b
commit
979fb50ea4
1 changed files with 29 additions and 40 deletions
|
@ -9,10 +9,8 @@ import re
|
||||||
|
|
||||||
HEARTS_FILE = 'hearts.json'
|
HEARTS_FILE = 'hearts.json'
|
||||||
|
|
||||||
PLUSES = {'prefix': ['++', '<3', '<3', ':heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:', '❤️', '💛', '💚', '💙', '💜'],
|
PLUSES = ['++', '<3', '<3', ':heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:', '❤️', '💛', '💚', '💙', '💜']
|
||||||
'suffix': ['++', '<3', '<3', ':heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:', '❤️', '💛', '💚', '💙', '💜']}
|
MINUSES = ['--', '–', '—', '</3', '</3', ':broken_heart:', '💔']
|
||||||
MINUSES = {'prefix': ['--', '–', '—', '</3', '</3', ':broken_heart:', '💔'],
|
|
||||||
'suffix': ['--', '–', '—', '</3', '</3', ':broken_heart:', '💔']}
|
|
||||||
|
|
||||||
LOGGER = logging.getLogger('hearts')
|
LOGGER = logging.getLogger('hearts')
|
||||||
|
|
||||||
|
@ -53,17 +51,23 @@ def calculate_score_and_find_operators(text):
|
||||||
original_text = text
|
original_text = text
|
||||||
score = 0
|
score = 0
|
||||||
|
|
||||||
times, text = _do_operators(text, PLUSES['prefix'], is_prefix=True)
|
while True:
|
||||||
score += times
|
found = False
|
||||||
|
|
||||||
times, text = _do_operators(text, PLUSES['suffix'], is_prefix=False)
|
op, is_prefix = has_operator(text, PLUSES)
|
||||||
score += times
|
if op:
|
||||||
|
text = strip_operator(text, op, is_prefix)
|
||||||
|
score += 1
|
||||||
|
found = True
|
||||||
|
|
||||||
times, text = _do_operators(text, MINUSES['prefix'], is_prefix=True)
|
op, is_prefix = has_operator(text, MINUSES)
|
||||||
score -= times
|
if op:
|
||||||
|
text = strip_operator(text, op, is_prefix)
|
||||||
|
score -= 1
|
||||||
|
found = True
|
||||||
|
|
||||||
times, text = _do_operators(text, MINUSES['suffix'], is_prefix=False)
|
if not found:
|
||||||
score -= times
|
break
|
||||||
|
|
||||||
did_change = original_text != text
|
did_change = original_text != text
|
||||||
if did_change:
|
if did_change:
|
||||||
|
@ -71,23 +75,12 @@ def calculate_score_and_find_operators(text):
|
||||||
else:
|
else:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
def _do_operators(text, operators, is_prefix):
|
def strip_operator(text, operator, is_prefix):
|
||||||
times = 0
|
len_op = len(operator)
|
||||||
length = 0
|
if is_prefix:
|
||||||
check_func = has_prefix if is_prefix else has_suffix
|
return text[len_op:].lstrip()
|
||||||
while True:
|
else:
|
||||||
op = check_func(text, operators)
|
return text[:-len_op].rstrip()
|
||||||
if not op:
|
|
||||||
break
|
|
||||||
LOGGER.info('Found operator: {} (prefix = {})'.format(op, is_prefix))
|
|
||||||
times += 1
|
|
||||||
op_len = len(op)
|
|
||||||
length += op_len
|
|
||||||
if is_prefix:
|
|
||||||
text = text[op_len:].lstrip()
|
|
||||||
else:
|
|
||||||
text = text[:-op_len].rstrip()
|
|
||||||
return times, text
|
|
||||||
|
|
||||||
def top5():
|
def top5():
|
||||||
data = read_data()
|
data = read_data()
|
||||||
|
@ -133,14 +126,10 @@ def write_data(obj):
|
||||||
with open(HEARTS_FILE, 'w') as f:
|
with open(HEARTS_FILE, 'w') as f:
|
||||||
json.dump(obj, f, sort_keys=True, indent=4)
|
json.dump(obj, f, sort_keys=True, indent=4)
|
||||||
|
|
||||||
def has_prefix(text, prefixes):
|
def has_operator(text, operators):
|
||||||
for p in prefixes:
|
for op in operators:
|
||||||
if text.startswith(p):
|
if text.startswith(op):
|
||||||
return p
|
return op, True
|
||||||
return None
|
elif text.endswith(op):
|
||||||
|
return op, False
|
||||||
def has_suffix(text, suffixes):
|
return None, None
|
||||||
for s in suffixes:
|
|
||||||
if text.endswith(s):
|
|
||||||
return s
|
|
||||||
return None
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue