-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDF论文NLP自然语言分析可视化.R
More file actions
86 lines (61 loc) · 2.33 KB
/
Copy pathPDF论文NLP自然语言分析可视化.R
File metadata and controls
86 lines (61 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
###############################################################
#
# PDF论文文本数据挖掘
#
###############################################################
#文本数据挖掘NLP三大核心 主题提取,词云图,情感分析;
library(pdftools)
library(stringr)
library(ggplot2)
library(wordcloud)
# 假设PDF文件名为 "example.pdf"
pdf_file <- "//Users//wulixin//Desktop//银行外源增资、资产负债表调整与风险承担行为_刘岩.pdf"
pdf_text <- pdftools::pdf_text(pdf_file)
# 将提取的文本转换为UTF-8编码(如果需要)
pdf_text_utf8 <- iconv(pdf_text, from = "当前编码", to = "UTF-8")
pdf_text_utf8<-pdf_text
# 去除空白行和不必要的字符
cleaned_text <- str_trim(str_split(pdf_text_utf8, "\n")[[1]])
cleaned_text <- cleaned_text[cleaned_text != ""]
library(jiebaR)
library(jiebaRD)
# 初始化jiebaR分词器(只需执行一次)
cutter = worker()
# 使用jiebaR进行分词
segmented_words <- segment(cleaned_text, cutter )
# 将分词结果转换为字符向量(如果需要去除空格等,可以进一步处理)
segmented_words_clean <- str_trim(segmented_words)
segmented_words_r <- segmented_words_clean[segmented_words_clean != ""]
# 统计词频
word_freq <- table(segmented_words_r)
# 转换为数据框
df <- as.data.frame(word_freq)
colnames(df) <- c("word", "freq")
library(tm)
# 创建一个文本向量(用于tm包处理)
texts <- Corpus(VectorSource(cleaned_text))
# 进行词频统计等分析(这里省略具体步骤)
library(ggcorrplot)
library(corrplot)
library(showtext)
library(plotly)
showtext_auto(enable=TRUE)
font_add('Songti','Songti.ttc')
font_families()
stopwords <- c("的", "了", "啊", "是", "与", "用于", "进行", "我们","年")
# 过滤掉停用词
df<- df%>%filter(!word %in% stopwords)
# 使用ggplot2绘制词频条形图
ggplot(df%>%filter(freq>4), aes(x = reorder(word, -freq), y = freq)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(title = "词频条形图", x = "单词", y = "频率") +
theme_minimal()
# 使用wordcloud绘制词云图
set.seed(123) # 为了可重复性
wordcloud(words = df$word, freq = df$freq, min.freq = 1,
max.words = 200, random.order = FALSE, rot.per = 0.35,
colors = brewer.pal(8, "Dark2"))
########词云图可视化
library(d3wordcloud)
d3wordcloud(df$word, df$Freq)