Skip to main content

A Python wrapper for Stanford CoreNLP's SUTime

Project description

sutime

Python wrapper for Stanford CoreNLP's SUTime Java library

Build Status

CircleCI

Introduction

This library provides a simple access to SUTime functions in Python.

Installation

>> pip install sutime
>> # use package pom.xml to install all Java dependencies via Maven into ./jars
>> mvn dependency:copy-dependencies -DoutputDirectory=./jars

Example

import os
import json
from sutime import SUTime

if __name__ == '__main__':
    test_case = u'I need a desk for tomorrow from 2pm to 3pm'

    jar_files = os.path.join(os.path.dirname(__file__), 'jars')
    sutime = SUTime(jars=jar_files, mark_time_ranges=True)

    print(json.dumps(sutime.parse(test_case), sort_keys=True, indent=4))

Result:

[
    {
        "end": 26,
        "start": 18,
        "text": "tomorrow",
        "type": "DATE",
        "value": "2016-10-14"
    },
    {
        "end": 42,
        "start": 27,
        "text": "from 2pm to 3pm",
        "type": "DURATION",
        "value": {
            "begin": "T14:00",
            "end": "T15:00"
        }
    }
]

Other examples can be found in the test directory.

Functions

SUTime(jars=None, jvm_started=False, mark_time_ranges=False, include_range=False, jvm_flags=None)
    """
    jars: List of paths to the SUTime Java dependencies.
    jvm_started: Optional attribute to specify if the JVM has already been
        started (with all Java dependencies loaded).
    mark_time_ranges: Optional attribute to specify CoreNLP property
        sutime.markTimeRanges. Default is False.
        "Tells sutime to mark phrases such as 'From January to March'
        instead of marking 'January' and 'March' separately"
    include_range: Optional attribute to specify CoreNLP property
        sutime.includeRange. Default is False.
        "Tells sutime to mark phrases such as 'From January to March'
        instead of marking 'January' and 'March' separately"
    jvm_flags: Optional attribute to specify an iterable of string flags
        to be provided to the JVM at startup. For example, this may be
        used to specify the maximum heap size using '-Xmx'. Has no effect
        if jvm_started is set to True. Default is None.
    """

sutime.parse(input_str, reference_date=''):
    """Parses datetime information out of string input.

    It invokes the SUTimeWrapper.annotate() function in Java.

    Args:
        input_str: The input as string that has to be parsed.
        reference_date: Optional reference data for SUTime.

    Returns:
        A list of dicts with the result from the SUTimeWrapper.annotate()
            call.

    Raises:
        RuntimeError: An error occurres when CoreNLP is not loaded.
    """

Credit

Contributions

  • René Springer: Support for reference date
  • Constantine Lignos: Support for JVM flags, adoption of CircleCI 2.0, fix for mutable default argument, fix for test execution

License

  • GPLv3+ (check the LICENSE file)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

sutime-1.0.0rc2-py2.py3-none-any.whl (22.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sutime-1.0.0rc2-py2.py3-none-any.whl.

File metadata

  • Download URL: sutime-1.0.0rc2-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for sutime-1.0.0rc2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 80f3c1a7c061b01b6bd61855195013ac6f5ca1f2d2748777be3f79f9c3cd480c
MD5 19d31a39ccfa8a7e62f14dca0d90802a
BLAKE2b-256 87ec37c20b4e60c0a4dc670678b7a06c1057e32935f6d7942e4e928069ccad56

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