I'm working on a Python function that takes a piece of text and indicates if words are modified by words like very or not.
I'm looking for instance to label very nice differently from not nice. This does what it's supposed to do but I feel it might be clunky or there might be a more very efficient approach I'm not aware of.
def polarizer(input_sentence, modifier_dict, window_length):
"""
The polarizer attaches the label given in modifier_dict to all items in input_sentence occurring a window_length after the modifier.
modifier_dict needs to be formatted like so: {'not':'-', 'yes':'+', ..}
Returns list with labels added.
"""
#find positions of modifiers
indices= [(range(n+1, n+1+window_length), modifier_dict[i]) for n,i in enumerate(input_sentence) if i in modifier_dict.keys()]
#iterate over modifiers
for ind in indices:
for n in ind[0]:
#add label unless there is already a label attached
if n < len(input_sentence) and not input_sentence[n].endswith(tuple(modifier_dict.values())):
input_sentence[n]=input_sentence[n]+ind[1]
outputsentence= input_sentence
return outputsentence
#Test sentences
modifier_dict={'very':'+'}
test1="This is fine".split(" ")
test2="This is very fine".split(" ")
print polarizer(test1, modifier_dict, 2)
print polarizer(test2, modifier_dict, 2)