Skip to main content

Parsing and storing NIF information

Project description

nifWrapper

The NLP Interchange Format (NIF) is a RDF/OWL-based format wich allows the spotting of words from text corpora and its metada such as part-of-speech tags, knowledge-base links, entity type, etc. Likewise other Python libraries (e.g., pynif), this library transform NIF data to python classes in order to better proccessing this information.

If you want to create/visualizate NIF data see NIFify in GitHub or a demo.

Example

Install first the library:

pip install nifwrapper

and then, try:

from nifwrapper import *


gold_ttl = '''
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix nif: <http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .
@prefix itsrdf: <http://www.w3.org/2005/11/its/rdf#> .

<https://example.org/doc1>
        a nif:String , nif:Context  , nif:RFC5147String ;
        nif:isString """Kiev is an important industrial, scientific, educational and cultural center of Eastern Europe."""^^xsd:string ;
        nif:beginIndex "0"^^xsd:nonNegativeInteger ;
        nif:endIndex "95"^^xsd:nonNegativeInteger ;
        nif:sourceUrl <https://example.org/doc1> .

<https://example.org/doc1#char=0,95>
        a nif:String , nif:Context , nif:RFC5147String ;
        nif:isString """Kiev is an important industrial, scientific, educational and cultural center of Eastern Europe."""^^xsd:string ;
        nif:beginIndex "0"^^xsd:nonNegativeInteger ;
        nif:endIndex "95"^^xsd:nonNegativeInteger ;
        nif:broaderContext <https://example.org/doc1> .

<https://example.org/doc1#char=0,4>
        a nif:String , nif:Context , nif:Phrase , nif:RFC5147String ;
        nif:referenceContext <https://example.org/doc1#char=0,95> ;
        nif:context <https://example.org/doc1> ;
        nif:anchorOf """Kiev"""^^xsd:string ;
        nif:beginIndex "0"^^xsd:nonNegativeInteger ;
        nif:endIndex "4"^^xsd:nonNegativeInteger ;
        itsrdf:taIdentRef <https://en.wikipedia.org/wiki/Kiev> .

<https://example.org/doc1#char=33,43>
        a nif:String , nif:Context , nif:Phrase , nif:RFC5147String ;
        nif:referenceContext <https://example.org/doc1#char=0,95> ;
        nif:context <https://example.org/doc1> ;
        nif:anchorOf """scientific"""^^xsd:string ;
        nif:beginIndex "33"^^xsd:nonNegativeInteger ;
        nif:endIndex "43"^^xsd:nonNegativeInteger ;
        itsrdf:taIdentRef <https://en.wikipedia.org/wiki/Education> .

<https://example.org/doc1#char=45,56>
        a nif:String , nif:Context , nif:Phrase , nif:RFC5147String ;
        nif:referenceContext <https://example.org/doc1#char=0,95> ;
        nif:context <https://example.org/doc1> ;
        nif:anchorOf """educational"""^^xsd:string ;
        nif:beginIndex "45"^^xsd:nonNegativeInteger ;
        nif:endIndex "56"^^xsd:nonNegativeInteger ;
        itsrdf:taIdentRef <https://en.wikipedia.org/wiki/Education> .

<https://example.org/doc1#char=80,94>
        a nif:String , nif:Context , nif:Phrase , nif:RFC5147String ;
        nif:referenceContext <https://example.org/doc1#char=0,95> ;
        nif:context <https://example.org/doc1> ;
        nif:anchorOf """Eastern Europe"""^^xsd:string ;
        nif:beginIndex "80"^^xsd:nonNegativeInteger ;
        nif:endIndex "94"^^xsd:nonNegativeInteger ;
        itsrdf:taIdentRef <https://en.wikipedia.org/wiki/Eastern_Europe> .
'''

## ---- parsing
parser = NIFParser()
wrp_gold = parser.parser_turtle(gold_ttl)

## ---- displaying turtle format
print(wrp_gold.toString()) 

## --- Benchmark

# - inline NIF corpus creation
wrp_sys = NIFWrapper()
doc = NIFDocument("https://example.org/doc1")
#--
sent = NIFSentence("https://example.org/doc1#char=0,95")
sent.addAttribute("nif:isString","Kiev is an important industrial, scientific, educational and cultural center of Eastern Europe.","xsd:string")
sent.addAttribute("nif:broaderContext",["https://example.org/doc1"],"URI LIST")

#-- 
a1 = NIFAnnotation("https://example.org/doc1#char=0,4", "0", "4", ["https://en.wikipedia.org/wiki/Kiev"], ["dbo:Place"])
a1.addAttribute("nif:anchorOf","Kiev","xsd:string")
a1.addAttribute("ex:newPredicate","This is a test","xsd:string")
sent.pushAnnotation(a1)

#--
a2 = NIFAnnotation("https://example.org/doc1#char=45,56", "45", "56", ["https://en.wikipedia.org/wiki/University"], ["dbo:Organization"])
a2.addAttribute("nif:anchorOf","educational","xsd:string")
sent.pushAnnotation(a2)

#--
a3 = NIFAnnotation("https://example.org/doc1#char=80,94", "80", "94", ["https://en.wikipedia.org/wiki/Eastern_Europe"], ["dbo:Organization"])
a3.addAttribute("nif:anchorOf","Eastern Europe","xsd:string")
sent.pushAnnotation(a3)
#--
doc.pushSentence(sent)
wrp_sys.pushDocument(doc)


## Quality Evaluation
bmk = NIFBenchmark(wrp_sys, wrp_gold)
print(bmk.microF())

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

nifwrapper-1.5.3.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

nifwrapper-1.5.3-py2.py3-none-any.whl (31.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file nifwrapper-1.5.3.tar.gz.

File metadata

  • Download URL: nifwrapper-1.5.3.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.8

File hashes

Hashes for nifwrapper-1.5.3.tar.gz
Algorithm Hash digest
SHA256 e1d35097010278ed9ef9e701321737a9de504ce25f671ce3b311f5d73b7d6d5a
MD5 0e32fe9ea099d2b462906346c09a5e8c
BLAKE2b-256 6abe6a1a2c54ca5cf78b85fd19810faaaeac8fc1bc08c98d1c9229c3820b2a8e

See more details on using hashes here.

File details

Details for the file nifwrapper-1.5.3-py2.py3-none-any.whl.

File metadata

  • Download URL: nifwrapper-1.5.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 31.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.8

File hashes

Hashes for nifwrapper-1.5.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a3c09cd55ef34b4f3dc0d1c5c962fa27d245b66eda2f5db6914a3e6da1a7c70d
MD5 acf65c3e57c43233d65d5e228291eec0
BLAKE2b-256 42c87d9c67d0d0e90901e61b845ee91a8b1c845954b733b7d4fa5c4d71a0933b

See more details on using hashes here.

Supported by

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