Skip to main content

spanner nlp

Project description

Stanford CoreNLP Python Wrapper

Python wrapper for Stanford CoreNLP that interfaces with the Stanford CoreNLP server. It provides a simple API for text processing tasks such as Tokenization, Part of Speech Tagging, Named Entity Reconigtion, Constituency Parsing, Dependency Parsing, as described in the Full List Of Annotators.

Prerequisites

  • Java 1.8+ (Download Page). You can check java version with the command: java -version.
  • Python 3.6+ (Download Page). You can check python version with the command: python --version.
  • Stanford CoreNLP files version 4.1.0 (Download Page).

Usage

Annotators wrapper - Simple Usage - Using local files

This example will demonstrate how to use the annotators wrapper using the local files downloded from Stanford CoreNLP.
All the annotators and their information can be found in Stanford CoreNLP Full List Of Annotators.

from StanfordCoreNLP import StanfordCoreNLP

with StanfordCoreNLP('stanford-corenlp-4.1.0') as nlp:
    print('Tokenize:', nlp.tokenize("Hello world. Hello world again."))
    print('Sentence Splitting:', nlp.ssplit("Hello world. Hello world again."))
    print('Part of Speech:', nlp.pos("Marie was born in Paris."))

Example output Tokenize:

Tokenize: [
    {
        "token": "Hello",
        "span": [
            0,
            5
        ]
    },
    {
        "token": "world",
        "span": [
            6,
            11
        ]
    },
    ...

Example output Sentence Splitting:

Sentence Splitting: [
    "Hello world.",
    "Hello world again."
]

Example output Part of Speech:

Part of Speech: [
    {
        "token": "Marie",
        "pos": "NNP",
        "span": [
            0,
            5
        ]
    },
    {
        "token": "was",
        "pos": "VBD",
        "span": [
            6,
            9
        ]
    },
    ...

Manual Annotators

The examples below will demonstrate how to define annotators Manualy using local files or using existing server.

Properties for using manual annotators:

Manual Annotators - Using local files

from StanfordCoreNLP import StanfordCoreNLP

nlp = StanfordCoreNLP('stanford-corenlp-4.1.0')
text = 'The small red car turned very quickly around the corner.'
pros = {'annotators' : 'ner', 'pinelineLanguage' : 'en', 'outputFormat' : 'xml'} #Named Entity Recognition example
print(nlp.annotate(text, properties = pros))
nlp.close()

Example output:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="CoreNLP-to-HTML.xsl" type="text/xsl"?>
<root>
  <document>
    <sentences>
      <sentence id="1">
        <tokens>
          <token id="1">
            <word>The</word>
            <lemma>the</lemma>
            <CharacterOffsetBegin>0</CharacterOffsetBegin>
            <CharacterOffsetEnd>3</CharacterOffsetEnd>
            <POS>DT</POS>
            <NER>O</NER>
          </token>
          <token id="2">
           ...

Manual Annotators - Using existing server

from StanfordCoreNLP import StanfordCoreNLP

nlp = StanfordCoreNLP('http://corenlp.run', port = 80)
text = 'Joe Smith lives in California. He used to live in Oregon.'
pros = {'annotators' : 'lemma', 'pinelineLanguage' : 'en', 'outputFormat' : 'JSON'} #Lemmatization example
print(nlp.annotate(text, properties = pros))
nlp.close()

Example output:

{
  "sentences": [
    {
      "index": 0,
      "tokens": [
        {
          "index": 1,
          "word": "Joe",
          "originalText": "Joe",
          "lemma": "Joe",
          "characterOffsetBegin": 0,
          "characterOffsetEnd": 3,
          "pos": "NNP",
          "before": "",
          "after": " "
        },
        {
          "index": 2,
           ...

Manual Annotators - Support a number of annotators at the same time - Using local files

Note: This example also support using existing server.

from StanfordCoreNLP import StanfordCoreNLP

nlp = StanfordCoreNLP('stanford-corenlp-4.1.0', lang = 'en')
text = 'Joe Smith lives in California. He used to live in Oregon.'
pros = {'annotators' : 'tokenize, ssplit, pos', 'pinelineLanguage' : 'en', 'outputFormat' : 'JSON'}
print(nlp.annotate(text, pros, True))
nlp.close()

Example output:

{
    "tokenize": [
        {
            "token": "Joe",
            "span": [
                0,
                3
            ]
        },
        {
            "token": "Smith",
            "span": [
                4,
                9
            ]
        },
        {
            "token": "lives",
            "span": [
                10,
                15
            ]
        },
        {
            "token": "in",
            "span": [
                16,
                18
            ]
        },
        {
            "token": "California",
            "span": [
                19,
                29
            ]
        },
        ...

Debug

You can debug using the logging module in python. This example will demonstrate how to use the logging module:

from StanfordCoreNLP import StanfordCoreNLP
import logging

nlp = StanfordCoreNLP('stanford-corenlp-4.1.0', quiet = False, loggingLevel = logging.DEBUG)
text = 'The small red car turned very quickly around the corner.'
print(nlp.annotate(text)) #default annotate
nlp.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spanner-nlp-0.0.6.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spanner_nlp-0.0.6-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file spanner-nlp-0.0.6.tar.gz.

File metadata

  • Download URL: spanner-nlp-0.0.6.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6

File hashes

Hashes for spanner-nlp-0.0.6.tar.gz
Algorithm Hash digest
SHA256 5a65e6d4f41b8ca58a61f0a00828188fd85df3e367256f257fabaf76ec63d5da
MD5 15f9eec8d385f3528cdcb947a17fbb9f
BLAKE2b-256 bf7447511c88b1d546f0dccac85d0da36e3d62f4e0c76b25b7cc7b58eb031e89

See more details on using hashes here.

File details

Details for the file spanner_nlp-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: spanner_nlp-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6

File hashes

Hashes for spanner_nlp-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 78362d539fb5b3d00083fad60ba6d4b741848840b932d7d6b6b37161b445b43a
MD5 376db867a8b8beae20ac8430904f0a25
BLAKE2b-256 ac7e4768ee9c82d17d0502bb19323d5e2cf5756d408f19acf41ba840637ce8ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page