### Install SciSpaCy Model Source: https://github.com/allenai/scispacy/blob/main/README.md Installs a specific SciSpaCy model (e.g., en_core_sci_sm) from a provided URL. Ensure the model version is compatible with your SciSpaCy installation. ```bash pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_sm-0.5.4.tar.gz ``` -------------------------------- ### Install SciSpaCy Model from URL (Python) Source: https://github.com/allenai/scispacy/blob/main/README.md Installs a SciSpaCy model directly from a URL using pip. This requires copying the download link address. ```python pip install CMD-V(to paste the copied URL) ``` -------------------------------- ### Install SciSpaCy Library Source: https://github.com/allenai/scispacy/blob/main/README.md Installs the SciSpaCy library using pip. It's recommended to use an isolated Python environment. ```bash pip install scispacy ``` -------------------------------- ### Install SciSpaCy Model from Local File (Python) Source: https://github.com/allenai/scispacy/blob/main/README.md Installs a SciSpaCy model from a local file path using pip. This is useful after downloading the model archive. ```python pip install ``` -------------------------------- ### Install scispaCy and Models Source: https://github.com/allenai/scispacy/blob/main/docs/index.md Installs the scispaCy package and a specified model by providing its URL. This is the initial step before using any scispaCy models for text processing. ```python pip install scispacy pip install ``` -------------------------------- ### EntityLinker Knowledge Base Lookup Example Source: https://github.com/allenai/scispacy/blob/main/README.md Demonstrates how to retrieve detailed information about a linked knowledge base concept using its ID. This requires an initialized EntityLinker instance with a loaded knowledge base. ```python print(linker.kb.cui_to_entity[concept_id]) ``` -------------------------------- ### Install ScispaCy and Biomedical Models (Bash) Source: https://context7.com/allenai/scispacy/llms.txt Installs the scispaCy library and various pre-trained biomedical models using pip. These models include general-purpose scientific models (sm, md, lg, scibert) and specialized NER models for different corpora. ```bash pip install scispacy pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_sm-0.5.4.tar.gz pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_md-0.5.4.tar.gz pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_lg-0.5.4.tar.gz pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_scibert-0.5.4.tar.gz pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_ner_craft_md-0.5.4.tar.gz pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_ner_jnlpba_md-0.5.4.tar.gz pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_ner_bc5cdr_md-0.5.4.tar.gz pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_ner_bionlp13cg_md-0.5.4.tar.gz ``` -------------------------------- ### EntityLinker Pipeline Integration and Usage Example Source: https://github.com/allenai/scispacy/blob/main/README.md Shows how to load a SpaCy model, add the SciSpaCy EntityLinker to the pipeline with specific configurations (e.g., resolving abbreviations, choosing a linker), process a document, and access the linked entities from a SpaCy Span. ```python import spacy import scispacy from scispacy.linking import EntityLinker nlp = spacy.load("en_core_sci_sm") # This line takes a while, because we have to download ~1GB of data # and load a large JSON file (the knowledge base). Be patient! # Thankfully it should be faster after the first time you use it, because # the downloads are cached. # NOTE: The resolve_abbreviations parameter is optional, and requires that # the AbbreviationDetector pipe has already been added to the pipeline. Adding # the AbbreviationDetector pipe and setting resolve_abbreviations to True means # that linking will only be performed on the long form of abbreviations. nlp.add_pipe("scispacy_linker", config={"resolve_abbreviations": True, "linker_name": "umls"}) doc = nlp("Spinal and bulbar muscular atrophy (SBMA) is an \ninherited motor neuron disease caused by the expansion \nof a polyglutamine tract within the androgen receptor (AR). \nSBMA can be caused by this easily.") # Let's look at a random entity! entity = doc.ents[1] print("Name: ", entity) # >>> Name: bulbar muscular atrophy # Each entity is linked to UMLS with a score ``` -------------------------------- ### Entity Linking with Different Knowledge Bases in SciSpaCy Source: https://context7.com/allenai/scispacy/llms.txt This section provides examples of configuring the SciSpaCy EntityLinker to use various biomedical knowledge bases beyond UMLS, including MeSH, RxNorm, Gene Ontology (GO), and Human Phenotype Ontology (HPO). Each example shows how to load a spaCy model, add the linker with a specific knowledge base, process a relevant text, and print the linked entity names. ```python import spacy from scispacy.linking import EntityLinker # Example: Using MeSH for medical subject headings nlp_mesh = spacy.load("en_core_sci_sm") nlp_mesh.add_pipe("scispacy_linker", config={"linker_name": "mesh"}) doc = nlp_mesh("The patient was diagnosed with type 2 diabetes mellitus.") linker = nlp_mesh.get_pipe("scispacy_linker") for ent in doc.ents: if ent._.kb_ents: print(f"{ent.text}: {[linker.kb.cui_to_entity[c[0]].canonical_name for c in ent._.kb_ents[:2]]}") # Example: Using RxNorm for drug linking nlp_rxnorm = spacy.load("en_core_sci_sm") nlp_rxnorm.add_pipe("scispacy_linker", config={"linker_name": "rxnorm"}) doc = nlp_rxnorm("The patient was prescribed metformin and lisinopril.") # Example: Using Gene Ontology nlp_go = spacy.load("en_core_sci_sm") nlp_go.add_pipe("scispacy_linker", config={"linker_name": "go"}) doc = nlp_go("The protein is involved in DNA repair and cell cycle regulation.") # Example: Using Human Phenotype Ontology nlp_hpo = spacy.load("en_core_sci_sm") nlp_hpo.add_pipe("scispacy_linker", config={"linker_name": "hpo"}) doc = nlp_hpo("The patient presented with intellectual disability and seizures.") ``` -------------------------------- ### Create Conda Environment for SciSpaCy Source: https://github.com/allenai/scispacy/blob/main/README.md Creates a Conda virtual environment named 'scispacy' with a specified Python version (e.g., 3.10). This is a prerequisite for installing SciSpaCy. ```bash mamba create -n scispacy python=3.10 ``` -------------------------------- ### Load and Use SciSpaCy Model in Python Source: https://github.com/allenai/scispacy/blob/main/README.md Loads a SciSpaCy model (e.g., 'en_core_sci_sm') using spaCy's load function and processes a sample text. This demonstrates basic usage after installation. ```python import spacy nlp = spacy.load("en_core_sci_sm") doc = nlp("Alterations in the hypocretin receptor 2 and preprohypocretin genes produce narcolepsy in some animals.") ``` -------------------------------- ### Named Entity Recognition Models in SciSpaCy (Python) Source: https://context7.com/allenai/scispacy/llms.txt Provides an overview of SciSpaCy's specialized NER models trained on various biomedical corpora. This example shows the BC5CDR model for identifying Diseases and Chemicals. ```python import spacy # BC5CDR model: Diseases and Chemicals nlp_bc5cdr = spacy.load("en_ner_bc5cdr_md") doc = nlp_bc5cdr("Aspirin may cause gastrointestinal bleeding in patients with peptic ulcer disease.") print("BC5CDR Entities (DISEASE, CHEMICAL):") for ent in doc.ents: print(f" {ent.text:30} -> {ent.label_}") # Output: ``` -------------------------------- ### Search for version references Source: https://github.com/allenai/scispacy/blob/main/RELEASE.md Uses git grep to identify all files containing a specific version string, which is necessary when updating documentation and readme files after a new release. ```bash git grep ``` -------------------------------- ### EntityLinker with Custom Knowledge Bases in Python Source: https://context7.com/allenai/scispacy/llms.txt Demonstrates how to initialize the EntityLinker from custom knowledge bases, including UMLS, local JSON/JSONL files, and external ontologies via Pyobo. This allows linking entities to arbitrary concept IDs and definitions. ```python import spacy from scispacy.linking import EntityLinker from scispacy.linking_utils import KnowledgeBase, UmlsKnowledgeBase # Method 1: Create linker directly from a knowledge base object uumls_kb = UmlsKnowledgeBase() linker = EntityLinker.from_kb(umls_kb) nlp = spacy.load("en_core_sci_sm") text = "The androgen receptor gene is associated with Kennedy disease." doc = linker(nlp(text)) for ent in doc.ents: if ent._.kb_ents: print(f"{ent.text}: {ent._.kb_ents[:2]}") # Method 2: Load KB from a custom JSON/JSONL file # The file should contain entities with: concept_id, canonical_name, aliases, types, definition custom_kb = KnowledgeBase("path/to/custom_kb.jsonl") custom_linker = EntityLinker.from_kb(custom_kb, filter_for_definitions=False) # Method 3: Using pyobo for additional ontologies # pip install "pyobo>=0.12.9" import pyobo # Create linker from HGNC (gene symbols) database hgnc_linker = pyobo.get_scispacy_entity_linker("hgnc", filter_for_definitions=False) nlp = spacy.load("en_core_sci_sm") text = "RAC(Rho family)-alpha serine/threonine-protein kinase is encoded by the AKT1 gene." doc = hgnc_linker(nlp(text)) for span in doc.ents: for identifier, score in span._.kb_ents: print(f"{span.text}: {identifier} (score: {score:.3f})") # Output: AKT1: hgnc:391 (score: 1.000) ``` -------------------------------- ### Execute scispacy model training pipeline Source: https://github.com/allenai/scispacy/blob/main/RELEASE.md Runs the full training and packaging pipeline for scispacy models using the spacy project CLI. This command triggers the training process defined in the project configuration. ```bash spacy project run all ``` -------------------------------- ### Initialize and Use AbbreviationDetector (Python) Source: https://github.com/allenai/scispacy/blob/main/README.md Demonstrates how to load a spaCy pipeline, add the AbbreviationDetector component, process a document, and access detected abbreviations and their definitions. Requires the 'scispacy' library and a spaCy model. ```python import spacy from scispacy.abbreviation import AbbreviationDetector nlp = spacy.load("en_core_sci_sm") # Add the abbreviation pipe to the spacy pipeline. nlp.add_pipe("abbreviation_detector") doc = nlp("Spinal and bulbar muscular atrophy (SBMA) is an \ninherited motor neuron disease caused by the expansion \nof a polyglutamine tract within the androgen receptor (AR). \nSBMA can be caused by this easily.") print("Abbreviation", "\t", "Definition") for abrv in doc._.abbreviations: print(f"{abrv} \t ({abrv.start}, {abrv.end}) {abrv._.long_form}") ``` -------------------------------- ### Link Entities using External Ontologies via pyobo Source: https://github.com/allenai/scispacy/blob/main/README.md Illustrates how to integrate external databases like HGNC into scispaCy using pyobo. This allows for linking entities to standard identifiers outside of the default UMLS knowledge base. ```python import pyobo import spacy from scispacy.linking import EntityLinker from tabulate import tabulate linker: EntityLinker = pyobo.get_scispacy_entity_linker("hgnc", filter_for_definitions=False) nlp = spacy.load("en_core_web_sm") text = ( "RAC(Rho family)-alpha serine/threonine-protein kinase " "is an enzyme that in humans is encoded by the AKT1 gene." ) doc = linker(nlp(text)) rows = [ ( span, span.start_char, span.end_char, f"[{identifier}](https://bioregistry.io/{identifier})", score, ) for span in doc.ents for identifier, score in span._.kb_ents ] print(tabulate(rows, headers=["text", "start", "end", "prefix", "identifier"], tablefmt="github")) ``` -------------------------------- ### Retrieve Entity Information from scispaCy Linker Source: https://github.com/allenai/scispacy/blob/main/README.md Demonstrates how to access the scispacy_linker pipe from a spaCy pipeline to retrieve entity details such as CUI, name, and definition from the knowledge base. ```python linker = nlp.get_pipe("scispacy_linker") for umls_ent in entity._.kb_ents: print(linker.kb.cui_to_entity[umls_ent[0]]) ``` -------------------------------- ### Visualize Dependency Parses with scispaCy Source: https://github.com/allenai/scispacy/blob/main/docs/index.md Visualizes the dependency parse of a sentence from a scispaCy processed document using spaCy's displaCy. This function requires the spacy library and is particularly useful within Jupyter notebooks for immediate visual feedback. ```python from spacy import displacy displacy.render(next(doc.sents), style='dep', jupyter=True) ``` -------------------------------- ### Activate Conda Environment Source: https://github.com/allenai/scispacy/blob/main/README.md Activates the 'scispacy' Conda environment. This command needs to be run in each terminal session where SciSpaCy will be used. ```bash mamba activate scispacy ``` -------------------------------- ### Make AbbreviationDetector Serializable (Python) Source: https://github.com/allenai/scispacy/blob/main/README.md Shows how to add the AbbreviationDetector to a spaCy pipeline with the `make_serializable=True` option, which is necessary for serializing doc objects. ```python nlp.add_pipe("abbreviation_detector", config={"make_serializable": True}) ``` -------------------------------- ### Entity Linking with UMLS Knowledge Base in SciSpaCy Source: https://context7.com/allenai/scispacy/llms.txt This code snippet shows how to integrate and use the EntityLinker component in SciSpaCy with the UMLS knowledge base. It demonstrates adding the linker to the spaCy pipeline, processing a document, and examining the linked entities, including their concept IDs, scores, canonical names, definitions, and types. It highlights configuration options like similarity thresholds and filtering for definitions. ```python import spacy from scispacy.linking import EntityLinker nlp = spacy.load("en_core_sci_sm") # Add entity linker with UMLS knowledge base # First run downloads ~1GB of data (cached afterwards) nlp.add_pipe("scispacy_linker", config={ "resolve_abbreviations": True, # Link abbreviations to their long forms "linker_name": "umls", # Knowledge base: 'umls', 'mesh', 'rxnorm', 'go', 'hpo' "threshold": 0.7, # Minimum similarity threshold "max_entities_per_mention": 5, # Maximum linked entities per mention "filter_for_definitions": True # Only return entities with definitions }) doc = nlp("Spinal and bulbar muscular atrophy (SBMA) is an " ""inherited motor neuron disease caused by the expansion " ""of a polyglutamine tract within the androgen receptor (AR).") # Get the linker component for KB lookups linker = nlp.get_pipe("scispacy_linker") # Examine linked entities for entity in doc.ents: print(f"\nEntity: {entity.text}") # kb_ents contains List[Tuple[concept_id, score]] if entity._.kb_ents: for concept_id, score in entity._.kb_ents[:3]: # Top 3 matches # Look up full entity information kb_entity = linker.kb.cui_to_entity[concept_id] print(f" CUI: {concept_id} (score: {score:.3f})") print(f" Name: {kb_entity.canonical_name}") print(f" Definition: {kb_entity.definition[:100] if kb_entity.definition else 'N/A'}...") print(f" Types: {', '.join(kb_entity.types)}") ``` -------------------------------- ### Load and Use scispaCy Model for Text Processing Source: https://github.com/allenai/scispacy/blob/main/docs/index.md Loads a pre-trained scispaCy model (e.g., en_core_sci_sm) using spaCy and processes a given text. It demonstrates sentence segmentation and entity extraction. Dependencies include the scispacy and spacy libraries. ```python import scispacy import spacy nlp = spacy.load("en_core_sci_sm") text = """ Myeloid derived suppressor cells (MDSC) are immature myeloid cells with immunosuppressive activity. They accumulate in tumor-bearing mice and humans with different types of cancer, including hepatocellular carcinoma (HCC). """ doc = nlp(text) print(list(doc.sents)) # >>> ["Myeloid derived suppressor cells (MDSC) are immature myeloid cells with immunosuppressive activity.", # "They accumulate in tumor-bearing mice and humans with different types of cancer, including hepatocellular carcinoma (HCC)."] # Examine the entities extracted by the mention detector. # Note that they don't have types like in SpaCy, and they # are more general (e.g including verbs) - these are any # spans which might be an entity in UMLS, a large # biomedical database. print(doc.ents) # >>> (Myeloid derived suppressor cells, # MDSC, # immature, # myeloid cells, # immunosuppressive activity, # accumulate, # tumor-bearing mice, # humans, # cancer, # hepatocellular carcinoma, # HCC) ``` -------------------------------- ### Build a Comprehensive Biomedical NLP Pipeline Source: https://context7.com/allenai/scispacy/llms.txt Integrates multiple ScispaCy components including abbreviation detection, UMLS entity linking, and hyponym extraction. This pipeline processes biomedical abstracts to resolve abbreviations, link entities to the UMLS knowledge base, and identify taxonomic relationships. ```python import spacy from scispacy.abbreviation import AbbreviationDetector from scispacy.linking import EntityLinker from scispacy.hyponym_detector import HyponymDetector nlp = spacy.load("en_core_sci_sm") nlp.add_pipe("abbreviation_detector") nlp.add_pipe("scispacy_linker", config={"resolve_abbreviations": True, "linker_name": "umls", "threshold": 0.7}) nlp.add_pipe("hyponym_detector", last=True, config={"extended": True}) doc = nlp("Amyotrophic lateral sclerosis (ALS) is a progressive neurodegenerative disease.") # Accessing results for abbr in doc._.abbreviations: print(f"{abbr.text} = {abbr._.long_form}") for ent in doc.ents: if ent._.kb_ents: print(f"Entity: {ent.text}, Match: {ent._.kb_ents[0]}") ``` -------------------------------- ### Detect and Access Abbreviations with SciSpaCy Source: https://context7.com/allenai/scispacy/llms.txt This snippet demonstrates how to use SciSpaCy's abbreviation detector to find abbreviations within a document. It shows how to access the detected abbreviations, their positions, and their long forms. It also illustrates how to configure the detector for serialization, returning dictionaries instead of Span objects. ```python import spacy nlp = spacy.load("en_core_sci_sm") nlp.add_pipe("abbreviation_detector", config={"make_serializable": False}) doc = nlp("The World Health Organization (WHO) issued guidelines.") print("Detected Abbreviations:") print("-" * 60) for abrv in doc._.abbreviations: print(f"Abbreviation: {abrv.text}") print(f" Position: ({abrv.start}, {abrv.end})") print(f" Long form: {abrv._.long_form}") print() # For serialization support (e.g., multiprocessing), use make_serializable=True nlp2 = spacy.load("en_core_sci_sm") nlp2.add_pipe("abbreviation_detector", config={"make_serializable": True}) doc2 = nlp2("The World Health Organization (WHO) issued guidelines.") # Serializable format returns dictionaries instead of Span objects for abbr in doc2._.abbreviations: print(f"Short: {abbr['short_text']}, Long: {abbr['long_text']}") ``` -------------------------------- ### Load and Use ScispaCy Model for Biomedical NLP (Python) Source: https://context7.com/allenai/scispacy/llms.txt Loads a scispaCy model using spaCy's load function and processes biomedical text. It demonstrates accessing sentence boundaries, detected entities, and token-level linguistic features like POS tags and dependencies. ```python import spacy # Load a scispaCy model nlp = spacy.load("en_core_sci_sm") # Process biomedical text text = """ Myeloid derived suppressor cells (MDSC) are immature myeloid cells with immunosuppressive activity. They accumulate in tumor-bearing mice and humans with different types of cancer, including hepatocellular carcinoma (HCC). """ doc = nlp(text) # Access sentence boundaries print("Sentences:") for sent in doc.sents: print(f" - {sent.text.strip()}") # Access detected entities (biomedical mentions) print("\nEntities:") for ent in doc.ents: print(f" - {ent.text}") # Access token-level information print("\nToken analysis:") for token in list(doc)[:10]: print(f" {token.text:20} POS: {token.pos_:6} DEP: {token.dep_:10} HEAD: {token.head.text}") ``` -------------------------------- ### Query Biomedical Knowledge Bases Source: https://context7.com/allenai/scispacy/llms.txt Utilizes ScispaCy utility classes to interface with knowledge bases like UMLS. This allows for looking up entities by CUI, finding CUIs from aliases, and navigating semantic type trees. ```python from scispacy.linking_utils import UmlsKnowledgeBase kb = UmlsKnowledgeBase() # Access entity by CUI entity = kb.cui_to_entity.get("C0002736") print(f"Name: {entity.canonical_name}") # Find CUIs for an alias matching_cuis = kb.alias_to_cuis.get("heart attack", set()) # Navigate semantic tree tree = kb.semantic_type_tree disease_types = tree.get_descendants("T047") ``` -------------------------------- ### Perform Biomedical Named Entity Recognition with ScispaCy Source: https://context7.com/allenai/scispacy/llms.txt Demonstrates loading specialized biomedical models (CRAFT, JNLPBA, BIONLP13CG) to extract entities such as genes, proteins, chemicals, and diseases from text. Each model is loaded via spacy.load and applied to a document to iterate through detected entities and their labels. ```python # CRAFT model: Genes, Species, Chemicals, Cell types, GO terms nlp_craft = spacy.load("en_ner_craft_md") doc = nlp_craft("The BRCA1 gene in Homo sapiens regulates DNA repair mechanisms.") for ent in doc.ents: print(f"{ent.text:30} -> {ent.label_}") # JNLPBA model: Molecular biology entities nlp_jnlpba = spacy.load("en_ner_jnlpba_md") doc = nlp_jnlpba("The p53 protein binds to DNA and regulates cell cycle arrest.") for ent in doc.ents: print(f"{ent.text:30} -> {ent.label_}") # BIONLP13CG model: Cancer genetics entities nlp_bionlp = spacy.load("en_ner_bionlp13cg_md") doc = nlp_bionlp("Tumor cells in the liver exhibit increased EGFR expression.") for ent in doc.ents: print(f"{ent.text:30} -> {ent.label_}") ``` -------------------------------- ### Custom Tokenizer for Biomedical Text in Python Source: https://context7.com/allenai/scispacy/llms.txt Introduces SciSpaCy's custom tokenizer, optimized for biomedical text, which handles scientific notation, chemical formulas, and abbreviations better than standard tokenizers. It also includes a utility to clean newlines. ```python import spacy from scispacy.custom_tokenizer import combined_rule_tokenizer, remove_new_lines # Load model with default tokenizer nlp = spacy.load("en_core_sci_sm") # Replace with custom biomedical tokenizer nlp.tokenizer = combined_rule_tokenizer(nlp) # Preprocess text to handle line breaks in scientific documents raw_text = """The compound 5-HT2A recep- receptor antagonist was administered at 10mg/kg.""" cleaned_text = remove_new_lines(raw_text) print(f"Cleaned: {cleaned_text}") # Output: Cleaned: The compound 5-HT2A receptor antagonist was administered at 10mg/kg. doc = nlp(cleaned_text) # Tokenization handles scientific terms appropriately print("Tokens:") for token in doc: print(f" '{token.text}'") # The custom tokenizer preserves compound terms like "5-HT2A" and handles # unit expressions like "10mg/kg" appropriately ``` -------------------------------- ### Extract Hyponyms with HyponymDetector Source: https://github.com/allenai/scispacy/blob/main/README.md Shows how to add the HyponymDetector component to a spaCy pipeline to identify hyponymy relations in text. The detector populates the doc._.hearst_patterns attribute with extracted concept pairs. ```python import spacy from scispacy.hyponym_detector import HyponymDetector nlp = spacy.load("en_core_sci_sm") nlp.add_pipe("hyponym_detector", last=True, config={"extended": False}) doc = nlp("Keystone plant species such as fig trees are good for the soil.") print(doc._.hearst_patterns) ``` -------------------------------- ### HyponymDetector for Extracting Is-A Relationships in Python Source: https://context7.com/allenai/scispacy/llms.txt Explains how to use the HyponymDetector component to identify hyponym-hypernym (is-a) relationships in text using Hearst patterns. It can be configured with extended patterns for higher recall. ```python import spacy from scispacy.hyponym_detector import HyponymDetector nlp = spacy.load("en_core_sci_sm") # Add hyponym detector (extended=True for more patterns with higher recall) nlp.add_pipe("hyponym_detector", last=True, config={"extended": False}) # Process text with taxonomic relationships doc = nlp("Keystone plant species such as fig trees are good for the soil.") # Access detected hyponym patterns print("Hearst Patterns:") for pattern_name, hypernym, hyponym in doc._.hearst_patterns: print(f" Pattern: {pattern_name}") print(f" Hypernym (general): {hypernym}") print(f" Hyponym (specific): {hyponym}") print() # Output: # Hearst Patterns: # Pattern: such_as # Hypernym (general): Keystone plant species # Hyponym (specific): fig trees # Extended patterns example nlp_extended = spacy.load("en_core_sci_sm") nlp_extended.add_pipe("hyponym_detector", last=True, config={"extended": True}) text = """ Cardiovascular diseases, especially heart failure, are common in elderly patients. Treatments include ACE inhibitors and other antihypertensive medications. Beta blockers, for example metoprolol, reduce heart rate. """ doc = nlp_extended(text) print("Extended Pattern Detection:") for pattern_name, hypernym, hyponym in doc._.hearst_patterns: print(f" {hypernym} -> {hyponym} [{pattern_name}]") # Supported base patterns: such_as, include, especially, other # Extended patterns add: which_may_include, for_example, e.g., type, mainly, # particularly, be_a, kind_of, form_of, and many more ``` -------------------------------- ### ScispaCy Abbreviation Detector (Python) Source: https://context7.com/allenai/scispacy/llms.txt Adds the AbbreviationDetector component to a scispaCy pipeline to identify abbreviation definitions using the Schwartz-Hearst algorithm. It processes text containing abbreviations and their definitions, storing results in `doc._.abbreviations`. ```python import spacy from scispacy.abbreviation import AbbreviationDetector nlp = spacy.load("en_core_sci_sm") # Add the abbreviation detector to the pipeline nlp.add_pipe("abbreviation_detector") # Process text with abbreviations doc = nlp("Spinal and bulbar muscular atrophy (SBMA) is an " "inherited motor neuron disease caused by the expansion " "of a polyglutamine tract within the androgen receptor (AR). " "SBMA can be caused by this easily.") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.