Bohui Zhang commited on
Commit ·
dabff4d
1
Parent(s): 4389c07
Update verbalizers
Browse files- esgen/verbalizer.py +63 -6
esgen/verbalizer.py
CHANGED
|
@@ -1,15 +1,72 @@
|
|
| 1 |
-
def
|
| 2 |
-
comment = f"
|
| 3 |
return comment
|
| 4 |
|
| 5 |
|
| 6 |
def chatbot_verbaliser(prop_label, freq, examples):
|
| 7 |
message = f"{freq * 100:.2f}% instances within the class use the property '{prop_label}'. Here are examples:\n"
|
| 8 |
# verbalise examples
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
examples_verbalised = '\n'.join([f"- {line}" for line in example_lines])
|
| 13 |
message += examples_verbalised
|
| 14 |
-
|
|
|
|
| 15 |
return message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def init_comment_verbaliser(prop_label, freq):
|
| 2 |
+
comment = f"{prop_label}, {freq * 100:.2f}%"
|
| 3 |
return comment
|
| 4 |
|
| 5 |
|
| 6 |
def chatbot_verbaliser(prop_label, freq, examples):
|
| 7 |
message = f"{freq * 100:.2f}% instances within the class use the property '{prop_label}'. Here are examples:\n"
|
| 8 |
# verbalise examples
|
| 9 |
+
# TODO: remove IDs and change strings to links
|
| 10 |
+
# example_lines = [f"{example['subjectLabel']} [https://www.wikidata.org/wiki/{example['subject']}], "
|
| 11 |
+
# f"{example['propertyLabel']} [https://www.wikidata.org/wiki/Property:{example['property']}], "
|
| 12 |
+
# f"{example['objectLabel']} [https://www.wikidata.org/wiki/{example['object']}]"
|
| 13 |
+
# for example in examples]
|
| 14 |
+
example_lines = [f"{example['subjectLabel']} ({example['subject']}), "
|
| 15 |
+
f"{example['propertyLabel']} ({example['property']}), "
|
| 16 |
+
f"{example['objectLabel']} ({example['object']})"
|
| 17 |
+
for example in examples]
|
| 18 |
examples_verbalised = '\n'.join([f"- {line}" for line in example_lines])
|
| 19 |
message += examples_verbalised
|
| 20 |
+
# TODO: remove indent
|
| 21 |
+
message += "\n\nDo you think the property is properly used for all instances within the class?"
|
| 22 |
return message
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def prefix_verbaliser(abbr, url):
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
:param abbr:
|
| 29 |
+
:param url:
|
| 30 |
+
:return:
|
| 31 |
+
"""
|
| 32 |
+
return f"PREFIX {abbr}: <{url}>\n"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def triple_constraint_verbaliser(constraint):
|
| 36 |
+
""" single line constraint verbaliser
|
| 37 |
+
|
| 38 |
+
:param constraint:
|
| 39 |
+
:return:
|
| 40 |
+
"""
|
| 41 |
+
if constraint.constraint_type == "value_shape":
|
| 42 |
+
output = f"\t {constraint.prop} @<{constraint.value}> {constraint.cardinality} ;"
|
| 43 |
+
elif isinstance(constraint.value, list): # value sets
|
| 44 |
+
output = f"\t {constraint.prop} [ {' '.join(constraint.value)} ] {constraint.cardinality} ;"
|
| 45 |
+
else:
|
| 46 |
+
output = f"\t {constraint.prop} {constraint.value} {constraint.cardinality} ;"
|
| 47 |
+
if constraint.comment:
|
| 48 |
+
output += f" # {constraint.comment} \n"
|
| 49 |
+
else:
|
| 50 |
+
output += "\n"
|
| 51 |
+
return output
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def value_shape_verbaliser(name: str, constraints: list, extra: bool):
|
| 55 |
+
"""
|
| 56 |
+
assumptions:
|
| 57 |
+
the first property in constraints is wdt:P31 or wdt:P279
|
| 58 |
+
|
| 59 |
+
:param name:
|
| 60 |
+
:param constraints:
|
| 61 |
+
:param extra:
|
| 62 |
+
:return:
|
| 63 |
+
"""
|
| 64 |
+
output = "".join(triple_constraint_verbaliser(constraint) for constraint in constraints)
|
| 65 |
+
# if extra:
|
| 66 |
+
# nested_constraints = f"<{name}> EXTRA {{\n{output}}}\n"
|
| 67 |
+
# else:
|
| 68 |
+
# nested_constraints = f"<{name}> {{\n{output}}}\n"
|
| 69 |
+
# print(constraints)
|
| 70 |
+
# nested_constraints = f"<{name}> EXTRA {constraints[0].prop} {{\n{output}}}\n"
|
| 71 |
+
nested_constraints = f"<{name}> {{\n{output}}}\n"
|
| 72 |
+
return nested_constraints
|