Create example.py
Browse files- example.py +69 -0
example.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Minimal usage example for hv-split.
|
| 3 |
+
|
| 4 |
+
python example.py
|
| 5 |
+
|
| 6 |
+
Splits eight queries with different ambiguity sources, shows the bundle
|
| 7 |
+
structure, and demonstrates answer_each with a mock callback.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from hv_split import HVInterpret
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
QUERIES = [
|
| 14 |
+
("lexical", "I need to access the bank"),
|
| 15 |
+
("referential",
|
| 16 |
+
"She told her that the manager had changed it, and it broke"),
|
| 17 |
+
("scope", "All that glitters is not gold"),
|
| 18 |
+
("presuppositional", "Why did the CEO resign last week?"),
|
| 19 |
+
("framing",
|
| 20 |
+
"in the language of category theory, what is an identity?"),
|
| 21 |
+
("temporal", "recently, has the function changed?"),
|
| 22 |
+
("multi-source",
|
| 23 |
+
"why did the current bank say recently that the function she used "
|
| 24 |
+
"in the language of category theory had changed?"),
|
| 25 |
+
("clean", "compute 2 + 2"),
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def main() -> None:
|
| 30 |
+
m = HVInterpret()
|
| 31 |
+
|
| 32 |
+
print("=" * 78)
|
| 33 |
+
print("Bundle summary")
|
| 34 |
+
print("=" * 78)
|
| 35 |
+
print(f" {'name':<16} {'ambig':>7} {'conf':>6} {'n':>3} sources")
|
| 36 |
+
print(" " + "-" * 70)
|
| 37 |
+
for name, q in QUERIES:
|
| 38 |
+
b = m.split(q)
|
| 39 |
+
srcs = ", ".join(b.sources) or "(none)"
|
| 40 |
+
print(
|
| 41 |
+
f" {name:<16} {b.ambiguity_score:>7.3f} "
|
| 42 |
+
f"{b.confidence:>6.3f} {b.n_interpretations:>3} {srcs}"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
print()
|
| 46 |
+
print("=" * 78)
|
| 47 |
+
print("Full bundle: multi-source query")
|
| 48 |
+
print("=" * 78)
|
| 49 |
+
b = m.split(QUERIES[6][1])
|
| 50 |
+
print(m.render(b))
|
| 51 |
+
|
| 52 |
+
print("=" * 78)
|
| 53 |
+
print("answer_each with a mock callback")
|
| 54 |
+
print("=" * 78)
|
| 55 |
+
for interp, answer in m.answer_each(
|
| 56 |
+
QUERIES[0][1],
|
| 57 |
+
lambda q, i: f"[would answer as: {i.reading}]",
|
| 58 |
+
):
|
| 59 |
+
print(f" prior {interp.prior:.3f} -> {answer}")
|
| 60 |
+
|
| 61 |
+
print()
|
| 62 |
+
print("=" * 78)
|
| 63 |
+
print("JSON output (presuppositional query)")
|
| 64 |
+
print("=" * 78)
|
| 65 |
+
print(m.render(m.split(QUERIES[3][1]), mode="json"))
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
main()
|