Git Product home page Git Product logo

spacy-graphql's Introduction

spacy-graphql

A very simple and experimental app that lets you query spaCy's linguistic annotations using GraphQL. It's my first ever experiment with GraphQL, so it's probably not as elegant as it could be.

The API currently supports most token attributes, named entities, sentences and text categories (if available as doc.cats, i.e. if you added a text classifier to a model). The meta field will return the model meta data. Models are only loaded once and kept in memory.

It currently doesn't do anything particularly clever, so regardless of your query, the full model pipeline will always be applied, even if you only need the token texts. Similarly, if you only request entities, the document will still be tagged and parsed.

Installation & Usage

To try it out, clone this repo and install the dependencies. By default, the en_core_web_sm model will be preinstalled. Note that the API requires Python 3.5 or higher.

git clone https://github.com/ines/spacy-graphql
cd spacy-graphql
pip install -r requirements.txt
# optional: install more spaCy models

Executing the app.py will start the server:

python app.py

You can use the SPACY_HOST and SPACY_PORT environment variables to change the host and port. By default, the API is served on localhost:8080.

If you navigate to the URL in your browser, you can explore the API interactively using GraphiQL. It also shows the complete documentation for the available fields.

GraphiQL

Example query

Both the text and model argument are required. The value of model is passed to spacy.load, so you'll be able to load any model that's installed in the same environment out-of-the-box.

{
  nlp(text: "Zuckerberg is the CEO of Facebook.", model: "en_core_web_sm") {
    meta {
      lang
      description
    }
    doc {
      text
      tokens {
        text
        pos_
      }
      ents {
        text
        label_
      }
    }
  }
}

Example Response

{
  "data": {
    "nlp": {
      "meta": {
        "lang": "en",
        "description": "English multi-task CNN trained on OntoNotes, with GloVe vectors trained on Common Crawl. Assigns word vectors, context-specific token vectors, POS tags, dependency parse and named entities."
      },
      "doc": {
        "text": "Zuckerberg is the CEO of Facebook.",
        "tokens": [
          {
            "text": "Zuckerberg",
            "pos_": "PROPN"
          },
          {
            "text": "is",
            "pos_": "VERB"
          },
          {
            "text": "the",
            "pos_": "DET"
          },
          {
            "text": "CEO",
            "pos_": "NOUN"
          },
          {
            "text": "of",
            "pos_": "ADP"
          },
          {
            "text": "Facebook",
            "pos_": "PROPN"
          },
          {
            "text": ".",
            "pos_": "PUNCT"
          }
        ],
        "ents": [
          {
            "text": "Zuckerberg",
            "label_": "PERSON"
          },
          {
            "text": "Facebook",
            "label_": "ORG"
          }
        ]
      }
    }
  }
}

spacy-graphql's People

Contributors

ines avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.