semvec/pypi-packages huggingface dataset:
bratevalwrapper4nlp 0.0.2
A small wrapper for BratEval that encapsulates the java commands
Requires Python: >=3.8

# BratEval Wrapper for NLP

This library wraps the Java-based [BratEval](https://github.com/READ-BioMed/brateval) utility to evaluate annotation data for named-entity-recognition (NER).
Given the availability of the Git, Java JDK and Maven, it clones and compiles brateval and wraps the io interactions into Python.

**Note that currently the release v0.3.2 of brateval is used.** See: https://github.com/READ-BioMed/brateval/tree/v0.3.2

**Note that a valid Java JDK and Maven environment must be set up correctly.**

### Install
First, make sure to install a Java JDK environment as well as Maven (for compiling the JAR file) before using the wrapper.

<details>
<summary>[Click to show] Preparation instructions for Ubuntu</summary>

```bash
# Install Java (11, or 21), Maven and Git first
sudo apt install -y openjdk-21-jdk-headless maven git

# Add JAVA_HOME variable to ~/.bashrc
echo 'export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")' >> $HOME/.bashrc

# Login again to re-load the JAVA_HOME environment variable (or export the variable manually)
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
```

</details>

Eventually, install the package using pip: **`python3 -m pip install bratevalwrapper4nlp`**

### Example
The following script demonstrates the use of the `evaluate` function.
```python
import json
from bratevalwrapper4nlp import evaluate

# Define document (ground truth and prediction)
doc_ground_truth = {
    "text": "This is a fine example.",
    "label": [
        (10, 14, "LABEL2"),
        (15, 22, "LABEL1"),
    ]
}
doc_prediction = {
    "text": "This is a fine example.",
    "label": [
        (10, 22, "LABEL1"),
    ]
}

# Verify the text spans
for src, doc in {"Ground Truth": doc_ground_truth, "Prediction": doc_prediction}.items():
    for lbl_start, lbl_stop, lbl_cls in doc.get("label", []):
        print("[{}] {} has label {}".format(
            src,
            repr(doc["text"][lbl_start:lbl_stop]),
            lbl_cls
        ))

# Run evaluation
score_response = evaluate(
    doc_ground_truth,     # or list of docs: [doc_ground_truth]
    doc_prediction,       # or list of docs: [doc_prediction]
    span_match="overlap", # "overlap", "exact", or float (overlap percentage between 1.0 and 0.0)
    type_match="exact"    # "exact", or "inexact" (ignores label classes)
)
scores = score_response["scores"]

print("Obtained scores:")
print(json.dumps(scores, indent=2))
```
dseqmap4nlp 0.0.5
A small tool to parse and process annotated text corpora
Requires Python: >=3.8

# DSeqMap for NLP

An NLP utility for handling named entity recognition (NER) annotation data between discretized and character-wise representations. 

### Install
Use `pip install dseqmap4nlp` to install the package.

### Example
The following script demonstrates some transformation of annotation data.
```python
from dseqmap4nlp import SpacySequenceMapper, LabelLoader, CharSequenceMapper

# Load JSONL
samples = [
    {"text": "Das ist gut.", "label": [
        (0,3, "a"),
        (2,7, "b"),
        (6,8, "b")
    ]}
]

for sample in samples:
    text = sample["text"]
    anns = sample["label"]
    # Mapper @ Chars <-> SpaCy Tokens
    mapper = SpacySequenceMapper(text, nlp="de")
    # (trivial) Mapper @ Chars <-> Chars
    charmapper = CharSequenceMapper(text=text)

    # Load annotation data
    # assuming the format: [ (start_idx, stop_idx, label_class), ...]
    # and merge it with to a certain (char <-> discrete sequence) mapper
    annotationset = LabelLoader.from_text_spans(anns, mapper)
    
    # Determine number fo overlaps
    print("Overlaps:", annotationset.countOverlaps())

    # Apply the following transformations to the annotation data:
    # - transform char-based labels onto discretized sequence items (e.g. tokens)
    #   -> Expand if a label's char bounds are not exactly at token bounds
    # - Remove shorter spans in case of overlapping spans
    #   -> Note: New overlaps could also be introduced by span expansion!
    filtered_spans = annotationset\
        .toDSeqSpans(strategy=["expand"])\
        .withoutOverlaps(strategy="prefer_longest", merge_same_classes=True)

    # Check overlaps again (No overlap should exist anymore!)
    print("Overlaps:", filtered_spans.countOverlaps())

    # Transform annotation data into IOB2-formatted sequence.
    print("Sequence:")
    print(filtered_spans.toFormattedSequence(schema="IOB2"))

    # Try to generate an IOB2 sequence with overlaps. (It should fail!)
    print("Previous sequence (should fail):")
    try:
        # Should raise an error...
        print(annotationset.toFormattedSequence(schema="IOB2"))
    except ValueError as e:
        print("Error raised: " + repr(e))
```