keyword_konan / text_annotator.py
mlo0ollm's picture
keyword annoation module
0c98584
import re
def generate_annotated_text(text, keyw_list):
'''
:param text: str
:param keyw_list: list of str
:return: str
e.g. input1 = 'I like an apple. Do you like apples too?'
input2 = ["like", "apple"]
output = annotated_text("I ", ("like", ""), " an ", ("apple", ""), ". Do you ", ("like", ""), " ", ("apple", ""), "s too?")
'''
def find_keyword_index(text, keyw_list):
indices = []
for keyword in keyw_list:
for match in re.finditer(keyword, text):
indices.append((keyword, match.start()))
return indices
indices = find_keyword_index(text, keyw_list)
# 1. indices๋ฅผ index ๊ฐ’์— ๋”ฐ๋ผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
sorted_indices = sorted(indices, key=lambda x: x[1])
output = 'annotated_text('
last_index = 0
# 2. input1์—์„œ ๊ฐ ํ‚ค์›Œ๋“œ์˜ ์œ„์น˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ถ„ํ•  ๋ฐ ์žฌ์ฒ˜๋ฆฌ
for word, start_idx in sorted_indices:
output += f'"{text[last_index:start_idx]}", ("{word}", ""), '
last_index = start_idx + len(word)
# 3. ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด ์ƒ์„ฑ
output += f'"{text[last_index:]}"' + ')'
# 4. ์—ฐ์† ๋ฌธ์ž์—ด ๋Œ€์น˜
string_cont_type1 = re.compile(r'", ""\), " ", \("')
string_cont_type2 = re.compile(r'", ""\), "", \("')
output = string_cont_type1.sub(' ', output)
output = string_cont_type2.sub(' ', output)
return output