2016-09-15 47 views
2

基於text2vec包的小插圖,提供了一個用於創建文字嵌入的示例。將wiki數據標記化,然後創建術語共生矩陣(TCM),用於創建單詞使用包裝中提供的手套功能嵌入。 我想爲隨包提供的電影評論數據構建詞嵌入。我的問題是:準備word2vec中的文字嵌入R包

  1. 我是否需要將所有電影評論摺疊成一個長字符串,然後進行標記。

這將導致2條評論之間的邊界標記共現,這是沒有意義的。

**vignettes code:** 
library(text2vec) 
library(readr) 
temp <- tempfile() 
download.file('http://mattmahoney.net/dc/text8.zip', temp) 
wiki <- read_lines(unz(temp, "text8")) 
unlink(temp) 
# Create iterator over tokens 
tokens <- strsplit(wiki, split = " ", fixed = T) 
# Create vocabulary. Terms will be unigrams (simple words). 
vocab <- create_vocabulary(itoken(tokens)) 
vocab <- prune_vocabulary(vocab, term_count_min = 5L) 
# We provide an iterator to create_vocab_corpus function 
it <- itoken(tokens) 
# Use our filtered vocabulary 
vectorizer <- vocab_vectorizer(vocab, 
           # don't vectorize input 
           grow_dtm = FALSE, 
           # use window of 5 for context words 
           skip_grams_window = 5L) 
tcm <- create_tcm(it, vectorizer) 
fit <- glove(tcm = tcm, 
      word_vectors_size = 50, 
      x_max = 10, learning_rate = 0.2, 
      num_iters = 15) 

我感興趣的開發的嵌入字爲能搞到的數據如下:

library(text2vec) 
data("movie_review") 

回答

3

不,你並不需要串聯評論。你只需要到超過令牌從正確的迭代器構造tcm

library(text2vec) 
data("movie_review") 
tokens = movie_review$review %>% tolower %>% word_tokenizer 
it = itoken(tokens) 
# create vocabulary 
v = create_vocabulary(it) %>% 
    prune_vocabulary(term_count_min = 5) 
# create co-occurrence vectorizer 
vectorizer = vocab_vectorizer(v, grow_dtm = F, skip_grams_window = 5) 

現在我們需要重新初始化(穩定的0.3版用於開發0.4不需要重新初始化迭代器):

it = itoken(tokens) 
tcm = create_tcm(it, vectorizer) 

飛度型號:

fit <- glove(tcm = tcm, 
      word_vectors_size = 50, 
      x_max = 10, learning_rate = 0.2, 
      num_iters = 15) 
+0

謝謝您的回答。這正是我所希望的。 – amitkb3