Delete script.R
Browse files
script.R
DELETED
@@ -1,196 +0,0 @@
|
|
1 |
-
library(tidyverse)
|
2 |
-
|
3 |
-
# ----- #
|
4 |
-
read_captions_from_directory <- function(directory_path) {
|
5 |
-
# ディレクトリ内の.txtファイルのリストを取得
|
6 |
-
txt_files <- list.files(directory_path, pattern = "\\.txt$", full.names = TRUE)
|
7 |
-
|
8 |
-
# 各.txtファイルからキャプションを読み込み
|
9 |
-
data <- lapply(txt_files, function(file) {
|
10 |
-
captions <- readLines(file, warn = FALSE)
|
11 |
-
captions_list <- strsplit(captions, ",")[[1]]
|
12 |
-
captions_list <- trimws(captions_list) # 余分な空白を取り除く
|
13 |
-
tibble(
|
14 |
-
image_path = gsub(".txt$", ".png", file),
|
15 |
-
caption_order = 1:length(captions_list),
|
16 |
-
caption = captions_list
|
17 |
-
)
|
18 |
-
})
|
19 |
-
|
20 |
-
# データフレームに変換
|
21 |
-
bind_rows(data)
|
22 |
-
}
|
23 |
-
|
24 |
-
get_caption_frequency <- function(data) {
|
25 |
-
data %>%
|
26 |
-
group_by(caption) %>%
|
27 |
-
summarise(frequency = n()) %>%
|
28 |
-
arrange(-frequency)
|
29 |
-
}
|
30 |
-
|
31 |
-
search_by_caption <- function(data, target_caption) {
|
32 |
-
data %>%
|
33 |
-
filter(caption == target_caption) %>%
|
34 |
-
group_by(image_path) %>%
|
35 |
-
distinct()
|
36 |
-
}
|
37 |
-
|
38 |
-
remove_caption_and_adjust_order <- function(data, target_image_path, target_caption) {
|
39 |
-
# キャプションが存在するか確認
|
40 |
-
if (!any(data$image_path == target_image_path & data$caption == target_caption)) {
|
41 |
-
cat(sprintf("The caption '%s' does not exist for image '%s'.\n", target_caption, target_image_path))
|
42 |
-
return(data)
|
43 |
-
}
|
44 |
-
|
45 |
-
# 削除するキャプションのcaption_orderを取得
|
46 |
-
removed_order <- data$caption_order[data$image_path == target_image_path & data$caption == target_caption]
|
47 |
-
# キャプションを削除
|
48 |
-
data <- data %>% filter(!(image_path == target_image_path & caption == target_caption))
|
49 |
-
# caption_orderを調整
|
50 |
-
data$caption_order[data$image_path == target_image_path & data$caption_order > removed_order] <- data$caption_order[data$image_path == target_image_path & data$caption_order > removed_order] - 1
|
51 |
-
|
52 |
-
return(data)
|
53 |
-
}
|
54 |
-
|
55 |
-
remove_low_frequency_captions <- function(data, threshold) {
|
56 |
-
# キャプションの頻度を取得
|
57 |
-
caption_freq <- get_caption_frequency(data)
|
58 |
-
|
59 |
-
# 指定された頻度以下のキャプションのリストを作成
|
60 |
-
low_freq_captions <- caption_freq %>%
|
61 |
-
filter(frequency <= threshold) %>%
|
62 |
-
pull(caption)
|
63 |
-
|
64 |
-
# 低頻度のキャプションを削除し、caption_orderを調整
|
65 |
-
for (caption in low_freq_captions) {
|
66 |
-
unique_images <- unique(data$image_path[data$caption == caption])
|
67 |
-
for (image in unique_images) {
|
68 |
-
data <- remove_caption_and_adjust_order(data, image, caption)
|
69 |
-
}
|
70 |
-
}
|
71 |
-
|
72 |
-
return(data)
|
73 |
-
}
|
74 |
-
|
75 |
-
edit_captions_interactively <- function(data, target_caption) {
|
76 |
-
# キャプションで画像を検索
|
77 |
-
image_paths <- search_by_caption(data, target_caption)$image_path
|
78 |
-
|
79 |
-
for (path in image_paths) {
|
80 |
-
# OSに応じて画像を開く
|
81 |
-
if (Sys.info()["sysname"] == "Windows") {
|
82 |
-
cmd <- sprintf('start "" "%s"', path)
|
83 |
-
shell(cmd, intern = TRUE)
|
84 |
-
} else if (Sys.info()["sysname"] == "Darwin") { # macOS
|
85 |
-
cmd <- sprintf('open "%s"', path)
|
86 |
-
system(cmd)
|
87 |
-
} else { # Linux
|
88 |
-
cmd <- sprintf('xdg-open "%s"', path)
|
89 |
-
system(cmd)
|
90 |
-
}
|
91 |
-
|
92 |
-
# ユーザーにキャプションの削除を選択させる
|
93 |
-
cat(sprintf("Do you want to remove the caption '%s' from image '%s'? (yes/no/end): ", target_caption, path))
|
94 |
-
response <- readline()
|
95 |
-
|
96 |
-
if (tolower(response) == "end") {
|
97 |
-
break
|
98 |
-
} else if (tolower(response) == "yes") {
|
99 |
-
data <- remove_caption_and_adjust_order(data, path, target_caption)
|
100 |
-
}
|
101 |
-
}
|
102 |
-
|
103 |
-
return(data)
|
104 |
-
}
|
105 |
-
|
106 |
-
add_caption_at_order <- function(data, target_image_path, target_caption, target_order = NULL) {
|
107 |
-
# 指定された画像の最大のcaption_orderを取得
|
108 |
-
max_order <- max(data$caption_order[data$image_path == target_image_path], na.rm = TRUE)
|
109 |
-
|
110 |
-
# キャプションの重複チェック
|
111 |
-
if (target_caption %in% data$caption[data$image_path == target_image_path]) {
|
112 |
-
return(data) # 重複がある場合、データをそのまま返す
|
113 |
-
}
|
114 |
-
|
115 |
-
# target_orderが指定されていない場合、キャプションを表示してユーザーに選ばせる
|
116 |
-
if (is.null(target_order)) {
|
117 |
-
print_image_captions_as_csv(data, target_image_path)
|
118 |
-
cat("Enter the position (order) to insert the new caption (1 to", max_order + 1, "): ")
|
119 |
-
target_order <- as.numeric(readline())
|
120 |
-
|
121 |
-
# 不適切な値が入力された場合、最大のorder + 1で追加
|
122 |
-
if (target_order <= 0 || target_order > max_order + 1) {
|
123 |
-
target_order <- max_order + 1
|
124 |
-
}
|
125 |
-
}
|
126 |
-
|
127 |
-
# 指定されたorder以降のcaption_orderを増加
|
128 |
-
data <- data %>%
|
129 |
-
mutate(caption_order = ifelse(image_path == target_image_path & caption_order >= target_order, caption_order + 1, caption_order))
|
130 |
-
|
131 |
-
# 新しいキャプションを追加
|
132 |
-
new_caption <- tibble(
|
133 |
-
image_path = target_image_path,
|
134 |
-
caption_order = target_order,
|
135 |
-
caption = target_caption
|
136 |
-
)
|
137 |
-
data <- bind_rows(data, new_caption)
|
138 |
-
|
139 |
-
return(data)
|
140 |
-
}
|
141 |
-
|
142 |
-
move_caption_order <- function(data, target_image_path, target_caption, new_order) {
|
143 |
-
|
144 |
-
# キャプションが存在するか確認
|
145 |
-
if (!any(data$image_path == target_image_path & data$caption == target_caption)) {
|
146 |
-
cat(sprintf("The caption '%s' does not exist for image '%s'.\n", target_caption, target_image_path))
|
147 |
-
return(data)
|
148 |
-
}
|
149 |
-
|
150 |
-
# キャプションを削除
|
151 |
-
data_after_removal <- remove_caption_and_adjust_order(data, target_image_path, target_caption)
|
152 |
-
|
153 |
-
# 新しい位置にキャプションを追加
|
154 |
-
data_after_addition <- add_caption_at_order(data_after_removal, target_image_path, target_caption, new_order)
|
155 |
-
return(data_after_addition)
|
156 |
-
}
|
157 |
-
|
158 |
-
# キャプションが存在するか確認 (使わないかも)
|
159 |
-
is_caption_present <- function(data, target_image_path, target_caption) {
|
160 |
-
return(any(data$image_path == target_image_path & data$caption == target_caption))
|
161 |
-
}
|
162 |
-
|
163 |
-
# すべてのキャプションを表示
|
164 |
-
print_all_unique_captions_as_csv <- function(data) {
|
165 |
-
# 重複なく全てのキャプションを取得
|
166 |
-
unique_captions <- unique(data$caption)
|
167 |
-
# CSV形式で表示
|
168 |
-
cat(paste(unique_captions, collapse = ", "), "\n")
|
169 |
-
}
|
170 |
-
|
171 |
-
print_image_captions_as_csv <- function(data, target_image_path) {
|
172 |
-
captions <- filter(data, image_path == target_image_path) %>%
|
173 |
-
arrange(caption_order) %>%
|
174 |
-
pull(caption)
|
175 |
-
|
176 |
-
cat(paste(captions, collapse = ", "), "\n")
|
177 |
-
}
|
178 |
-
|
179 |
-
# 代表するキャプションに集約
|
180 |
-
remove_related_captions_except_representative <- function(data, related_captions, representative_caption, target_image_path) {
|
181 |
-
|
182 |
-
# representative_captionがtarget_image_pathに紐づいているか確認
|
183 |
-
if (!any(data$image_path == target_image_path & data$caption == representative_caption)) {
|
184 |
-
cat(sprintf("The representative caption '%s' is not associated with image '%s'.\n", representative_caption, target_image_path))
|
185 |
-
return(data)
|
186 |
-
}
|
187 |
-
|
188 |
-
# target_image_pathに関連するキャプションを削除
|
189 |
-
for (caption in related_captions) {
|
190 |
-
if (caption != representative_caption) {
|
191 |
-
data <- remove_caption_and_adjust_order(data, target_image_path, caption)
|
192 |
-
}
|
193 |
-
}
|
194 |
-
|
195 |
-
return(data)
|
196 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|