Skip to content

Instantly share code, notes, and snippets.

@chiefastro
chiefastro / singleton_multiprocessing_success_pass_arg.py
Created Dec 5, 2020
Fix errors thrown by singleton during multiprocessing in python by passing as argument
View singleton_multiprocessing_success_pass_arg.py
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 Dec 5, 2020
Fix errors thrown by singleton during multiprocessing in python with environment variables
View singleton_multiprocessing_success_env_vars.py
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_error.py
Created Dec 5, 2020
Running a singleton with multiprocessing in python throws error.
View singleton_multiprocessing_error.py
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 / custom_ner.py
Created Nov 30, 2020
Custom entity tagging with spaCy's EntityRuler
View custom_ner.py
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 / parent_verb_polarity.py
Last active Nov 30, 2020
Get parent verb polarity with spaCy
View parent_verb_polarity.py
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 / token_meta.py
Created Nov 30, 2020
Display token meta with spacy
View token_meta.py
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 / ner_spacy.py
Last active Nov 30, 2020
Visualizing named entity recognition with spaCy and displaCy
View ner_spacy.py
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 / visualize_dependency_parse_displacy.py
Last active Nov 30, 2020
Visualize a parsed dependency tree with displaCy
View visualize_dependency_parse_displacy.py
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 / spacy_pipeline.py
Last active Nov 30, 2020
Linguistics in one line of code with spaCy
View spacy_pipeline.py
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