/
strj-fill.R
152 lines (148 loc) · 4.27 KB
/
strj-fill.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
#' Fill Japanese iteration marks
#'
#' Fills Japanese iteration marks (Odori-ji) with their previous characters
#' if the element has more than 5 characters.
#'
#' @param text Character vector.
#' @returns A character vector.
#' @export
#' @examples
#' strj_fill_iter_mark(c(
#' "\u3042\u3044\u3046\u309d\u3003\u304b\u304d",
#' "\u91d1\u5b50\u307f\u3059\u309e",
#' "\u306e\u305f\u308a\u3033\u3035\u304b\u306a",
#' "\u3057\u308d\uff0f\u2033\uff3c\u3068\u3057\u305f"
#' ))
strj_fill_iter_mark <- function(text) {
text <- text %>%
stringi::stri_omit_empty_na() %>%
stringi::stri_replace_all_regex("(\uff0f\u2033\uff3c)", "\u3034\u3035") %>%
stringi::stri_replace_all_regex("(\uff0f\uff3c)", "\u3033\u3035")
purrr::map_chr(text, function(res) {
if (nchar(res) > 4) {
while (stringi::stri_detect_regex(res, "[\u30fd\u309d\u3003\u30fe\u309e\u3033\u3034\u3035]")) {
res <- magrittr::freduce(
res,
list(fill_iter_mark_double2, fill_iter_mark_double, fill_iter_mark_single2, fill_iter_mark_single)
)
}
}
unlist(res)
})
}
#' @noRd
fill_iter_mark_single <- function(text) {
if (stringi::stri_detect_regex(text, "[\u30fd\u309d\u3003]")) {
textloop <- stringi::stri_split_boundaries(text, type = "character") %>%
purrr::map(~ embed(., 2)[, 2:1]) %>%
purrr::map(~ as.data.frame(.))
purrr::map(textloop, function(df) {
df <- df %>%
dplyr::mutate(
V2 = dplyr::if_else(
stringi::stri_detect_regex(df$V2, "[\u30fd\u309d\u3003]"),
.data$V1,
.data$V2
)
)
stringi::stri_join(c(df[1, 1], df$V2), collapse = "")
})
} else {
text
}
}
#' @noRd
fill_iter_mark_single2 <- function(text) {
if (stringi::stri_detect_regex(text, "[\u30fe\u309e]")) {
textloop <- stringi::stri_split_boundaries(text, type = "character") %>%
purrr::map(~ embed(., 2)[, 2:1]) %>%
purrr::map(~ as.data.frame(.))
purrr::map(textloop, function(df) {
df <- df %>%
dplyr::mutate(
V2 = dplyr::if_else(
stringi::stri_detect_regex(df$V2, "[\u30fe\u309e]"),
paste0(.data$V1, enc2utf8("\uff9e")),
.data$V2
)
)
stringi::stri_join(c(df[1, 1], df$V2), collapse = "")
})
} else {
text
}
}
#' @noRd
fill_iter_mark_double <- function(text) {
if (stringi::stri_detect_regex(text, "(\u3033\u3035)")) {
textloop <- stringi::stri_split_boundaries(text, type = "character") %>%
purrr::map(function(x) {
embed(x, 4)[, 4:1]
}) %>%
purrr::map(function(x) {
as.data.frame(x)
})
purrr::map(textloop, function(df) {
df <- df %>%
dplyr::mutate(
V3 = dplyr::if_else(
stringi::stri_detect_regex(df$V3, "[\u3033]"),
.data$V1,
.data$V3
),
V4 = dplyr::if_else(
stringi::stri_detect_regex(df$V4, "[\u3033]"),
.data$V2,
.data$V4
)
) %>%
dplyr::mutate(
V4 = dplyr::if_else(
stringi::stri_detect_regex(df$V4, "[\u3035]"),
.data$V2,
.data$V4
)
)
stringi::stri_join(c(df[1, 1:3], df$V4), collapse = "")
})
} else {
text
}
}
#' @noRd
fill_iter_mark_double2 <- function(text) {
if (stringi::stri_detect_regex(text, "(\u3034\u3035)")) {
textloop <- stringi::stri_split_boundaries(text, type = "character") %>%
purrr::map(function(x) {
embed(x, 4)[, 4:1]
}) %>%
purrr::map(function(x) {
as.data.frame(x)
})
purrr::map(textloop, function(df) {
df <- df %>%
dplyr::mutate(
V3 = dplyr::if_else(
stringi::stri_detect_regex(df$V3, "[\u3034]"),
paste0(.data$V1, enc2utf8("\uff9e")),
.data$V3
),
V4 = dplyr::if_else(
stringi::stri_detect_regex(df$V4, "[\u3034]"),
paste0(.data$V2, enc2utf8("\uff9e")),
.data$V4
)
) %>%
dplyr::mutate(
V4 = dplyr::if_else(
stringi::stri_detect_regex(df$V4, "[\u3035]"),
.data$V2,
.data$V4
)
)
stringi::stri_join(c(df[1, 1:3], df$V4), collapse = "")
})
} else {
text
}
}