Update Hammurabi example code to v3 (#9218)

* Update Hammurabi example code

* Fix typo
This commit is contained in:
Edward 2021-09-16 13:32:44 +02:00 committed by GitHub
parent c4f0800fb8
commit 8bda39f088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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",