Skip to content

kootenpv/spacy_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spacy_api

Helps with loading models in a separate, dedicated process.

Caching happens on unique arguments.

Features

  • ✓ Serve models separately
  • ✓ Client- and Server-side caching
  • ✓ CLI interface

Install

Should work with py2 and py3.

Assumes you have installed spacy.

Install:

pip install spacy_api[all]

Example

Run the server:

spacy serve

Then open a python process and run code in the next section.

Single document

from spacy_api import Client

spacy_client = Client() # default args host/port

doc = spacy_client.single("How are you")
doc
# [[How, are, you]]

# iterate over sentences
for sentence in doc.sents:
    for token in sentence:
        print(token.text, token.pos_, token.lemma_)

# iterate over a whole document
for token in doc:
    print(token)

Switch to running spacy within the process

Instead of

from spacy_api import Client

use

from spacy_api import LocalClient

Arguments

LocalClient/Client:

# language/model
spacy_client = Client(model="en")

# Using google pretrained vectors
spacy_client = Client(embeddings_path="en_google")

To make a call:

# Tell spacy which attributes to give back, comma separated
spacy_client.single("How are you", attributes="text,lemma_,pos,vector")

Naturally, you can use any combination of these.

Bulk of documents

docs = spacy_client.bulk(["How are you"]*100)
for doc in docs:
    for sentence in doc.sents:
        for token in sentence:
            print(token.text, token.pos_, token.lemma_)

About

Server/Client around Spacy to load spacy only once

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages