forked from yask123/Summarize-it
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.py
49 lines (42 loc) · 1.63 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division, print_function, unicode_literals
class ItemsCount(object):
def __init__(self, value):
self._value = value
self.string_types = (str, unicode)
def __call__(self, sequence):
if isinstance(self._value, self.string_types):
if self._value.endswith("%"):
total_count = len(sequence)
percentage = int(self._value[:-1])
# at least one sentence should be chosen
count = max(1, total_count*percentage // 100)
return sequence[:count]
else:
return sequence[:int(self._value)]
elif isinstance(self._value, (int, float)):
return sequence[:int(self._value)]
else:
ValueError("Unsuported value of items count '%s'." % self._value)
def __repr__(self):
return to_string("<ItemsCount: %r>" % self._value)
def maybe_get(cont, key, default=None):
return cont[key] if key in cont else default
def get_msg_text(msg):
"""Pull the appropriate text from the message"""
if 'text' in msg and len(msg['text']) > 0:
return msg['text']
if 'attachments' in msg:
ats = msg['attachments']
if len(ats) > 0:
at = ats[0]
att_text = []
if 'title' in at:
att_text.append(at['title'])
if 'text' in at:
att_text.append(at['text'])
max_text = max(att_text, key=lambda txt: len(txt))
if len(max_text) > 0:
return max_text
return u""