This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processRhymes.R
184 lines (163 loc) · 5.32 KB
/
processRhymes.R
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
getRhymes <- function(data) {
# This function takes a list/column of data table, and then generate another data table with vowels of those characters
#
# Args:
# data A list or a column of a data table containing rhyming characters
# Returns:
# A data table consists of the vowels of those rhyming characters
# Side-effects:
# None
DTRhymes <- data.table(char = character(), rhymes = character())
vocabulary <-
str_split(data, '') %>%
unlist() %>%
unique()
# Only take the first vowel if there're multiple available
# Return NA if it's not found
for (c in vocabulary) {
rhm <-
glue(
'http://ytenx.org/zim?dzih={c}&kyonh=1&jtkb=1&jtdt=1&jtkd=1&jtgt=1'
) %>%
read_html() %>%
html_node('p.yonh a') %>%
html_attr('href')
if (!is.na(rhm)) {
rhm <-
glue('http://ytenx.org{rhm}') %>%
read_html() %>%
html_table() %>% .[[1]]
rhm <-
rhm[which(rhm$'X1' == '韻攝'),2]
} else {
rhm <- 'X'
}
cat(paste0(c,' --> ', rhm, '...\n'))
DTRhymes <- rbindlist(list(DTRhymes,
data.table(char = c, rhymes = rhm)))
}
return(DTRhymes)
}
matchRymes <- function(data, dict) {
# This function takes a list/column of data table, and then return a list of vowels for each character, according to the second argument, a data table as a look-up table
#
# Args:
# data A list or a column of a data table containing rhyming characters
# dict A data table functioning as a look-up table
# Returns:
# A list of list with vowels for each character
# Side-effects:
# None
map(data,
function(x) {
nextRhyme <- character()
for (i in unlist(str_split(x, ''))) {
nextRhyme <- c(nextRhyme, dict[char == i]$rhymes)
}
nextRhyme <- paste0(nextRhyme, collapse = '')
})
}
if (!file.exists('rhymes.csv')) {
DTRhymes <- getRhymes(DT$lastChar)
fwrite(DTRhymes, 'rhymes.csv')
} else {
DTRhymes <- fread(file = 'rhymes.csv')
}
DT[ , 'rhymes' := lapply(DT[,.(lastChar)],
matchRymes,
DTRhymes)]
DT[ , 'rhymes' := lapply(.SD$rhymes,
function(x) (
str_split(x, '') %>%
lapply(function(x) paste0(x, ' ')) %>%
unlist() %>%
reduce(str_c) %>%
str_remove_all(' $')))]
allEqualRhyme <- function(x) {
#len <- str_count(x, ' ')+1
return(length(unique(unlist(str_split(x, ' ')))) > 1)
# if (length(stri_unique(unlist(str_split(x, ' ')))) == 1) {
# return(1)
# } else {
# return(0)
# }
}
getRhymes <- function(data) {
# This function takes a list/column of data table, and then generate another data table with vowels of those characters
#
# Args:
# data A list or a column of a data table containing rhyming characters
# Returns:
# A data table consists of the vowels of those rhyming characters
# Side-effects:
# None
DTRhymes <- data.table(char = character(), rhymes = character())
vocabulary <-
str_split(data, '') %>%
unlist() %>%
unique()
# Only take the first vowel if there're multiple available
# Return NA if it's not found
for (c in vocabulary) {
rhm <-
glue(
'http://ytenx.org/zim?dzih={c}&kyonh=1&jtkb=1&jtdt=1&jtkd=1&jtgt=1'
) %>%
read_html() %>%
html_node('p.yonh a') %>%
html_attr('href')
if (!is.na(rhm)) {
rhm <-
glue('http://ytenx.org{rhm}') %>%
read_html() %>%
html_table() %>% .[[1]]
rhm <-
rhm[which(rhm$'X1' == '韻攝'),2]
} else {
rhm <- 'X'
}
cat(paste0(c,' --> ', rhm, '...\n'))
DTRhymes <- rbindlist(list(DTRhymes,
data.table(char = c, rhymes = rhm)))
}
return(DTRhymes)
}
matchRymes <- function(data, dict) {
# This function takes a list/column of data table, and then return a list of vowels for each character, according to the second argument, a data table as a look-up table
#
# Args:
# data A list or a column of a data table containing rhyming characters
# dict A data table functioning as a look-up table
# Returns:
# A list of list with vowels for each character
# Side-effects:
# None
map(data,
function(x) {
nextRhyme <- character()
for (i in unlist(str_split(x, ''))) {
nextRhyme <- c(nextRhyme, dict[char == i]$rhymes)
}
nextRhyme <- paste0(nextRhyme, collapse = '')
})
}
if (!file.exists('rhymes.csv')) {
DTRhymes <- getRhymes(DT$lastChar)
fwrite(DTRhymes, 'rhymes.csv')
} else {
DTRhymes <- fread(file = 'rhymes.csv')
}
DT[ , 'rhymes' := lapply(DT[,.(lastChar)],
matchRymes,
DTRhymes)]
DT[ , 'rhymes' := lapply(.SD$rhymes,
function(x) (
str_split(x, '') %>%
lapply(function(x) paste0(x, ' ')) %>%
unlist() %>%
reduce(str_c) %>%
str_remove_all(' $')))]
allEqualRhyme <- function(x) {
return(length(unique(unlist(str_split(x, ' ')))) == 1)
}
DT[ , 'correctRhyming' := unlist(lapply(.SD$rhymes, allEqualRhyme))]