File size: 5,831 Bytes
336795c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import sys
sys.dont_write_bytecode = True

import re
import unicodedata
import numpy
import sentencepiece
from sentencepiece import sentencepiece_model_pb2 as sentencepieceModel

from helper import onnxSessionBuild

pathModel = "./"

glinerTypeAllowList = ["person", "organization", "place", "category", "event"]
glinerScoreMin = 0.4
glinerEntId = 250103
glinerSepId = 250104
glinerClsId = 1
glinerEosId = 2
glinerMaxWidth = 12
glinerMaxLength = 384

proto = sentencepieceModel.ModelProto()

with open(f"{pathModel}spm.model", "rb") as file:
    proto.ParseFromString(file.read())

proto.normalizer_spec.add_dummy_prefix = False

sentencepieceGliner = sentencepiece.SentencePieceProcessor()
sentencepieceGliner.LoadFromSerializedProto(proto.SerializeToString())

onnxSessionGliner = onnxSessionBuild(f"{pathModel}onnx/model.onnx")

def wideCheck(character):
    return character != "" and unicodedata.east_asian_width(character) in ("W", "F")

def tokenizeWord(text):
    resultList = []

    for segment in re.findall(r"\d|\D+", text):
        for tokenId in sentencepieceGliner.encode(segment, out_type=int):
            resultList.append(tokenId)

    return resultList

def wordSplit(text):
    resultList = []

    for match in re.finditer(r"\w+(?:[-_]\w+)*|[^\w\s]", text):
        word = match.group(0)
        start = match.start()

        segment = ""
        segmentStart = start

        for a in range(len(word)):
            if wideCheck(word[a]):
                if segment != "":
                    resultList.append({"text": segment, "start": segmentStart, "end": segmentStart + len(segment)})

                    segment = ""

                resultList.append({"text": word[a], "start": start + a, "end": start + a + 1})

                segmentStart = start + a + 1
            else:
                if segment == "":
                    segmentStart = start + a

                segment += word[a]

        if segment != "":
            resultList.append({"text": segment, "start": segmentStart, "end": segmentStart + len(segment)})

    return resultList

def predict(text):
    resultList = []

    wordList = wordSplit(text)

    if len(wordList) > glinerMaxLength:
        wordList = wordList[0:glinerMaxLength]

    numWords = len(wordList)

    if numWords == 0:
        return resultList

    inputIdList = [glinerClsId]
    wordsMaskList = [0]

    for a in range(len(glinerTypeAllowList)):
        inputIdList.append(glinerEntId)
        wordsMaskList.append(0)

        for tokenId in tokenizeWord(glinerTypeAllowList[a]):
            inputIdList.append(tokenId)
            wordsMaskList.append(0)

    inputIdList.append(glinerSepId)
    wordsMaskList.append(0)

    for a in range(numWords):
        subwordList = tokenizeWord(wordList[a]["text"])

        for b in range(len(subwordList)):
            inputIdList.append(subwordList[b])
            wordsMaskList.append(a + 1 if b == 0 else 0)

    inputIdList.append(glinerEosId)
    wordsMaskList.append(0)

    inputIds = numpy.array([inputIdList], dtype=numpy.int64)
    attentionMask = numpy.ones((1, len(inputIdList)), dtype=numpy.int64)
    wordsMask = numpy.array([wordsMaskList], dtype=numpy.int64)
    textLengths = numpy.array([[numWords]], dtype=numpy.int64)

    spanIdxList = []
    spanMaskList = []

    for a in range(numWords):
        for b in range(glinerMaxWidth):
            end = a + b

            spanIdxList.append([a, end])
            spanMaskList.append(end <= numWords - 1)

    spanIdx = numpy.array([spanIdxList], dtype=numpy.int64)
    spanMask = numpy.array([spanMaskList], dtype=bool)

    feedObject = {
        "input_ids": inputIds,
        "attention_mask": attentionMask,
        "words_mask": wordsMask,
        "text_lengths": textLengths,
        "span_idx": spanIdx,
        "span_mask": spanMask
    }

    logits = onnxSessionGliner.run(["logits"], feedObject)[0]

    probability = 1.0 / (1.0 + numpy.exp(-logits[0]))

    candidateList = []

    for a in range(numWords):
        for b in range(glinerMaxWidth):
            end = a + b

            if end > numWords - 1:
                continue

            for c in range(len(glinerTypeAllowList)):
                score = float(probability[a][b][c])

                if score > glinerScoreMin:
                    candidateList.append({
                        "wordStart": a,
                        "wordEnd": end,
                        "label": glinerTypeAllowList[c],
                        "score": score
                    })

    candidateList.sort(key=lambda candidate: candidate["score"], reverse=True)

    takenList = []

    for a in range(len(candidateList)):
        candidate = candidateList[a]

        isOverlap = False

        for b in range(len(takenList)):
            taken = takenList[b]

            if candidate["wordStart"] <= taken["wordEnd"] and taken["wordStart"] <= candidate["wordEnd"]:
                isOverlap = True

                break

        if isOverlap == False:
            takenList.append(candidate)

            charStart = wordList[candidate["wordStart"]]["start"]
            charEnd = wordList[candidate["wordEnd"]]["end"]

            resultList.append({
                "start": charStart,
                "end": charEnd,
                "text": text[charStart:charEnd],
                "label": candidate["label"],
                "score": candidate["score"]
            })

    resultList.sort(key=lambda entity: entity["start"])

    return resultList

text = unicodedata.normalize("NFKC", "Linus Torvalds created Linux in Helsinki and later joined the Linux Foundation, while 田中太郎 works at 東京大学 in Tokyo.")

entityList = predict(text)

for a in range(len(entityList)):
    print(f"{entityList[a]['score']:.6f} | {entityList[a]['label']} | {entityList[a]['text']}")