library("tm") #text mining
library("RWeka") # for tokenization algorithms more complicated than single-word
corpus <- read.csv("HeartBleed_4400_Labeled_AllCleanTDM.csv")
df <- data.frame(corpus)
require(tm)
mycorpus <- Corpus(DataframeSource(df))
BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
tdm <- TermDocumentMatrix(mycorpus, control = list(tokenize = BigramTokenizer))
#tdm <- removeSparseTerms(tdm, 0.99)
arr=findFreqTerms(tdm, lowfreq=3, highfreq=Inf)
library(devtools)
install_github("mukul13/rword2vec")
library(rword2vec)
ls("package:rword2vec")
model=word2vec(train_file = "Data",output_file = "vec.bin",binary=1)

for (i in 1:20){
  dist=distance(file_name = "vec.bin",search_word = arr[i],num = 20)
  write.table(t(as.vector(unlist(dist$word))), "Top20HeartBleed_4400_Labeled_AllClean.csv", sep = ",", col.names = !file.exists("Top20HeartBleed_4400_Labeled_AllClean.csv"), append = T)
}
