This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.品詞情報.Rmd
288 lines (257 loc) · 9.45 KB
/
2.品詞情報.Rmd
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
---
title: "Untitled"
author: "oushiei"
date: "2023-01-28"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# 語彙
```{r}
suppressPackageStartupMessages({
library(quanteda)
library(quanteda.textstats)
library(jiebaR)
library(readtext)
library(purrr)
library(tidyverse)
library(tidytext)
library(dplyr)
library(lattice)
library(showtext)
library(stringi)
library(patchwork)
library(tidyverse)})
```
```{r}
showtext_auto()
font_add("SourceHanSerif",regular = "/Library/Fonts/SourceHanSerif-Regular.ttc")
par(family = "SourceHanSerif")
library(showtext)
showtext_auto()
font_add("SourceHanSerif",regular = "/Library/Fonts/SourceHanSerif-Regular.ttc",
bold="SourceHanSerif-Bold.ttc")
theme_ou <- function(){
font <- "SourceHanSerif" #assign font family up front
theme_minimal() %+replace% #replace elements we want to change
theme(
panel.grid.major = element_blank(), #strip major gridlines
panel.grid.minor = element_blank(), #strip minor gridlines
axis.ticks = element_blank(), #strip axis ticks
plot.title = element_text( #title
family = font, #set font family
size = 20, #set font size
face = 'bold', #bold typeface
hjust = 0, #left align
vjust = 0), #raise slightly
plot.subtitle = element_text( #subtitle
family = font, #font family
size = 14), #font size
plot.caption = element_text( #caption
family = font, #font family
size = 9, #font size
hjust = 1), #right align
axis.title = element_text( #axis titles
family = font, #font family
size = 15,
face='bold'), #font size
axis.text = element_text( #axis text
family = font, #axis famuly
size = 15), #font size
axis.text.x = element_text( #margin for axis text
margin=margin(5, b = 10)),
legend.text = element_text(size=15,
family = font,,face='bold'),
strip.background = element_blank(),
strip.text.x = element_blank()
)
}
```
# nlplr形態素解析器よりpos済みのテキストを読み込み
```{r}
read <- readtext("/Users/oushiei/Desktop/论文终稿/finalcode/data/pos",docvarsfrom = "filenames")
readc <- read %>% corpus %>% tidy
readc %>% head()
```
# 形態素と品詞情報を別々のcolumに保存する
```{r}
postoken <- readc%>%unnest_tokens(word, text, token = stringr::str_split, pattern = " ")
postoken0 <- postoken %>% unnest_tokens(pos,word,token = stringr::str_split,pattern="/")
postoken0
pos <- postoken %>% ##
separate(word,
into = c("词语","pos"),
sep = "/",
extra = "drop")
pos %>%slice(1000:1020)
```
# 延べ語数と品詞情報を数える
```{r}
#==========
# 翻訳者ごとに品詞を集計する
#==========
#形容詞、名詞、動詞、数量詞それぞれはa\n\v\qで始まる
a <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^a+")) %>% count(name = "形容词")
n <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^n+")) %>% count(name = "名词")
v <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^v+")) %>% count(name = "动词")
q <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^q+")) %>% count(name = "量词")
a;n;v;q
anvq <- print(list(a,n,v,q) %>% reduce(inner_join, by='docvar1'))
#代名詞r、前置詞p、接続詞c、助詞u、感動詞e、副詞d
r <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^r+")) %>% count(name = "代词")
p <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^p+")) %>% count(name = "介词")
c <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^c+")) %>% count(name = "连词")
u <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^u+")) %>% count(name = "助词")
e <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^e+")) %>% count(name = "叹词")
d <- pos%>%group_by(docvar1) %>%
dplyr::filter(str_detect(pos, pattern = "^d+")) %>% count(name = "副词")
r;p;c;u;e;d
rpcued <- print(list(r,p,c,u,e,d) %>% reduce(inner_join, by='docvar1'))
```
# データの再構造
```{r}
anvq #実詞
rpcued #虚詞
token <- structure(list(token = c(吴树文 = 25218L, 曹曼 = 19718L,
杨爽 = 23894L, 林少华 = 20142L, 谭晶华徐建雄 = 25744L,
郑民钦 = 23192L)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L))
#==========
# 前節で集計したデータを利用しましょ
#==========
shici <- cbind(anvq,token)
xuci <- cbind(rpcued,token)
#==========
# 相対頻度に変換する
#==========
hin_shi <- shici %>% pivot_longer(.,2:5,names_to = "cixing") %>% group_by(docvar1) %>%
mutate(relative=value/token*100)
hin_xu <- xuci %>% pivot_longer(.,2:7,names_to = "cixing") %>% group_by(docvar1) %>%
mutate(relative=value/token*100)
#ここにある相対頻度を簡単な例で説明すれば、
#当テキストにある形容詞がのべ語数にしめる割合。
```
# 可視化
```{r}```{r}```{r}
hin_shi
options(digits = 2)
ggplot(data=hin_shi,aes(cixing,relative,group=docvar1,fill=docvar1,label=relative))+
geom_bar(stat="identity",
position="dodge",width=0.9,size=0.5)+
geom_text(aes(cixing,relative,group=docvar1,label=round(relative,2)),
position = position_dodge(width = .9),vjust = -.25)+
scale_fill_manual(values=c("#DE3533", "#0047AB", "#006644",
"#10C25B", "#808080","#FF8000"))+
facet_wrap(~ cixing, scales = "free",)+
theme_ou()+
labs(title = "実詞の相対頻度",x="",y="")+
theme(
legend.position = "left"
)
ggplot(data=hin_xu,aes(cixing,relative,fill=docvar1,label=relative))+
geom_bar(stat="identity",
position="dodge",width=0.9,size=0.5)+
geom_text(aes(cixing,relative,group=docvar1,label=round(relative,2)),
position = position_dodge(width = .9),vjust = -.25)+
scale_fill_manual(values=c("#DE3533", "#0047AB", "#006644",
"#10C25B", "#808080","#FF8000"))+
theme_ou()+
facet_wrap(~ cixing, scales = "free",)+
labs(title = "虚詞の相対頻度",x="",y="")+
theme(
legend.position=0
)
```
# 実詞と虚詞とのべ語数の関係を掘り下げる
```{r}
sum_shi <- hin_shi %>% group_by(docvar1) %>% summarise(shi_sum =sum(relative))
sum_xu <- hin_xu %>% group_by(docvar1) %>% summarise(xu_sum =sum(relative))
sumtwo <- mutate(sum_shi,sum_xu,token) %>% rename(実詞=shi_sum,虚詞=xu_sum)
attach(sumtwo)
library(psych)
pairs.panels(sumtwo[,-1], cex.labels=3,pch=21, cex = 1, cex.axis = 2)
sumtwo[,-1] %>%
GGally::ggscatmat()
#==========
# barplot
#==========
sumtwo_longer <- sumtwo%>% pivot_longer(2:3,names_to = "品詞","valu")
sumtwo_longer
sumtwo_longer %>% ggplot(aes(x=factor(docvar1),y=value,group=`品詞`,fill=`品詞`))+
geom_bar(stat = "identity",position = "dodge")
sumtwo_longer
sumtwo_longer %>% ggplot(aes(x=factor(docvar1)))+
geom_bar(aes(y=token),stat = "identity",position = "dodge",fill="#006644")+
geom_line(aes(y=value*200,group=`品詞`,color=`品詞`))+
scale_y_continuous(
name = "First Axis",
sec.axis = sec_axis(~./200, name="Second Axis")
)+
theme_ou()+ theme(axis.title.x = element_blank())
#==========
# base r barplot + lineplot
#==========
sumtwo <- sumtwo %>% mutate(num =1:6)
attach(sumtwo)
barplot(sumtwo$token~ sumtwo$num,
main = "延べ語数",
xlab = "",
beside=T,
col="lightblue",
density=20,
names.arg = sumtwo$docvar1,
horiz = F)
sumtwo
par(new = TRUE) # Add new plot
plot(num, 実詞, pch = 1, col = "#484891", # Create second plot without axes
axes = FALSE, xlab = "", ylab = "",type="b")
par(new = TRUE)
lines(num, 虚詞, pch = 2, col = "#984B4B",
xlab = "", ylab = "",type="b" )
axis(side = 4, at = pretty(range(実詞)))
#==========
# 3d plot
#==========
library("plot3D")
sumtwo
attach(sumtwo)
library(scatterplot3d)
s1 <- sumtwo$実詞
s2 <- sumtwo$虚詞
s3 <- as.numeric(sumtwo$token)
s4 <- sumtwo$docvar1
scatterplot3d(s1,s3,s2,
xlab="断落数", ylab="字数", zlab="词数",
angle=45,main = "《红楼梦》")
text3D(s1,s3,s2,
labels = sumtwo$docvar1)
sumtwo
# Create a scatter plot
attach(sumtwo)
# Plot texts
par(family = "SourceHanSerif")
text3D(s1, s2, s3,
labels = s4, colvar = s3,
col = gg.col(80), theta = 60, phi = 5,
xlab = "x:実詞率%", ylab = "y:虚詞率%", zlab = "z:のべ語数",
main = "", cex = 1.2,cex.main = 2,cex.lab = 2,
bty = "b2", ticktype = "detailed", d = 2,
clab = c("のべ語数"), adj = 0.5, font = 2)
#“b”, “b2”, “f”, “g”, “bl”, “bl2”, “u”, “n”
scatter3D(s1, s2, s3,
theta = 60, phi = 25,
main = "", cex = 1.2,
bty = "b2", ticktype = "detailed", d = 2,
clab = c("のべ語数"), adj = 0.5, font = 2, type = "h",
ticktype = "detailed",add = T)
```