-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.R
More file actions
201 lines (159 loc) · 6.28 KB
/
Script.R
File metadata and controls
201 lines (159 loc) · 6.28 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
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
# Install and or load packages - set variables ---------------------------------
packages <- c("pdftools","reticulate")
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
install.packages(packages[!installed_packages])
}
library(pdftools)
library(reticulate)
library(TheOpenAIR)
library(ds4psy)
library(glue)
library(factoextra)
# Initialize virtual ENV for Python
Sys.setenv(RETICULATE_PYTHON_ENV = "py_backend")
version <- "3.11"
virtualenv_create(python = virtualenv_starter(version))
virtualenv_install(requirements = "requirements.txt")
use_virtualenv("py_backend")
# Get your python file
source_python("aa_tokenizer.py")
source_python("aa_summarization.py")
source_python("aa_embedding.py")
outputpath = getwd()
inputpath = "Testfiles/1.pdf"
token = ""
################################################################################
# Short text summarization
################################################################################
# Load PDF file and parse ------------------------------------------------------
txt = pdf_text(inputpath)
document = txt[2] # select the page you want to summarize
# Call Aleph Alpha API ---------------------------------------------------------
maximum_tokens = 170
summary = summary(token, document, as.integer(maximum_tokens))
summary
################################################################################
# Long text summarization
################################################################################
# Load PDF file and parse ------------------------------------------------------
# This step can be optimized by checking better OCR parsing libraries or adding procedures beforehand
txt = pdf_text(inputpath)
maximum_tokens = 74
# processing of PDF file to text
stringtosum = ""
for (x in 1:length(txt)) {
stringtosum = paste(stringtosum, txt[x], sep = " ", collapse = " ")
}
sumtokeninput = count_tokens(stringtosum)
# count tokens per page and total
dft = data.frame(page="", tokens="")
x = 1
for (x in 1:length(txt)) {
string = gsub("[\r\n]", "", txt[i]) # remove new lines
#tokens = aa_tokenizer(token, string)
tokens = count_tokens(string)
tupel = as.integer(c(x, tokens))
dft = rbind(dft, tupel)
}
dft = dft[-1,]
dft$tokens <- as.integer(dft$tokens)
sumtokeninput <- sum(dft[, 'tokens'])
# Chunking algorithm -----------------------------------------------------------
string = paste(txt, sep="", collapse="")
string = gsub("[\r\n]", "", string) # remove new lines
dftext = text_to_sentences(string, split_delim = "\\.") # only split at "."
dftext = as.data.frame(dftext)
colnames(dftext) = "chunks"
chunksize = 5
overlap = 1
cutsize = chunksize - overlap
iterations = round(nrow(dftext)/cutsize)-1
df = data.frame(matrix(nrow = 0, ncol = 0))
for (x in 1:iterations){
chunk = paste(dftext[c(1:chunksize),], sep="", collapse="")
df = rbind(df, chunk)
dftext = as.data.frame(dftext[-c(1:cutsize), ])
}
if (nrow(dftext) > 0) {
chunk = paste(dftext[c(1:nrow(dftext)),], sep="", collapse="")
df = rbind(df, chunk)
}
colnames(df) <- c("Text_chunk")
text_chunks = as.list(df[,1])
vectors = embedding(token, text_chunks)
vectors
vectors = matrix(unlist(vectors), ncol = 5120, byrow = TRUE)
fviz_nbclust(vectors, kmeans, method = "silhouette") +
geom_vline(xintercept = "", linetype = 2)
# Clustering algorithm ---------------------------------------------------------
# Presummary -------------------------------------------------------------------
set.seed(123)
groupsize = 2
km.res <- kmeans(vectors, groupsize, nstart = 25)
df2 <- cbind(df, cluster = km.res$cluster)
presummarydf = data.frame(matrix(nrow = 0, ncol = 0))
for (x in 1:nrow(df2)) {
document = df2[x,1]
source_python("aa_summarization.py")
summary = summary(token, document, as.integer(maximum_tokens))
print(summary)
presummarydf = rbind(presummarydf, summary)
}
for (x in 1:nrow(presummarydf)) {
stringtosum = paste(stringtosum, presummarydf[x,1], sep = " ", collapse = " ")
}
sumtokenoutput = 0
sumtokenoutput = sumtokenoutput + count_tokens(stringtosum)
df2 <- cbind(df2, presummarydf)
colnames(df2) <- c("Text_chunk", "cluster", "Summary")
# Create summary per cluster ---------------------------------------------------
clustersummarydf = data.frame(matrix(nrow = 0, ncol = 0))
x = 0
for (x in 1:groupsize) {
#x = 2
group = df2[df2$cluster == x,]
document = ""
stringtosum = ""
for (y in 1:nrow(group)) {
stringtosum = paste(stringtosum, group[y,3], sep = " ", collapse = " ")
document = stringtosum
}
clustersummarydf = rbind(clustersummarydf, document)
}
colnames(clustersummarydf) <- c("Clustersummary")
finalsummarydf = data.frame(matrix(nrow = 0, ncol = 0))
maximum_tokens = 350
for (x in 1:nrow(clustersummarydf)) {
document = clustersummarydf[x,1]
source_python("aa_summarization.py")
summary = summary(token, document, as.integer(maximum_tokens))
print(summary)
finalsummarydf = rbind(finalsummarydf, summary)
}
colnames(finalsummarydf) <- c("Finalsummary")
finalsummarydf[1,1]
# Guided summary ---------------------------------------------------------------
document = ""
stringtosum = ""
for (x in 1:nrow(clustersummarydf)) {
stringtosum = paste(stringtosum, clustersummarydf[x,1], sep = " ", collapse = " ")
document = stringtosum
}
maximum_tokens = 350
question1 = "Was ist der Hintergrund?"
source_python("aa_summarization_guided.py")
background = summaryguided(token, document, question1, as.integer(maximum_tokens))
background
question2 = "Ist das Urteil aus Sicht der Krankenkasse für die Abrechnungsprüfung relevant? Ist der Rechnungsbetrag zu kürzen? Wenn ja, aus welchem Grund?"
resolution = summaryguided(token, document, question2, as.integer(maximum_tokens))
resolution
guidedsummary = data.frame(matrix(nrow = 0, ncol = 3))
vector1 = cbind("Background", question1, background)
vector2 = cbind("Resolution", question2, resolution)
df3 = as.data.frame(rbind(vector1, vector2))
colnames(df3) <- c("Part","Question","Summary")
# Cost evaluation --------------------------------------------------------------
#summary = glue("BACKGROUND: {background} RESOLUTION: {resolution}")
# sumtokeninput = sumtokeninput + count_tokens(document)
# sumtokenoutput = sumtokenoutput + count_tokens(document) + count_tokens(background) + count_tokens(resolution)