-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathentity_span.py
43 lines (34 loc) · 1.33 KB
/
entity_span.py
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
#! #!/usr/bin/env python
# -*- coding: utf-8 -*-
# ***************************************************************************80
#
# entity_span.py -t <text> -e <entity>
#
# *****************************************************************************
# third party imports
import click
@click.command()
@click.option('-t', '--text', type=str, required=True, help='text')
@click.option('-e', '--entity', type=str, required=True, help='entity')
def main(text: str, entity: str):
"""Main Function"""
total_number_of_bytes, start_pos, end_pos = process(text, entity)
print(f"total number of bytes of given text: {total_number_of_bytes}")
print(f"start_pos (UTF8 byte OFFSET): {start_pos}")
print(f"end_pos (UTF8 byte OFFSET): {end_pos}")
def process(text: str, entity: str) -> tuple:
"""Finds totoal number of bytes of given text, start and end position as UTF8 byte offset"""
total_number_of_bytes = len(bytes(text, "utf8"))
index = text.find(entity)
if index != -1:
if index == 0:
start_pos = index
else:
start_pos = len(bytes(text[0:index], "utf8"))
end_pos = start_pos + len(bytes(entity, "utf8"))
else:
print("Given entity is not present in the text")
quit()
return total_number_of_bytes, start_pos, end_pos
if __name__ == "__main__":
main()