Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用单词簇值转换字符串列_R_Nlp_Cluster Computing - Fatal编程技术网

R 使用单词簇值转换字符串列

R 使用单词簇值转换字符串列,r,nlp,cluster-computing,R,Nlp,Cluster Computing,我正在R中做一些基本的NLP工作。我有两个数据集,希望用每个单词的聚类值替换其中一个中的单词 第一个数据集保存句子,第二个数据集保存每个单词的聚类值(假设第一个数据集中的每个单词都有一个聚类值): 寻找一个有效的解决方案,因为我有很多长句。谢谢 您可以尝试以下方法: library(tidyr) library(dplyr) library(stringi) df1 <- unnest(stri_split_fixed(original_text_df$Text, ' '), group

我正在R中做一些基本的NLP工作。我有两个数据集,希望用每个单词的聚类值替换其中一个中的单词

第一个数据集保存句子,第二个数据集保存每个单词的聚类值(假设第一个数据集中的每个单词都有一个聚类值):


寻找一个有效的解决方案,因为我有很多长句。谢谢

您可以尝试以下方法:

library(tidyr)
library(dplyr)
library(stringi)

df1 <- unnest(stri_split_fixed(original_text_df$Text, ' '), group) %>%
  group_by(x) %>% mutate(cluster = cluster_df$Cluster[cluster_df$Word %in% x]) 
在此基础上,为了匹配预期的输出,您可以使用
split()
为每个组(句子)构建一个集群列表,并重建一个数据帧:

l <- split(df1$cluster, f = df1$group)
df2 <- data.frame(Text = do.call(rbind, lapply(l, paste0, collapse = " ")))

你可以参考几个月前我问过的一个非常类似的例子,其中展示了许多其他建议。

除了你用任意“集群值”创建的MWE之外,如何计算这些值?也就是说,什么包和函数从字符串向量创建集群值?是的,我想我们需要更多的信息来回答这个问题…不是一个R包-来自Pythontanks中的Gensim,用于您的解决方案和链接-我的问题是复杂的,因为两端的字符串分裂处理不规则的单词数。
library(tidyr)
library(dplyr)
library(stringi)

df1 <- unnest(stri_split_fixed(original_text_df$Text, ' '), group) %>%
  group_by(x) %>% mutate(cluster = cluster_df$Cluster[cluster_df$Word %in% x]) 
#Source: local data frame [8 x 3]
#Groups: x
#
#  group    x cluster
#1    X1 this       2
#2    X1   is       2
#3    X1 some       3
#4    X1 text       4 
#5    X2 this       2
#6    X2   is       2
#7    X2 more       3
#8    X2 text       4
l <- split(df1$cluster, f = df1$group)
df2 <- data.frame(Text = do.call(rbind, lapply(l, paste0, collapse = " ")))
#      Text
#X1 2 2 3 4
#X2 2 2 3 4