我有一个 doc/docx 文档流,我需要计算其中的字数。
到目前为止的过程是手动打开文档并记下 MS Word 本身提供的字数统计,我正在尝试使用 R 使其自动化。
这是我尝试过的:
library(textreadr)
library(stringr)
myDocx = read_docx(myDocxFile)
docText = str_c(myDocx , collapse = " ")
wordCount = str_count(test, "\\s+") + 1
不幸的是,wordCount
不是 MS Word 建议的。
例如,我注意到 MS Word 计算编号列表中的数字,而 textreadr
甚至不导入它们。
有解决办法吗?我也不介意在 Python 中尝试一些东西,尽管我在这方面的经验较少。
如有任何帮助,我们将不胜感激。
最佳答案
这应该可以使用 R 中的 tidytext
包来完成。
library(textreadr)
library(tidytext)
library(dplyr)
#read in word file without password protection
x <- read_docx(myDocxFile)
#convert string to dataframe
text_df <-tibble(line = 1:length(x),text = x)
#tokenize dataframe to isolate separate words
words_df <- text_df %>%
unnest_tokens(word,text)
#calculate number of words in passage
word_count <- nrow(words_df)
关于python - 获取 R 中 doc/docx 文件的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644947/