File size: 816 Bytes
a403f87
 
 
 
 
5adda08
a403f87
5adda08
 
 
 
 
a403f87
5adda08
 
a403f87
 
 
 
5adda08
a403f87
 
 
 
5adda08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from gliner import GLiNER
from typing import List

def run_ner(model, texts: List[str], labels_list: List[str], threshold=0.4):
    """
    Executes named entity recognition (NER) on a list of texts.

    Parameters:
    - model: loaded GLiNER model.
    - texts: list of texts to analyze.
    - labels_list: list of NER labels to detect.
    - threshold: confidence threshold for predictions.

    Returns:
    - ner_results: list of dictionaries containing detected entities for each text.
    """
    ner_results = []
    for text in texts:
        try:
            # Predict entities for the text
            entities = model.predict_entities(text, labels_list, threshold=threshold)
            ner_results.append(entities)
        except Exception as e:
            ner_results.append([])
    return ner_results