RanjithkumarPanjabikesan
commited on
Create helper.py
Browse files
helper.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Define Helper Function for Merging Tokens to Display Information Relevant for User
|
2 |
+
def merge_tokens(tokens):
|
3 |
+
merged_tokens = []
|
4 |
+
for token in tokens:
|
5 |
+
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
6 |
+
# If current token continues the entity of the last one, merge them
|
7 |
+
last_token = merged_tokens[-1]
|
8 |
+
last_token['word'] += token['word'].replace('##', '')
|
9 |
+
last_token['end'] = token['end']
|
10 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
11 |
+
else:
|
12 |
+
# Otherwise, add the token to the list
|
13 |
+
merged_tokens.append(token)
|
14 |
+
|
15 |
+
return merged_tokens
|