Created
October 8, 2022 13:42
-
-
Save andrea-dagostino/b95331cbca4290fbc19214805beda6d4 to your computer and use it in GitHub Desktop.
fuzzy_logic_tagging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fuzzy_tagging(tags, articles): | |
""" | |
Questa funzione riceve in input una lista di tag predefiniti e la lista di contenuto testuale da taggare. | |
Restituisce un dataframe Pandas con gli articoli taggati | |
""" | |
results = [] | |
# ciclo nei tag | |
for i, tag in enumerate(tags): | |
d = {} | |
ranking = process.extract(tag, articles, limit=4) | |
for r in ranking: | |
d = {"tag": tag, "index": articles.index(r[0]), "confidence": r[1]} | |
results.append(d) | |
# organizziamo tutto in un df pandas | |
raw_tags = pd.DataFrame(results) | |
raw_tags.set_index('index', inplace=True, drop=True) | |
d = {} | |
for i, row in raw_tags.iterrows(): | |
if d.get(i): | |
if row['confidence'] >= 55: # se la soglia supera il valore di 55, aggiungere il tag a quelli esistenti se è già presente un tag | |
d[i] += ', ' + str(row['category']) | |
else: | |
d[i] = str(row['category']) | |
# creiamo il dataset finale | |
tags = pd.Series(d, name='tag') | |
tagged_df = pd.concat([posts, tags], axis=1) | |
return tagged_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment