Skip to content

Instantly share code, notes, and snippets.

@chiefastro
chiefastro / spacy_pipeline.py
Last active November 30, 2020 01:59
Linguistics in one line of code with spaCy
import spacy
# make sure to download the model first
# use either the CLI or this line of code
spacy.cli.download('en_core_web_sm')
# load pre-trained model pipeline
nlp = spacy.load('en_core_web_sm')
# text document
@chiefastro
chiefastro / visualize_dependency_parse_displacy.py
Last active November 30, 2020 02:33
Visualize a parsed dependency tree with displaCy
from spacy import displacy
import spacy
# load pre-trained model pipeline
nlp = spacy.load('en_core_web_sm')
# text document
text = 'spaCy is pretty cool.'
# apply pipeline
@chiefastro
chiefastro / ner_spacy.py
Last active November 30, 2020 02:33
Visualizing named entity recognition with spaCy and displaCy
from spacy import displacy
import spacy
# load pre-trained model pipeline
nlp = spacy.load('en_core_web_sm')
# sentence with entities
text = """In 2015, independent researchers from Emory University and Yahoo! Labs showed that spaCy offered the fastest syntactic parser in the world and that its accuracy was within 1% of the best available."""
# apply pipeline
@chiefastro
chiefastro / token_meta.py
Created November 30, 2020 03:04
Display token meta with spacy
import pandas as pd
import spacy
from spacy import displacy
# load pre-trained model pipeline
nlp = spacy.load('en_core_web_sm')
# sentence for grammar rules
text = """He does not eat meat, but he loves Beyond Burgers."""
@chiefastro
chiefastro / parent_verb_polarity.py
Last active November 30, 2020 03:44
Get parent verb polarity with spaCy
import typing as t
import spacy
# load pre-trained model pipeline
nlp = spacy.load('en_core_web_sm')
# sentence for grammar rules
text = """He does not eat meat, but he loves Beyond Burgers."""
# apply pipeline
@chiefastro
chiefastro / custom_ner.py
Created November 30, 2020 04:27
Custom entity tagging with spaCy's EntityRuler
import spacy
from spacy.pipeline import EntityRuler
from spacy import displacy
# load pre-trained model pipeline
nlp = spacy.load('en_core_web_sm')
# sentence for grammar rules
text = """He does not eat meat, but he loves Beyond Burgers."""
@chiefastro
chiefastro / singleton_multiprocessing_error.py
Created December 5, 2020 05:30
Running a singleton with multiprocessing in python throws error.
import time
from joblib import Parallel, delayed
class OnlyOne:
"""Singleton Class, inspired by
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html"""
class __OnlyOne:
def __init__(self, arg):
if arg is None:
raise ValueError("Pretend empty instantiation breaks code")
@chiefastro
chiefastro / singleton_multiprocessing_success_env_vars.py
Last active December 5, 2020 06:04
Fix errors thrown by singleton during multiprocessing in python with environment variables
import time
from joblib import Parallel, delayed
import os
class OnlyOne:
"""Singleton Class, inspired by
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html
Modified to work with parallel processes using environment
variables to store state across processes.
@chiefastro
chiefastro / singleton_multiprocessing_success_pass_arg.py
Created December 5, 2020 06:06
Fix errors thrown by singleton during multiprocessing in python by passing as argument
import time
from joblib import Parallel, delayed
class OnlyOne:
"""Singleton Class, inspired by
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html"""
class __OnlyOne:
def __init__(self, arg):
if arg is None:
raise ValueError("Pretend empty instantiation breaks code")