Spaces:
Sleeping
Sleeping
File size: 1,116 Bytes
bbcc5b2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import pickle
from utils.ImageAndTextEmbedding.index import getTextEmbedding
with open("word2vec_model.pkl", "rb") as f:
textEmbedding_model = pickle.load(f)
def get_text_vector(example_text):
# Tokenize the text into words
words = example_text.lower().split()
# Filter out words that are not in the vocabulary of the Word2Vec model
words_in_vocab = [word for word in words if word in textEmbedding_model]
# Calculate the average vector representation of the words
if words_in_vocab:
text_vector = sum(textEmbedding_model[word] for word in words_in_vocab) / len(words_in_vocab)
return text_vector.tolist()
else:
return None
def get_text_discription_vector(text):
return getTextEmbedding(text)
# Example usage:
# example_text = "This is an example sentence."
# text_vector = get_text_vector(example_text)
# if text_vector:
# print("Vector representation of the example text:", text_vector)
# else:
# print("None of the words in the example text are in the vocabulary of the Word2Vec model.")
print("Text embedding model loaded successfully!") |