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", "github": "babylonhealth/hmrb",
"pip": "hmrb", "pip": "hmrb",
"code_example": [ "code_example": [
"import spacy # __version__ 3.0+", "import spacy",
"from hmrb.core import SpacyCore", "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 = \"\"\"", "grammar = \"\"\"",
"Var is_hurting:", " Law:",
"(", " - callback: \"loves_gorilla\"",
" optional (lemma: \"be\")", " (",
" (lemma: \"hurt\")", " ((pos: \"PROPN\") or (pos: \"PRON\"))",
")", " (lemma: \"love\")",
"Law:", " (lemma: \"gorilla\")",
" - package: \"headache\"", " )",
" - callback: \"mark_headache\"", " Law:",
"(", " - callback: \"loves_someone\"",
" (lemma: \"head\", pos: \"NOUN\")", " (",
" $is_hurting", " (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 = {", "conf = {",
" \"rules\": grammar", " \"rules\": grammar,",
" \"callbacks\": {", " \"callbacks\": {",
" \"mark_headache\": \"callbacks.headache_handler\",", " \"loves_gorilla\": \"callbacks.gorilla_callback\",",
" \"loves_someone\": \"callbacks.lover_callback\",",
" },", " },",
" \"map_doc\": \"augmenters.jsonify_span\",", " \"map_doc\": \"augmenters.jsonify_span\",",
" \"sort_length\": True,", " \"sort_length\": True,",
"}", "}",
"nlp = spacy.load(\"en_core_web_sm\")", "",
"nlp.add_pipe(\"hammurabi\", config=conf)", "nlp.add_pipe(\"hmrb\", config=conf)",
"nlp(sentences)" "nlp(sentences)"
], ],
"code_language": "python", "code_language": "python",