I want to find the exact Substring of a string.
import string
a=['accept','freed*','partie*','accepta*','freeing','party*']
sent="i am accepting your invitation for the party"
token=sent.split(" ")
for j in range(0,len(a)):
for i in range(0,len(token)):
if(token[i].find(a[j])==0):
print "found",token[i],a[j],token[i].find(a[j])
Output:
> found accepting accept 0
Desired output:
> found accepting accept 0
> found part party* 0
I have tried a lot, using re.search(),index().., but I didn't get the desired output. If anybody know this, pleas help me out?
- content of Posemo.csv : accept ,accepta*,accepted ,accepting ,accepts etc..
solution:
import operator,csv,re
from collections import defaultdict
def post_features(inpt_word_first_char):
input_file="/home/user/Thesis/BOOKS/Features/Posemo.csv"
match_words=[]
fin=open(input_file,"r")
read_list=fin.read()
match_words=[word for word in read_list.split() if word.startswith(inpt_word_first_char)]
return match_words
matches = defaultdict(list)
input_line="I am accepting your invitation for the party"
input_line=input_line.lower()
input_words=input_line.split(" ")
for i in range(0,len(input_words)):
inpt_word_first_char=input_words[i][0]
match_words=post_features(inpt_word_first_char)
match_words1=[]
for k in range (0,len(match_words)):
match_words1.append(match_words[k].rstrip("*"))
for match in match_words1:
if match in input_words[i] :
if((len(input_words[i])>=len(match) and len(match)>2) or len(match)==len(input_words[i])):
match_perc=map(operator.eq,input_words[i],match).count(True)
matches[input_words[i]].append([match,match_perc])
##print matches
for word,match_percentage in matches.iteritems():
print('Key: {} - Matched word : {}'.format(word,max(match_percentage[match_percentage.index(max(match_percentage))])))