-
Notifications
You must be signed in to change notification settings - Fork 0
/
nl_utils.py
72 lines (63 loc) · 2.33 KB
/
nl_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import inflect
from global_variables import GRAMMAR_ROLE_NAMES
def pluralize(restr_concept, whole=False):
p = inflect.engine()
pluralized = str()
change = True
for word in restr_concept.split():
if (change is True) and (word in GRAMMAR_ROLE_NAMES):
pluralized += f"{p.plural_verb(word)} "
if not whole:
change = False
else:
pluralized += f"{word} "
pluralized = pluralized.strip()
return pluralized
def restriction_concept_nl(restriction_concept):
"""
Given a restriction concept, returns it's natural language representation.
Args:
arguments_list (list): List of concept's arguments
Returns:
str: the restriction concept in nl
"""
restriction_concept_nl = str()
restriction = restriction_concept.restriction.split()
role_name = restriction_concept.role_name
concept_nl = restriction_concept.concept.nl()
if restriction[0] == "∀":
restriction_concept_nl = (
f"{role_name.replace('_',' ')} only {concept_nl} things"
)
elif restriction[0] == "∃":
restriction_concept_nl = (
f"exists something {concept_nl} that it {role_name.replace('_',' ')}"
)
else:
quantity = restriction[0]
quantifier = restriction[1]
if quantifier == ">":
restriction_concept_nl = (
f"{role_name.replace('_',' ')} more than {quantity} {concept_nl} things"
)
elif quantifier == "<":
restriction_concept_nl = (
f"{role_name.replace('_',' ')} less than {quantity} {concept_nl} things"
)
elif quantifier == ">=":
restriction_concept_nl = (
f"{role_name.replace('_',' ')} at least {quantity} {concept_nl} things"
)
elif quantifier == "<=":
restriction_concept_nl = (
f"{role_name.replace('_',' ')} at most {quantity} {concept_nl} things"
)
elif quantifier == "=":
restriction_concept_nl = (
f"{role_name.replace('_',' ')} exactly {quantity} {concept_nl} things"
)
else:
raise ValueError(
f"Got unexpected restriction concept: '{restriction_concept}'"
)
return restriction_concept_nl