Upload usage.py
Browse files
usage.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# maya2vec: Word embeddings for Yucatec Maya.
|
| 4 |
+
#
|
| 5 |
+
# maya2vec embeddings use 512 dimensions and were trained
|
| 6 |
+
# using the Skip-gram with Negative Sampling algorithm (SGNS)
|
| 7 |
+
# on data from La Jornada Maya (collaboration agreement),
|
| 8 |
+
# CENTROGEO - SEDECULTA phrases (Agreement SEDECULTA-DASJ-149-04-2024),
|
| 9 |
+
# and the T'aantsil corpus project (https://taantsil.com.mx/info).
|
| 10 |
+
#
|
| 11 |
+
# Writen by: Alejandro Molina Villegas
|
| 12 |
+
# Contact: amolina@centrogeo.org.mx
|
| 13 |
+
# Date: April 2025
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
import re
|
| 17 |
+
from gensim.models import Word2Vec
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Simple tokenizer for Yucatec Maya
|
| 21 |
+
def my_simple_tokenizer(text):
|
| 22 |
+
"""Tokenizes text while preserving punctuation as separate tokens."""
|
| 23 |
+
return [x.lower() for x in re.split(r"([.,;:¡!¿?]+)?\s+", text) if x]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Load the trained Word2Vec model
|
| 27 |
+
# <class 'gensim.models.doc2vec.Doc2Vec'>
|
| 28 |
+
maya2vec_path = "./model_512_60_5_-0.25_0.7308_3.35E-05"
|
| 29 |
+
model = Word2Vec.load(maya2vec_path)
|
| 30 |
+
print("Model loaded successfully.",type(model))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Generate word embedding for a single word
|
| 34 |
+
# <class 'numpy.ndarray'> (512,)
|
| 35 |
+
word = "meyaj"
|
| 36 |
+
if word in model.wv:
|
| 37 |
+
vector = model.wv[word]
|
| 38 |
+
print(f"Semantic encoded word '{word}' in", type(vector), vector.shape)
|
| 39 |
+
else:
|
| 40 |
+
print(f"The word '{word}' is out-of-vocabulary (OOV).")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Generate document embedding
|
| 44 |
+
# <class 'numpy.ndarray'> (512,)
|
| 45 |
+
text = "Bix a bel Táan in bin ich kool Tene' ooxolen"
|
| 46 |
+
tokens = my_simple_tokenizer(text)
|
| 47 |
+
try:
|
| 48 |
+
vector = model.wv.get_mean_vector(tokens)
|
| 49 |
+
print("Semantic encoded text in", type(vector), vector.shape)
|
| 50 |
+
except KeyError:
|
| 51 |
+
print("Some words in the input text are OOV, affecting the embedding computation.")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Compute cosine similarity between two words
|
| 55 |
+
# Similarity between 'peek'' and 'waalak'': 0.9583
|
| 56 |
+
word1, word2 = "peek'", "waalak'"
|
| 57 |
+
if word1 in model.wv and word2 in model.wv:
|
| 58 |
+
similarity = model.wv.similarity(word1, word2)
|
| 59 |
+
print(f"Similarity between '{word1}' and '{word2}': {similarity:.4f}")
|
| 60 |
+
else:
|
| 61 |
+
print(f"One or both words ('{word1}', '{word2}') are OOV.")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Handling OOV words
|
| 65 |
+
unknown_word = "furnance"
|
| 66 |
+
if unknown_word in model.wv:
|
| 67 |
+
vector = model.wv[unknown_word]
|
| 68 |
+
else:
|
| 69 |
+
print(f"The word '{unknown_word}' is OOV.")
|
| 70 |
+
|