## Example on Freebase - download the data
filename <- paste(
"https://raw.githubusercontent.com/bnosac-dev/GraphEmbeddings/master/",
"diffbot_data/FB15k/freebase_mtr100_mte100-train.txt",
sep = "")
tmpfile <- tempfile(pattern = "freebase_mtr100_mte100_", fileext = "txt")
ok <- suppressWarnings(try(
download.file(url = filename, destfile = tmpfile),
silent = TRUE))
if(!inherits(ok, "try-error") && ok == 0){
## Build the model on the downloaded data
x <- read.delim(tmpfile, header = FALSE, nrows = 1000,
col.names = c("entity_head", "relation", "entity_tail"),
stringsAsFactors = FALSE)
head(x)
set.seed(123456789)
model <- embed_entityrelationspace(x, dim = 50)
plot(model)
predict(model, "/m/027rn /location/country/form_of_government")
## Also add reverse relation
x_reverse <- x
colnames(x_reverse) <- c("entity_tail", "relation", "entity_head")
x_reverse$relation <- sprintf("REVERSE_%s", x_reverse$relation)
relations <- rbind(x, x_reverse)
set.seed(123456789)
model <- embed_entityrelationspace(relations, dim = 50)
predict(model, "/m/027rn /location/country/form_of_government")
predict(model, "/m/06cx9 REVERSE_/location/country/form_of_government")
}
## cleanup for cran
if(file.exists(tmpfile)) file.remove(tmpfile)
Run the code above in your browser using DataLab