Commit 8ce79ea1 authored by podrazka's avatar podrazka
Browse files

Add token attributes for the visualization of WIViT

parent 52b6db00
Loading
Loading
Loading
Loading
+25 −16
Changes for src/SBERT_Model.py: 25 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -128,6 +128,7 @@ class SBERT_Model:
        plt.savefig(f'{self.name}_histogram.png', bbox_inches='tight')

    def get_word_importance(self,sentence_pair, mask="[MASK]"):
        print(f"Getting word importance of {sentence_pair}")
        nlp = spacy.load("en_core_web_sm")
        sentence1 = nlp(sentence_pair[0])
        sentence2 = nlp(sentence_pair[1])
@@ -139,41 +140,49 @@ class SBERT_Model:
            for y in range(len(sentence1)):
                if y == i:
                    masked_sentence.append(mask)
                    masked_word = sentence1[y].text
                    masked_word = sentence1[y]
                else:
                    masked_sentence.append(sentence1[y].text)
            masked_sentence = " ".join(masked_sentence)
            masked_pair = {"masked word": masked_word, "sentence1": masked_sentence, "sentence2": sentence_pair[1]}
            masked_pair = {"masked word": masked_word, "sentence1": masked_sentence, "sentence2": sentence_pair[1], "masked sentence": 0}
            masked_pairs.append(masked_pair)
        for i in range(len(sentence2)):
            masked_sentence = []
            for y in range(len(sentence2)):
                if y == i:
                    masked_sentence.append(mask)
                    masked_word = sentence2[y].text
                    masked_word = sentence2[y]
                else:
                    masked_sentence.append(sentence2[y].text)
            masked_sentence = " ".join(masked_sentence)
            masked_pair = {"masked word": masked_word, "sentence1": sentence_pair[0], "sentence2": masked_sentence}
            masked_pair = {"masked word": masked_word, "sentence1": sentence_pair[0], "sentence2": masked_sentence, "masked sentence": 1}
            masked_pairs.append(masked_pair)
        scores = []
        for masked_pair in masked_pairs:
            embs = self.get_embeddings([masked_pair["sentence1"], masked_pair["sentence2"]])
            scores.append((masked_pair, float(abs(unmasked_similarity - self.get_cosine_scores(embs[0], embs[1])))))
#TODO: Find out more about the words that cause high deviation from unmasked similarity.
#         sorted_scores = copy.deepcopy(scores)
#         sorted_scores.sort(key = lambda x: x[1], reverse=True)
#         print(f"Pair: {sentence_pair}, Gold: {gold}")
#         for score in sorted_scores:
#             masked_pair = score[0]
#             print(f'1. {masked_pair["sentence1"]}\n \
# 2.{masked_pair["sentence2"]}\n \
# Deviation: {score[1]:.3f}, Masked word: {masked_pair["masked word"]}\n')
        html_output = []
        html_output.append('<div class="sentence">\n')
        token_scores = []
        for i, score in enumerate(scores):
            masked_pair = score[0]
            html_output.append(f'<div style="background-color: rgba(0, 160, 252, {score[1]:.2f});">{masked_pair["masked word"]}<br>{score[1]:.2f}</div>\n')
            if masked_pair["masked sentence"]:
                doc2 = 0
            else:
                doc2 = 1
            token_score = {"doc":[sentence1, sentence2][masked_pair["masked sentence"]], "doc2":[sentence1, sentence2][doc2], "word": masked_pair["masked word"].text, "i": masked_pair["masked word"].i,  "score": score[1], "sentence_pair": sentence_pair}
            token_scores.append(token_score)
        
        html_output = []
        html_output.append('<div class="sentence">\n')
        for i, score in enumerate(token_scores):
            s1_words = [s.text for s in score["doc"]]
            s2_words = [s.text for s in score["doc2"]]
            token = score["doc"][score["i"]]
            cos_score = score["score"]
            if s1_words.count(token.text) and s2_words.count(token.text):
              both_sentences = True
            else:
              both_sentences = False
            html_output.append(f'<div id="{randrange(0,10000)}_{token.text}_{token.lemma_}_{both_sentences}_{token.pos_}_{token.tag_}_{token.dep_}_{cos_score:.2f}" style="background-color: rgba(0, 160, 252, {cos_score:.2f});">{token.text}<br>{cos_score:.2f}</div>\n')
            if i == len(sentence1)-1:
                html_output.append('</div>\n<div class="sentence">\n')
        html_output.append('</div>\n')