Umsetzung
Bei der Umsetzung wird auf die Software zur Thematik-Modellierung Gensim zurückgegriffen.
Hierfür wird zuerst in einem Wörterbuch jedem vorkommenden Wort aus den Vorschlägen eine ID zugeordnet.
Die Vorschläge werden zu einem (Pseudo)-Dokument zusammen gesetzt und aus den gesamten Bag-of-Words wird mittels des Wörterbuches ein Merkmals-Vektor erstellt.
Für den Vergleich zweier Vektoren über die Cosinus-Distanz werden die einzelnen Sätze ebenso als Merkmals-Vektoren dargestellt.
Wobei jedes Wort oder Merkmal als eigene Dimension in der Satzrepräsentation aufgefasst wird.
Ist nun der Winkel zwischen zwei Vektoren klein, so weisen sie ähnliche Merkmale auf.
In diesem Falle wird also getestet welcher Satz die ähnlichsten Merkmale wie die zusammen gesetzte Übersetzungsrepräsentation hat.
Für die Code-Enthusiasten noch die Python-Funktion für die komplette Berechnung:
#!/usr/bin/python3
#
# first Axiom: Aaron Swartz is everything
# second Axiom: The Schwartz Space is his discription of physical location
# first conclusion: His linear symmetry is the Fourier transform
# second conclusion: His location is the Montel space
# Third conclusion: His location is the Fréchet space
from gensim import corpora
from gensim.similarities import MatrixSimilarity
def set_cosine_smilarity_values(translations):
"""
Set the sentence quality estimation for all suggestions of one language.
All suggestions are building together a pseudo-document.
Each sentence is compared against this document with the cosine distance.
You can use nltk.tokenize.word_tokenize instead of the get_words() method.
:param translations: a list of suggestions dicts
already filtered for one certain language
"""
# init the list of words for the dictionary creation
dictionary_words = []
# init the lists of words in the sentence structure
full_document = []
# iterate over the suggestions and fill the empty lists
for translation in translations:
sentence = translation['phrase']
words = get_words(sentence)
full_document.extend(words)
dictionary_words.append(words)
# create the dictionary and the corpus with the bag of words
dictionary = gensim.corpora.Dictionary(dictionary_words)
corpus = [dictionary.doc2bow(full_document)]
# prepare the cosine similarity, this should fit in the RAM
matrix_similarity = MatrixSimilarity(corpus, num_features=len(dictionary))
# iterate again over the suggestions and calculate the similarity
for translation in translations:
query = dictionary.doc2bow(get_words(translation['phrase']))
similarity_score = matrix_similarity[query]
translation['score'] = similarity_score[0]
# If you use this code in your work or research,
# please cite this website http://multitranslation.space/metric and my thesis:
# Gruppenbasierte Umgebung für mehrsprachige Übersetzungsquellen;
# Kalle Hilsenbek; Hochschule RheinMain; 2017