Search_result/autocrct.py
def autocorrect_query(query,train=None,test=None,cutoff=0.8,warning_on=True):
	"""
	autocorrect a query based on the training set
	"""	
	if train is None:
		train = pd.read_csv('../input/train.csv').fillna('')
	if test is None:
		test = pd.read_csv('../input/test.csv').fillna('')
	train_data = train.values[train['query'].values==query,:]
	test_data = test.values[test['query'].values==query,:]
	s = ""
	for r in train_data:
		s = "%s %s %s"%(s,BeautifulSoup(r[2]).get_text(" ",strip=True),BeautifulSoup(r[3]).get_text(" ",strip=True))
	for r in test_data:
		s = "%s %s %s"%(s,BeautifulSoup(r[2]).get_text(" ",strip=True),BeautifulSoup(r[3]).get_text(" ",strip=True))
	s = re.findall(r'[\'\"\w]+',s.lower())
	s_bigram = [' '.join(i) for i in bigrams(s)]
	s.extend(s_bigram)
	corrected_query = []	
	for q in query.lower().split():
		if len(q)<=2:
			corrected_query.append(q)
			continue
		corrected_word = difflib.get_close_matches(q, s,n=1,cutoff=cutoff)
		if len(corrected_word) >0:
			corrected_query.append(corrected_word[0])
		else :
			if warning_on:
				print ("WARNING: cannot find matched word for '%s' -> used the original word"%(q))
			corrected_query.append(q)	
	return ' '.join(corrected_query)

	autocorrect a query based on the training set
	
autocorrect a query based on the training set
autocorrect a query based on the training set
def autocorrect_query(query,train=None,test=None,cutoff=0.8,warning_on=True):
	if train is None:
		train = pd.read_csv('../input/train.csv').fillna('')
	if test is None:
		test = pd.read_csv('../input/test.csv').fillna('')
	train_data = train.values[train['query'].values==query,:]
	test_data = test.values[test['query'].values==query,:]
	s = ""
	for r in train_data:
		s = "%s %s %s"%(s,BeautifulSoup(r[2]).get_text(" ",strip=True),BeautifulSoup(r[3]).get_text(" ",strip=True))
	for r in test_data:
		s = "%s %s %s"%(s,BeautifulSoup(r[2]).get_text(" ",strip=True),BeautifulSoup(r[3]).get_text(" ",strip=True))
	s = re.findall(r'[\'\"\w]+',s.lower())
	s_bigram = [' '.join(i) for i in bigrams(s)]
	s.extend(s_bigram)
	corrected_query = []	
	for q in query.lower().split():
		if len(q)<=2:
			corrected_query.append(q)
			continue
		corrected_word = difflib.get_close_matches(q, s,n=1,cutoff=cutoff)
		if len(corrected_word) >0:
			corrected_query.append(corrected_word[0])
		else :
			if warning_on:
				print ("WARNING: cannot find matched word for '%s' -> used the original word"%(q))
			corrected_query.append(q)	
	return ' '.join(corrected_query)
def autocorrect_query ( query , train = None , test = None , cutoff = 0.8 , warning_on = True ) : if train is None : train = pd . read_csv ( '../input/train.csv' ) . fillna ( '' ) if test is None : test = pd . read_csv ( '../input/test.csv' ) . fillna ( '' ) train_data = train . values [ train [ 'query' ] . values == query , : ] test_data = test . values [ test [ 'query' ] . values == query , : ] s = "" for r in train_data : s = "%s %s %s" % ( s , BeautifulSoup ( r [ 2 ] ) . get_text ( " " , strip = True ) , BeautifulSoup ( r [ 3 ] ) . get_text ( " " , strip = True ) ) for r in test_data : s = "%s %s %s" % ( s , BeautifulSoup ( r [ 2 ] ) . get_text ( " " , strip = True ) , BeautifulSoup ( r [ 3 ] ) . get_text ( " " , strip = True ) ) s = re . findall ( r'[\'\"\w]+' , s . lower ( ) ) s_bigram = [ ' ' . join ( i ) for i in bigrams ( s ) ] s . extend ( s_bigram ) corrected_query = [ ] for q in query . lower ( ) . split ( ) : if len ( q ) <= 2 : corrected_query . append ( q ) continue corrected_word = difflib . get_close_matches ( q , s , n = 1 , cutoff = cutoff ) if len ( corrected_word ) > 0 : corrected_query . append ( corrected_word [ 0 ] ) else : if warning_on : print ( "WARNING: cannot find matched word for '%s' -> used the original word" % ( q ) ) corrected_query . append ( q ) return ' ' . join ( corrected_query )
autocorrect a query based on the training set
autocorrect a query based on the training set
"""
	autocorrect a query based on the training set
	"""
query
train
test
cutoff
warning_on