From 8bda39f088b1f0943ebd12f5ac425dcc74c4e5f0 Mon Sep 17 00:00:00 2001 From: Edward <43848523+thomashacker@users.noreply.github.com> Date: Thu, 16 Sep 2021 13:32:44 +0200 Subject: [PATCH] Update Hammurabi example code to v3 (#9218) * Update Hammurabi example code * Fix typo --- website/meta/universe.json | 64 +++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/website/meta/universe.json b/website/meta/universe.json index 6e5696be1..396f56bf4 100644 --- a/website/meta/universe.json +++ b/website/meta/universe.json @@ -3238,33 +3238,61 @@ "github": "babylonhealth/hmrb", "pip": "hmrb", "code_example": [ - "import spacy # __version__ 3.0+", + "import spacy", "from hmrb.core import SpacyCore", "", + "nlp = spacy.load(\"en_core_web_sm\")", + "sentences = \"I love gorillas. Peter loves gorillas. Jane loves Tarzan.\"", + "", + "def conj_be(subj: str) -> str:", + " if subj == \"I\":", + " return \"am\"", + " elif subj == \"you\":", + " return \"are\"", + " else:", + " return \"is\"", + "", + "@spacy.registry.callbacks(\"gorilla_callback\")", + "def gorilla_clb(seq: list, span: slice, data: dict) -> None:", + " subj = seq[span.start].text", + " be = conj_be(subj)", + " print(f\"{subj} {be} a gorilla person.\")", + "@spacy.registry.callbacks(\"lover_callback\")", + "def lover_clb(seq: list, span: slice, data: dict) -> None:", + " print(f\"{seq[span][-1].text} is a love interest of {seq[span.start].text}.\")", + "", "grammar = \"\"\"", - "Var is_hurting:", - "(", - " optional (lemma: \"be\")", - " (lemma: \"hurt\")", - ")", - "Law:", - " - package: \"headache\"", - " - callback: \"mark_headache\"", - "(", - " (lemma: \"head\", pos: \"NOUN\")", - " $is_hurting", - ")\"\"\"", + " Law:", + " - callback: \"loves_gorilla\"", + " (", + " ((pos: \"PROPN\") or (pos: \"PRON\"))", + " (lemma: \"love\")", + " (lemma: \"gorilla\")", + " )", + " Law:", + " - callback: \"loves_someone\"", + " (", + " (pos: \"PROPN\")", + " (lower: \"loves\")", + " (pos: \"PROPN\")", + " )", + "\"\"\"", + "", + "@spacy.registry.augmenters(\"jsonify_span\")", + "def jsonify_span(span):", + " return [{\"lemma\": token.lemma_, \"pos\": token.pos_, \"lower\": token.lower_} for token in span]", "", "conf = {", - " \"rules\": grammar", + " \"rules\": grammar,", " \"callbacks\": {", - " \"mark_headache\": \"callbacks.headache_handler\",", - " },", + " \"loves_gorilla\": \"callbacks.gorilla_callback\",", + " \"loves_someone\": \"callbacks.lover_callback\",", + " },", " \"map_doc\": \"augmenters.jsonify_span\",", " \"sort_length\": True,", "}", - "nlp = spacy.load(\"en_core_web_sm\")", - "nlp.add_pipe(\"hammurabi\", config=conf)", + "", + "nlp.add_pipe(\"hmrb\", config=conf)", "nlp(sentences)" ], "code_language": "python",