如何按作者分组计算摘要间Jaccard指数及作者专业化程度
解决步骤与代码实现
1. 数据预处理:提取摘要的唯一词集合
首先需要将每篇摘要文本转换为唯一词集合,这里统一文本大小写、去除标点后分词去重(规则可按需调整):
# 模拟输入数据(真实数据可通过read.table/read.csv读取) df <- data.frame( AuthorId = c(2163160142, 2218860995, 2186116765, 2183638412, 2797413770, 2185043195, 2285431655, 2228363342), abstract = c( "We propose a new version of HLLEM approximate Riemann solver where loca...", "PLM is today a reality for mechanical SMEs. Some companies...", "Classic surgical interruption of patent ductus arteriosus was partially...", "Nanotechnology is currently undergoing rapid development partly due to the...", "The first aim of this article is to understand how the preschool teacher’s...", "In the lobster Homarus, the 2 identified PS neurons have a strong suppressive...", "Introduction: La polymedication des personnes âgees est un probleme sanitaire...", "A ring opening polymerization process allowing the fast and controlled..." ), stringsAsFactors = FALSE ) # 定义函数:将摘要转为唯一词集合 get_unique_words <- function(text) { # 去除标点并转小写 cleaned_text <- tolower(gsub("[[:punct:]]", "", text)) # 分词后去重 unique(strsplit(cleaned_text, "\\s+")[[1]]) } # 给数据框添加词集合列 df$word_set <- lapply(df$abstract, get_unique_words)
2. 按作者分组计算两两Jaccard指数
使用dplyr按作者分组,对每个作者的摘要词集合生成两两组合,计算Jaccard指数:
library(dplyr) # 分组计算每个作者的所有两两Jaccard指数 author_jaccard <- df %>% group_by(AuthorId) %>% summarise( jaccard_pairs = list( # 仅当作者有2篇及以上摘要时计算 if(n() >= 2) { unlist(lapply(combn(word_set, 2, simplify = FALSE), function(pair) { intersect_len <- length(intersect(pair[[1]], pair[[2]])) union_len <- length(union(pair[[1]], pair[[2]])) intersect_len / union_len })) } else { numeric(0) } ), # 记录作者的摘要数量 n_papers = n() )
3. 计算专业化程度
根据需求(即作者所有两两摘要Jaccard指数的平均值),计算每位作者的专业化程度:
author_specialization <- author_jaccard %>% mutate( specialization = ifelse(n_papers >= 2, # 平均值 = Jaccard总和 / 两两组合数(组合数为n*(n-1)/2) sum(jaccard_pairs) / (n_papers * (n_papers - 1) / 2), # 仅1篇摘要时无法计算,设为NA(可按需调整为1或其他值) NA_real_) ) # 查看最终结果 print(author_specialization)
补充说明
- 分词规则可按需优化,比如去除停用词、添加词干提取等,提升词集合的准确性。
- 若作者只有1篇摘要,可根据业务需求将专业化程度设为
1(代表完全专业化)或保留NA。
内容的提问来源于stack exchange,提问作者Yassine Moudene
相关产品推荐
相关产品推荐

