Skip to content

Commit

Permalink
add pipe loader
Browse files Browse the repository at this point in the history
  • Loading branch information
HLasse committed Jul 26, 2021
1 parent 1cf1527 commit 43f3a5b
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions textdescriptives/load_components.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Adds components to a spaCy pipeline"""
"""Adds all components to a spaCy pipeline"""
from components import (
create_readability_component,
create_dependency_distance_component,
Expand All @@ -9,9 +9,8 @@
from spacy.tokens import Doc


@Language.component("textdescriptives")
def textdescriptives_component(nlp: Language, name: str):
"""Add all components to a spaCy pipeline"""
@Language.factory("textdescriptives")
def create_textdescriptives_component(nlp: Language, name: str):
for component in [
"descriptive_stats",
"readability",
Expand All @@ -22,33 +21,26 @@ def textdescriptives_component(nlp: Language, name: str):


class TextDescriptives:
"""
Utility spaCy v3.0 component to add all functionality from the TextDescriptives
package to a `Doc` objects. See `DescriptiveStatistics`, `Readability`, and `DependencyDistance`
for more details.
USAGE:
>>> import spacy
>>> from textdescriptives import TextDescriptives
>>> nlp = spacy.load("en_core_web_sm")
>>> nlp.add_pipe("textdescriptives")
>>> doc = nlp("This is a the first sentence. Here's a second, slightly longer one.")
>>> doc._.dependency_distance
>>> doc._.readability
>>> doc[0:4]._.token_length
"""

def __init__(self, nlp: Language):
"""Don't do anything except initialise the other components"""
"""Don't do anything, just return the Doc"""

def __call__(self, doc: Doc):
"""Run the pipeline component"""
return doc


# def add_components(nlp: Language):
# """Add all components to a spaCy pipeline"""
# for component in [
# "utilities",
# "descriptive_stats",
# "readability",
# "dependency_distance",
# ]:
# nlp.add_pipe(component, last=True)
# return nlp


import spacy

nlp = spacy.load("da_core_news_sm")
nlp = nlp.add_pipe("textdescriptives")
nlp.add_pipe("descriptive_stats")
doc = nlp("Det her er en fin lille sætning. Ja, den er bare rigtig sød.")
print(doc._.counts)
print(doc[0:4]._.token_length)
print(doc._.readability)
print(doc._.dependency_distance)

0 comments on commit 43f3a5b

Please sign in to comment.