所以我有兩個 Trigram 串列(每個 20 個單詞組合)例如
l1 = ('你好', 'its', 'me'), ('I', 'need', 'help') ...
l2 = ('我', '需要', '幫助'), ('什么', '是', '這個') ...
現在我想在一個圖表(可能是成對圖)中將這兩個串列可視化,以查看是否有相似之處(所有 3 個單詞必須相同)。
先感謝您
uj5u.com熱心網友回復:
INPUT:
l1 = [('hello', 'its', 'me'), ('I', 'need', 'help') ...]
l2 = [('I', 'need', 'help'), ('What', 'is', 'this') ...]
OUTPUT:
sim = [[('hello', 'its', 'me'), 1], [('I', 'need', 'help'), 2], [('What', 'is', 'this'), 1]]
merged = l1 l2
unique = set(merged)
results = []
for tri in unique:
results.append([tri, merged.count(tri)])
從你的描述來看,這似乎是你要找的。請讓我知道是否需要任何調整。
uj5u.com熱心網友回復:
Larry the Llama 給出的答案似乎錯過了“看看是否有相似之處”,因為該解決方案使用 set() 將洗掉任何重復項。
如果您希望進行完整迭代以找到完全相似的三元組:
merged = l1 l2
results_counter = {}
# Iterate all the trigrams
for index, trigram in enumerate(merged):
# Iterate all the trigrams which lay after in the array
for second_index in range(index, len(merged)):
all_same = True
# Find all of which are the same as the comparing trigram
for word_index, word in enumerate(trigram):
if merged[second_index][word_index] == trigram[word_index:
all_same = False
break
# If trigram was not found in the results_counter add the key else returning the value
previous_found = results_counter.setDefault(str(trigram), 0)
# Add one
previous_found[str(trigram)] = 1
# Will print the keys and the
for key in previous_found.keys():
# Print the count for each trigram
print(key, previous_found[key])
澄清后編輯:
import seaborn as sns
import pandas as pd
d1 = [("hello", "its", "me"), ("dont", "its", "me")]
d2 = [("hello", "its", "me"), ("Hello", "I", "dont")]
word_to_number = {}
number_to_word = {} # if you want to show the sentence again
def one_hot(l):
"""
This function one hot encodes (converts each appearens of a word
to a number) and returns the encoded list while also adding the
keys to converter dictionaries for reverse converting.
"""
one_hot_encoded = []
for trigram in l:
encoded_trigram = []
for word in trigram:
# Add encoding of the word
encoded_word = word_to_number.setdefault(word, len(word_to_number))
number_to_word[encoded_word] = word
# Add to the one hot encoded trigram = {}
encoded_trigram.append(encoded_word)
# Add to the list which is sent in
one_hot_encoded.append(encoded_trigram)
return one_hot_encoded
d1 = one_hot(d1)
d2 = one_hot(d2)
data = {}
for ind, trigram in enumerate(d1 d2):
# This will add each word to be compared
data["t" str(ind)] = trigram
frame = pd.DataFrame.from_dict(data)
print(frame)
plot = sns.pairplot(frame)
# Make it clear
plot.set(ylim=(frame.min().min() - 1, frame.max().max() 1))
plot.set(xlim=(frame.min().min() - 1, frame.max().max() 1))
import matplotlib.pyplot as plt
plt.show()
這篇文章將為您提供三元組的配對圖,盡管它不是很直觀,因為您必須尋找精確的線性值。您可以使用它,但請確保您不必使用許多不同的詞,因為這會扭曲軸并使視覺上很難看到結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/376616.html
標籤:Python matplotlib 海生 可视化 n-gram
