Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Allzweckmesser
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
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Messerschleifer
Allzweckmesser
Commits
1be725b2
Commit
1be725b2
authored
6 years ago
by
Simon Will
Browse files
Options
Downloads
Patches
Plain Diff
Add module for reading Hypotactic Corpus
parent
5ba4bba0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
allzweckmesser/corpus.py
+104
-0
104 additions, 0 deletions
allzweckmesser/corpus.py
with
104 additions
and
0 deletions
allzweckmesser/corpus.py
0 → 100644
+
104
−
0
View file @
1be725b2
# -*- coding: utf-8 -*-
import
os.path
from
bs4
import
BeautifulSoup
from
.model
import
Reading
,
Syllable
,
Token
class
HypotacticLine
:
def
__init__
(
self
,
element
):
self
.
element
=
element
tokens
=
[]
span_begin
=
0
idx
=
0
for
token_tag
in
element
.
children
:
syllables
=
[]
token_text
=
token_tag
.
text
token
=
Token
(
token
=
token_text
,
span
=
(
span_begin
,
span_begin
+
len
(
token_text
))
)
for
syllable_tag
in
token_tag
.
children
:
syllable_text
=
syllable_tag
.
text
if
'
long
'
in
syllable_tag
.
attrs
[
'
class
'
]:
syllable_length
=
2
elif
'
short
'
in
syllable_tag
.
attrs
[
'
class
'
]:
syllable_length
=
1
elif
'
elided
'
in
syllable_tag
.
attrs
[
'
class
'
]:
syllable_length
=
0
else
:
raise
ValueError
(
'
Could not determine syllable length of syllable {!r}
'
.
format
(
syllable_tag
)
)
syllable
=
Syllable
(
idx
=
idx
,
syllable
=
syllable_text
,
span
=
(
span_begin
,
span_begin
+
len
(
syllable_text
)),
syllable_length
=
syllable_length
,
vowel_length
=
None
)
idx
+=
1
syllables
.
append
(
syllable
)
span_begin
+=
len
(
syllable_text
)
token
.
syllables
=
syllables
tokens
.
append
(
token
)
self
.
reading
=
Reading
(
tokens
=
tokens
)
class
HypotacticDocument
:
def
__init__
(
self
,
file_path
,
parser
=
'
lxml
'
):
with
open
(
file_path
)
as
f
:
self
.
root
=
BeautifulSoup
(
f
,
parser
)
self
.
title
=
self
.
root
.
title
def
get_poems
(
self
,
filters
=
()):
yield
from
(
p
for
p
in
self
.
root
.
find_all
(
name
=
'
div
'
,
class_
=
'
poem
'
)
if
all
(
fil
(
p
)
for
fil
in
filters
)
)
def
get_lines
(
self
,
line_filters
=
(),
poem_filters
=
()):
yield
from
(
line
for
poem
in
self
.
get_poems
(
poem_filters
)
for
line
in
poem
.
find_all
(
name
=
'
div
'
,
class_
=
'
line
'
)
if
all
(
fil
(
line
)
for
fil
in
line_filters
)
)
class
HypotacticCorpus
:
def
__init__
(
self
,
file_paths
,
parser
=
'
lxml
'
):
self
.
file_paths
=
file_paths
self
.
parser
=
parser
self
.
documents
=
[
HypotacticDocument
(
p
,
parser
=
parser
)
for
p
in
file_paths
]
@classmethod
def
from_directory
(
cls
,
directory
,
*
args
,
**
kwargs
):
file_paths
=
[
os
.
path
.
abspath
(
os
.
path
.
join
(
directory
,
basename
))
for
basename
in
os
.
listdir
(
directory
)]
return
cls
(
file_paths
,
*
args
,
**
kwargs
)
def
get_poems
(
self
,
filters
=
()):
yield
from
(
p
for
doc
in
self
.
documents
for
p
in
doc
.
get_poems
(
filters
)
)
def
get_lines
(
self
,
line_filters
=
(),
poem_filters
=
()):
yield
from
(
p
for
doc
in
self
.
documents
for
p
in
doc
.
get_lines
(
line_filters
,
poem_filters
)
)
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