Naive Approach updated (emojis_to_consider, gibt Scores zurück)
This commit is contained in:
parent
60bac91655
commit
2706e19aec
@ -37,7 +37,7 @@ def stemming(messages):
|
|||||||
|
|
||||||
|
|
||||||
# * compare words to emoji descriptions
|
# * compare words to emoji descriptions
|
||||||
def evaluate_sentence(sentence, table, description_key = 'description', lang = 'eng'):
|
def evaluate_sentence(sentence, table, description_key = 'description', lang = 'eng', emojis_to_consider="all"):
|
||||||
|
|
||||||
tokenized_sentence = word_tokenize(sentence)
|
tokenized_sentence = word_tokenize(sentence)
|
||||||
n = len(tokenized_sentence)
|
n = len(tokenized_sentence)
|
||||||
@ -77,10 +77,7 @@ def evaluate_sentence(sentence, table, description_key = 'description', lang = '
|
|||||||
|
|
||||||
# load and preprocess data
|
# load and preprocess data
|
||||||
# emojis_to_consider can be either a list or "all"
|
# emojis_to_consider can be either a list or "all"
|
||||||
def prepareData(stemming=False, emojis_to_consider="all"):
|
def prepareData(stemming=False):
|
||||||
|
|
||||||
table.head()
|
|
||||||
|
|
||||||
if(stemming):
|
if(stemming):
|
||||||
table['description'] = stemming(table['description'])
|
table['description'] = stemming(table['description'])
|
||||||
|
|
||||||
@ -88,7 +85,6 @@ def prepareData(stemming=False, emojis_to_consider="all"):
|
|||||||
lookup = {}
|
lookup = {}
|
||||||
emoji_set = []
|
emoji_set = []
|
||||||
for index, row in table.iterrows():
|
for index, row in table.iterrows():
|
||||||
if(emojis_to_consider=="all" or (type(emojis_to_consider)==list and row['character'] in emojis_to_consider)):
|
|
||||||
lookup[index] = row['character']
|
lookup[index] = row['character']
|
||||||
emoji_set.append(row['character'])
|
emoji_set.append(row['character'])
|
||||||
|
|
||||||
@ -99,21 +95,31 @@ def prepareData(stemming=False, emojis_to_consider="all"):
|
|||||||
# make a prediction for an input sentence
|
# make a prediction for an input sentence
|
||||||
def predict(sentence, lookup, emojis_to_consider="all", criteria="threshold", description_key='description', lang = 'eng', n=10, t=0.9):
|
def predict(sentence, lookup, emojis_to_consider="all", criteria="threshold", description_key='description', lang = 'eng', n=10, t=0.9):
|
||||||
|
|
||||||
result = evaluate_sentence(sentence, table, description_key, lang)
|
result = evaluate_sentence(sentence, table, description_key, lang, emojis_to_consider=emojis_to_consider)
|
||||||
|
|
||||||
|
try:
|
||||||
if(criteria=="summed"):
|
if(criteria=="summed"):
|
||||||
indexes = np.argsort([-np.sum(x) for x in result])[0:n]
|
resultValues = [-np.sum(x) for x in result]
|
||||||
elif (criteria=="max_val"):
|
elif (criteria=="max_val"):
|
||||||
indexes = np.argsort([-np.max(x) for x in result])[0:n]
|
resultValues = [-np.max(x) for x in result]
|
||||||
elif(criteria=="avg"):
|
elif(criteria=="avg"):
|
||||||
indexes = np.argsort([-np.mean(x) for x in result])[0:n]
|
resultValues = [-np.mean(x) for x in result]
|
||||||
else:
|
else:
|
||||||
indexes= np.argsort([-len(np.where(x>t)[0]) / (x.shape[0] * x.shape[1]) for x in result])[0:n]
|
resultValues = [-len(np.where(x>t)[0]) / (x.shape[0] * x.shape[1]) for x in result]
|
||||||
|
indexes = np.argsort(resultValues)
|
||||||
|
results = np.sort(resultValues)
|
||||||
|
|
||||||
if(emojis_to_consider!="all"):
|
if (emojis_to_consider != "all" and type(emojis_to_consider) == list):
|
||||||
for i in indexes:
|
indexes2 = []
|
||||||
if (i not in lookup):
|
results2 = []
|
||||||
indexes = np.delete(indexes, [i])
|
for i in range(len(indexes)):
|
||||||
|
if lookup[indexes[i]] in emojis_to_consider:
|
||||||
|
indexes2.append(indexes[i])
|
||||||
|
results2.append(results[i])
|
||||||
|
indexes = indexes2
|
||||||
|
results = results2
|
||||||
|
indexes = indexes[0:n]
|
||||||
|
results = results[0:n]
|
||||||
|
|
||||||
# build a result table
|
# build a result table
|
||||||
table_array = [[lookup[indexes[i]], str(table.iloc[indexes[i]][description_key])] for i in range(n) ]
|
table_array = [[lookup[indexes[i]], str(table.iloc[indexes[i]][description_key])] for i in range(n) ]
|
||||||
@ -122,7 +128,11 @@ def predict(sentence, lookup, emojis_to_consider="all", criteria="threshold", de
|
|||||||
|
|
||||||
#display(table_frame)
|
#display(table_frame)
|
||||||
|
|
||||||
return list(table_frame[criteria])
|
return list(table_frame[criteria]), results
|
||||||
|
|
||||||
|
except ZeroDivisionError as err:
|
||||||
|
print("There seems to be a problem with the input format. Please enter a nonempty string")
|
||||||
|
|
||||||
|
|
||||||
#predict("I like to travel by train", description_key='description' , lang='eng')
|
#predict("I like to travel by train", description_key='description' , lang='eng')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user