为什么text2vec显示的文件多于实际存在的文件?

时间:2019-07-01 11:18:39

标签: r similarity text2vec

我正在测试text2vec。目录下只有2个文件(1.txt,2.txt,文件很小,每个文件约20 k)。我想测试它们的相似性。我不知道为什么会说54个文件。

> library(stringr)
>  library(NLP)
>  library(tm)
>  library(text2vec)


>  filedir="F:\\0 R\\similarity test\\corpus"
>  prep_fun = function(x) {
+     x %>% 
+     # make text lower case
+     str_to_lower %>% 
+     # remove non-alphanumeric symbols
+     str_replace_all("[^[:alnum:]]", " ") %>% 
+     # collapse multiple spaces
+     str_replace_all("\\s+", " ")
+  }
>  allfile=idir(filedir)
>  #files=list.files(path=filedir, full.names=T)
>  #allfile=ifiles(files)
>  it=itoken(allfile, preprocessor=prep_fun, progressbar=F)
>  stopwrd=stopwords("en")
>  v=create_vocabulary(it, stopwords=stopwrd)
> v
Number of docs: 54 
174 stopwords: i, me, my, myself, we, our ... 
ngram_min = 1; ngram_max = 1 
Vocabulary: 
          term term_count doc_count
  1:     house          2         2
  2: 224161072          2         2
  3:  suggests          2         2
  4:   remains          2         2
  5: published          2         2
 ---                               
338:      year         14         6
339:       nep         16        12
340:      will         16        10
341:   chinese         20        12
342:     malay         20        10
> 

我将数据导出到csv中,发现新文件名为:

1.txt_1
1.txt_2
1.txt_3
1.txt_4
...

...

如果我使用

#files=list.files(path=filedir, full.names=T)
#allfile=ifiles(files)

它仍然显示54个文档

它们之间也有相似性度量。其中大多数相似度为0。

请让我知道是否应该是这种情况。

我想要的只是1.txt和2.txt的一种相似性度量,并输出仅包含这两个文件的度量值的矩阵。

1 个答案:

答案 0 :(得分:3)

text2vec将每个文件中的每一行视为一个单独的文档。对于您的情况,我建议为idir / ifiles函数提供另一个reader函数。读者应该只读取整个文件并将行折叠为单个字符串。 (例如,读者=函数(x)粘贴(readLines(x),塌陷=''))

相关问题