-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathanalyse_text.py
98 lines (75 loc) · 2.71 KB
/
analyse_text.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (C) 2018 Ben North
#
# This file is part of 'plausibility argument of concept for compiling
# Python into Amazon Step Function state machine JSON'.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pysfn as PSF
class TextTooShortError(Exception):
pass
def get_summary(text):
if len(text) == 0:
raise TextTooShortError
return {'head': text[0]}
def augment_summary(text, summary):
aug_summary = dict(summary)
aug_summary['n_characters'] = len(text)
return aug_summary
def get_n_vowels(text):
return sum(text.count(v) for v in 'aeiou')
def get_n_spaces(text):
return text.count(' ')
def format_result(summary, infos):
"""
Expect inputs of:
summary --- dict with 'head' and 'n_characters' as keys
infos --- list of two elements, each a number
"""
return (f'text starts with {summary["head"]},'
f' has {summary["n_characters"]} chars,'
f' {infos[0]} vowels, and'
f' {infos[1]} spaces')
def format_c_result(summary):
return f'text starts with "c"; look: "{summary["head"]}"'
# Top-level function, very loosely inspired by the control flow in
#
# https://github.com/aws-samples/lambda-refarch-imagerecognition
#
@PSF.main
def summarise(text):
try:
summary = get_summary(text)
except TextTooShortError:
raise PSF.Fail('MalformedText', 'text too short')
if (PSF.StringEquals(summary['head'], 'a')
or PSF.StringEquals(summary['head'], 'b')):
summary = PSF.with_retry_spec(augment_summary, (text, summary),
(['States.ALL'], 1, 2, 1.5))
def get_n_vowels_task():
result = get_n_vowels(text)
return result
#
def get_n_spaces_task():
result = get_n_spaces(text)
return result
#
more_info = PSF.parallel(get_n_vowels_task, get_n_spaces_task)
result = format_result(summary, more_info)
#
elif PSF.StringEquals(summary['head'], 'c'):
result = format_c_result(summary)
#
else:
raise PSF.Fail('MalformedText', 'wrong starting letter')
return result