Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Patientenbriefe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nina Brolich
Patientenbriefe
Commits
26c751cc
Commit
26c751cc
authored
2 years ago
by
Nina Brolich
Browse files
Options
Downloads
Patches
Plain Diff
added script to extract plain text from tei
parent
4affddd1
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
plain_text.py
+29
-17
29 additions, 17 deletions
plain_text.py
with
29 additions
and
17 deletions
plain_text.py
+
29
−
17
View file @
26c751cc
...
...
@@ -8,6 +8,7 @@ dir = os.path.join(cwd, "teis")
outputdir
=
os
.
path
.
join
(
cwd
,
"
teis_converted
"
)
directorylist
=
os
.
listdir
(
dir
)
def
openXML
(
filepath
):
with
open
(
filepath
,
mode
=
'
r
'
,
encoding
=
'
utf-8
'
)
as
file
:
content
=
file
.
read
()
...
...
@@ -15,6 +16,7 @@ def openXML(filepath):
doc
=
BeautifulSoup
(
content
,
'
xml
'
)
return
doc
def
writeXML
(
outputpath
,
outputdir
,
doc
):
if
not
os
.
path
.
isdir
(
outputdir
):
os
.
makedirs
(
outputdir
)
...
...
@@ -22,54 +24,64 @@ def writeXML(outputpath, outputdir, doc):
file
.
write
(
str
(
doc
))
file
.
close
()
def
toPlainText
(
doc
):
#hand arzt
#
hand arzt
for
hand
in
doc
.
findAll
(
"
add
"
,
hand
=
"
arzt
"
):
addbrackets
(
hand
,
"
arzt
"
)
#unclear
#
unclear
for
unclear
in
doc
.
findAll
(
"
unclear
"
):
addbrackets
(
unclear
,
"
?
"
)
#del
#
del
for
del_elem
in
doc
.
findAll
(
"
del
"
):
if
"
hand
"
in
del_elem
.
attrs
:
addbrackets
(
del_elem
,
'
-hand2
'
)
else
:
addbrackets
(
del_elem
,
'
-
'
)
#alle leerzeichen in <w> tags loeschen
#
alle leerzeichen in <w> tags loeschen
for
w
in
doc
.
findAll
(
"
w
"
):
w_text
=
w
.
text
w
.
clear
()
w
.
append
(
re
.
sub
(
'
\s+
'
,
''
,
w_text
))
#leerzeichen zwischen zwei w-tags einfuegen
#
leerzeichen zwischen zwei w-tags einfuegen
for
w
in
doc
.
findAll
(
"
w
"
):
#sib = w.next_sibling
#
sib = w.next_sibling
if
str
(
w
.
next_sibling
).
startswith
(
'
<w
'
):
w
.
append
(
'
'
)
doctext
=
doc
.
body
.
text
#sonderzeichen ersetzen 'm̄' -> mm , 'n̄' -> nn,'−' -> -,'&' -> &,
# leerzeichen vor linebreak sicherstellen
doctext
=
str
(
doc
)
# doctext = re.sub('[^\s]<lb', ' <lb', doctext)
doctext
=
re
.
sub
(
'
<lb
'
,
'
<lb
'
,
doctext
)
# leerzeichen nach linebreak loeschen
# doctext = re.sub('<lb break=["yes"|"no"]>\s+', '<lb/>', doctext)
doctext
=
BeautifulSoup
(
doctext
,
"
xml
"
).
body
.
text
# leerzeichen vor und hinter eckigen klammern, z.b. Albert [arzt] Abschrift
doctext
=
re
.
sub
(
'
(\s+)(\[)(arzt|\?|\-)(\])(\s+)
'
,
'
\\
2
\\
3
\\
4
\\
5
'
,
doctext
)
# sonderzeichen ersetzen 'm̄' -> mm , 'n̄' -> nn,'−' -> -,'&' -> &,
doctext
=
re
.
sub
(
'
m
\u0304
'
,
'
mm
'
,
doctext
)
doctext
=
re
.
sub
(
'
n
\u0304
'
,
'
nn
'
,
doctext
)
#doctext = re.sub('\u8722', '-', doctext)
#mehrere leerzeichen zu einem leerzeichen
#
mehrere leerzeichen zu einem leerzeichen
doctext
=
re
.
sub
(
'
\s+
'
,
'
'
,
doctext
)
#leerzeichen vor linebreak sicherstellen
doctext
=
re
.
sub
(
'
[^\s]<lb
'
,
'
<lb
'
,
doctext
)
return
doctext
.
strip
()
def
addbrackets
(
tag
,
symbol
):
rep
=
"
[
"
+
symbol
+
"
]
"
#tagtext = tag.text
#tag.clear()
#tag.insert(0, rep + tagtext + rep)
#
tagtext = tag.text
#
tag.clear()
#
tag.insert(0, rep + tagtext + rep)
tag
.
insert
(
0
,
rep
)
tag
.
append
(
rep
)
for
directory
in
tqdm
(
directorylist
):
directorypath
=
os
.
path
.
join
(
dir
,
directory
)
filelist
=
os
.
listdir
(
directorypath
)
...
...
@@ -80,4 +92,4 @@ for directory in tqdm(directorylist):
outputpath
=
os
.
path
.
join
(
outputdir
,
directory
,
file
)
doc
=
openXML
(
filepath
)
res
=
toPlainText
(
doc
)
writeXML
(
outputpath
,
os
.
path
.
join
(
outputdir
,
directory
),
res
)
\ No newline at end of file
writeXML
(
outputpath
,
os
.
path
.
join
(
outputdir
,
directory
),
res
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment