Created
August 8, 2019 06:54
-
-
Save inkrement/83e8754537535bb9d17287155f62d66b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load_embedding <- function(file_path){ | |
# load full file | |
lines <- readLines(file_path) | |
# create new environment | |
embeddings_env <- new.env(hash = TRUE, parent = emptyenv()) | |
# this function is used to convert vectors to unit vectors | |
# by dividing their components by vector length | |
normalize_vector <- function(a){ | |
a/sqrt(sum(a**2)) | |
} | |
# iterate through the whole file line by line | |
for (i in 1:length(lines)) { | |
line <- lines[[i]] | |
values <- strsplit(line, " ")[[1]] | |
label <- values[[1]] | |
embeddings_env[[label]] <- normalize_vector(as.double(values[-1])) | |
} | |
embeddings_env | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment