Skip to main content

A Python wrapper for Stanford CoreNLP's SUTime

Project description

sutime

Python wrapper for Stanford CoreNLP's SUTime Java library.
Docs are also hosted on GitHub Pages.

Build Status

Travis CI Builds

Travis CI

PyPI

PyPI Version PyPI Downloads

Code Quality

Codacy Badge Scrutinizer Coverity Scan Code Climate maintainability CodeFactor Requirements Status

Installation

>> # Ideally, create a virtual environment before installing any dependencies
>> pip install sutime
>> # Install Java dependencies
>> mvn dependency:copy-dependencies -DoutputDirectory=./jars -f $(python3 -c 'import importlib; import pathlib; print(pathlib.Path(importlib.util.find_spec("sutime").origin).parent / "pom.xml")')

Append -P spanish to the mvn command to include the Spanish language model.

Supported Languages

SUTime currently supports only English, British and Spanish (Source). This Python wrapper is prepared to support the other CoreNLP languages (e.g. German) as well as soon as they get added to SUTime. The following command can be used to download the language models for arabic, chinese, english, french, german, and spanish:

>> mvn dependency:copy-dependencies -DoutputDirectory=./jars -f $(python -c 'import importlib; import pathlib; print(pathlib.Path(importlib.util.find_spec("sutime").origin).parent / "pom.xml")') -P <language>

However, SUTime only supports a subset (default model and spanish) of CoreNLP's languages and the other language models will get ignored.

Example

import json
from sutime import SUTime

if __name__ == '__main__':
    test_case = 'I need a desk for tomorrow from 2pm to 3pm'
    sutime = SUTime(mark_time_ranges=True, include_range=True)
    print(json.dumps(sutime.parse(test_case), sort_keys=True, indent=4))

Result:

[
    {
        "end": 26,
        "start": 18,
        "text": "tomorrow",
        "timex-value": "2020-11-03",
        "type": "DATE",
        "value": "2020-11-03"
    },
    {
        "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: Optional[str] = None,
    jvm_started: Optional[bool] = False,
    mark_time_ranges: Optional[bool] = False,
    include_range: Optional[bool] = False,
    jvm_flags: Optional[List[str]] = None,
    language: Optional[str] = 'english',
):
    """
    Args:
        jars (Optional[str]): Path to previously downloaded SUTime Java
            dependencies. Defaults to False.
        jvm_started (Optional[bool]): Flag to indicate that JVM has been
            already started (with all Java dependencies loaded). Defaults
            to False.
        mark_time_ranges (Optional[bool]): SUTime flag for
            sutime.markTimeRanges. Defaults to False.
            "Whether or not to recognize time ranges such as 'July to
            August'"
        include_range (Optional[bool]): SUTime flag for
            sutime.includeRange. Defaults to False.
            "Whether or not to add range info to the TIMEX3 object"
        jvm_flags (Optional[List[str]]): List of flags passed to JVM. For
            example, this may be used to specify the maximum heap size
            using '-Xmx'. Has no effect if `jvm_started` is set to True.
            Defaults to None.
        language (Optional[str]): Selected language. Currently supported
            are: english (/en), british, spanish (/es). Defaults to
            `english`.
    """

sutime.parse(input_str: str, reference_date: Optional[str] = '') -> List[Dict]:
    """Parse datetime information out of string input.

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

    Args:
        input_str (str): The input as string that has to be parsed.
        reference_date (Optional[str]): Optional reference data for SUTime.
            Defaults to `''`.

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

    Raises:
        RuntimeError: An error occurs 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
  • Cole Robertson: Updated instructions, JAR requirements, and SUTime JAR imports
  • Ludovico Pestarino: Modified json.loads for compatibility with json data (string handling)

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 Distribution

sutime-1.0.1.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

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

sutime-1.0.1-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file sutime-1.0.1.tar.gz.

File metadata

  • Download URL: sutime-1.0.1.tar.gz
  • Upload date:
  • Size: 29.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.7

File hashes

Hashes for sutime-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1f2f653ffe9f74b5fb48e5d01ff3a8cc9307592d280f95f137221f904d815b35
MD5 0a2521a81c2c2adc15984ac75822e355
BLAKE2b-256 c6c9ddce2da756db37e990379efadf17e3117364a63c7ca97bc50a6824d5f0f0

See more details on using hashes here.

File details

Details for the file sutime-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: sutime-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.7

File hashes

Hashes for sutime-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37f8ed813ec4be03b8fd7ee7993be1d0ce6b5e25a4b1bf842022fda6a3f06884
MD5 d03b034f5701f192cdf40d3633dcd7eb
BLAKE2b-256 74e73ce0509d822392fc2466db5c1780c25c19d69a49c6c6ce3c9b65701ec604

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