Skip to content

Commit 06d7beb

Browse files
committed
Final version of imprecise Gutenberg_cleaning and
first version of precise Gutenberg_cleaning
1 parent 587e299 commit 06d7beb

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

text_extraction_cleaning/Gutenberg_cleaning.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
fexi_pattern = re.compile(r'ΦΕΞΗ|ΒΙΒΛΙΟΘΗΚΗ|ΒΙΒΛΙΟΠΩΛΕΙΟΝ|^ΕΚΔΟΤΗΣ|ΕΝ ΑΘΗΝΑΙΣ|ΤΥΠΟΓΡΑΦΕΙΟΥ|ΒΙΒΛΙΟΓΡΑΦΙΑ|ΠΙΝΑΞ ΤΩΝ ΕΙΚΟΝΩΝ')
99
exception_to_fexi_intro_pattern = re.compile(r'ΕΚΔΟΤΙΚΟΣ ΟΙΚΟΣ ΓΕΩΡΓΙΟΥ Δ. ΦΕΞΗ')
1010
unique_line_remove_pattern = re.compile(r'ΒΑΣΙΛΙΚΟΝ ΤΥΠΟΓΡΑΦΕΙΟΝ')
11-
fexi_end_intro = re.compile(r'ΕΝ ΑΘΗΝΑΙΣ|(1[8-9][0-9][0-9])$|ΕΙΣΑΓΩΓΗ|ΠΡΟΛΟΓΟΣ|ΠΡΟΣΩΠΑ|ΤΑ ΦΟΡΕΜΑΤΑ|ΕΚΔΟΤΙΚΟΣ ΟΙΚΟΣ ΓΕΩΡΓΙΟΥ ΦΕΞΗ|ΕΙΣΗΓΗΣΙΣ|ΒΙΒΛΙΟΝ|ΤΟΜΟΣ|ΕΚΔΟΤΗΣ|ΕΚΔΟΣΕΙΣ ΦΕΞΗ|ΚΕΦΑΛΑΙΟΝ|Κεφάλαιον|Α\'\.|ΚΕΦΑΛΑΙΟ I.|(\(1[8-9][0-9][0-9]\))$|Η ΥΠΟΘΕΣΙΣ ΤΟΥ ΔΡΑΜΑΤΟΣ|ΠΡΟΛΕΓΟΜΕΝΑ|ΠΡΑΞΙΣ|ΒΙΒΛΙΟ ΠΡΩΤΟ.')
11+
fexi_end_intro = re.compile(r'ΕΝ ΑΘΗΝΑΙΣ|(1[8-9][0-9][0-9].?)$|ΕΙΣΑΓΩΓΗ|ΠΡΟΛΟΓΟΣ|ΠΡΟΣΩΠΑ|ΤΑ ΦΟΡΕΜΑΤΑ|ΕΚΔΟΤΙΚΟΣ ΟΙΚΟΣ ΓΕΩΡΓΙΟΥ ΦΕΞΗ|ΕΙΣΗΓΗΣΙΣ|ΒΙΒΛΙΟΝ|ΤΟΜΟΣ|ΕΚΔΟΤΗΣ|ΕΚΔΟΣΕΙΣ ΦΕΞΗ|ΚΕΦΑΛΑΙΟΝ|Κεφάλαιον|Α\'\.|ΚΕΦΑΛΑΙΟ I.|(\(1[8-9][0-9][0-9]\))$|Η ΥΠΟΘΕΣΙΣ ΤΟΥ ΔΡΑΜΑΤΟΣ|ΠΡΟΛΕΓΟΜΕΝΑ|ΠΡΑΞΙΣ|ΒΙΒΛΙΟ ΠΡΩΤΟ.|Τι είναι βουδδισμός;|PREFACE|ΑΡΙΣΤΟΤΕΛΗΣ ΒΑΛΑΩΡΙΤΗΣ|ΓΙΑ ΤΟΥΤΟ ΤΟ ΒΙΒΛΙΟ|ΠΑΡΑΜΥΘΙ ΧΩΡΙΣ ΟΝΟΜΑ')
1212
content_pattern = re.compile(r'ΠΕΡΙΕΧΟΜΕΝΑ|ΕΜΠΕΡΙΕΧΟΜΕΝΑ|ΠΙΝΑΚΑΣ ΠΕΡΙΕΧΟΜΕΝΩΝ|Π Ι Ν Α Κ Α Σ Π Ε Ρ Ι Ε Χ Ο Μ Ε Ν Ω Ν|Π Ρ Ο Σ Ω Π Α|ΠΡΟΣΩΠΑ|Π Ι Ν Α Κ Α Σ Π Ε Ρ Ι Ε Χ Ο Μ Ε Ν Ω Ν|ΤΑ ΤΗΣ ΤΡΑΓΩΔΙΑΣ ΠΡΟΣΩΠΑ|ΠΕΡΙΕΧΟΜΕΝA|ΠΑΡΑΡΤΗΜΑ|ΠΙΝΑΚΑΣ|( )*ΟΙ ΠΑΡΑΔΑΡΜΕΝΟΙ|Τ Α Π Ρ Ο Σ Ω Π Α Τ Ο Υ Δ Ρ Α Μ Α Τ Ο Σ|ΠΡΟΣΩΠΑ ΤΟΥ ΔΡΑΜΑΤΟΣ|Τα πρόσωπα της τραγωδίας|ΤΑ ΠΡΟΣΩΠΑ ΤΟΥ ΔΡΑΜΑΤΟΣ')
1313
re_clean_end_pattern = re.compile(r'ΕΚΛΕΚΤΑ ΕΡΓΑ|\*\*\*|\* \* \*|ΠΙΝΑΚΑΣ|ΤΕΥΧΗ ΕΚΔΟΘΕΝΤΑ|Σ Η Μ Ε I Ω Σ Ε Ι Σ|ΠΡΟΠΟΜΠΟΙ|Δ Ι Ο Ρ Θ Ω Σ Ε Ι Σ|_Πίναξ|ΤΕΛΟΣ ΤΟΥ ΠΡΩΤΟΥ ΤΟΜΟΥ|ΠΙΝΑΞ|ΝΤΟΠΙΕΣ ΖΩΓΡΑΦΙΕΣ|ΤΕΛΟΣ|.?1[\)\}\]]|ΠΕΡΙΕΧΟΜΕΝΑ')
14+
out_pattern = re.compile(r'\[Out')
1415

1516
no_greek_pattern = re.compile(r'([Α-Ω]+)|([α-ω]+)', re.UNICODE)
1617
#end_note_pattern = re.compile(r'(1)|1)|1}|1}|[1]|1]')
@@ -123,12 +124,18 @@ def remove_latin_text(text) :
123124
newtext = newtext + line + '\n'
124125
return newtext
125126

126-
def re_remove(text) :
127+
def re_remove(text,num_of_file) :
127128
newtext = ''
128129
lines = text.splitlines()
129130
non_capital_pattern = re.compile(r'([α-ω][α-ω])+')
130131
re_clean_end_found_flag = False
132+
flag_prob_fail = False
131133
for i,line in enumerate(lines) :
134+
if re.match(out_pattern,line) :
135+
#if i > len(lines)/4 and i < len(lines)/3 and flag_prob_fail == False :
136+
#print('Probable Failure at ',num_of_file)
137+
#flag_prob_fail = True
138+
continue
132139
if i > 105 and i < len(lines) - 300 :
133140
newtext = newtext+line+'\n'
134141
continue
@@ -139,15 +146,40 @@ def re_remove(text) :
139146
newtext = newtext+line+'\n'
140147
continue
141148
if re_clean_end_found_flag == True :
142-
newtext = newtext+'[Out:End of text, Second clean]'+line+'\n'
149+
#newtext = newtext+'[Out:End of text, Second clean]'+line+'\n'
143150
continue
144151
if i < 105 :
145152
if not re.search(non_capital_pattern,line) :
146-
newtext = newtext+'[Out:Pures Capitals/No greek characters in first 105 lines]'+line+'\n'
153+
#newtext = newtext+'[Out:Pures Capitals/No greek characters in first 105 lines]'+line+'\n'
147154
continue
148155
newtext = newtext+line+'\n'
149156
return newtext
150157

158+
def remove_extras(text) :
159+
intro_white_space_flag = True
160+
beginning_pattern = re.compile(r'[Α-Ω]')
161+
lines = text.splitlines()
162+
newtext = ''
163+
for line in lines :
164+
if re.match(beginning_pattern,line) :
165+
intro_white_space_flag = False
166+
if intro_white_space_flag :
167+
newtext = newtext + '[Out:Greek Text not begun]'+ line + '\n'
168+
continue
169+
newtext = newtext + line + '\n'
170+
return newtext
171+
172+
def remove_publisher_note(text) :
173+
ekdotis_pattern = re.compile(r'.+Ο ΕΚΔΟΤΗΣ',re.DOTALL)
174+
if re.match(ekdotis_pattern, text) :
175+
re.sub(ekdotis_pattern, '[Out'+re.match(ekdotis_pattern, text).group(0)+']',text)
176+
return text
177+
178+
def precision_cleaning(text) :
179+
text = remove_extras(text)
180+
text = remove_publisher_note(text)
181+
return text
182+
151183
def clean(pathout,pathin) :
152184
os.makedirs(pathout, exist_ok=True)
153185
for i,file in enumerate(os.listdir(pathin)) :
@@ -160,7 +192,8 @@ def clean(pathout,pathin) :
160192
print(f"Error reading {file}: {e}")
161193
continue
162194
text = remove_latin_text(text)
163-
text = re_remove(text)
195+
text = re_remove(text,file)
196+
text = precision_cleaning(text)
164197
output_file_path = os.path.join(pathout, file)
165198
try:
166199
with open(output_file_path, 'w', encoding='utf-8') as output_file:

0 commit comments

Comments
 (0)