Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F80566113
test_lineNumber.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Sep 1, 10:19
Size
5 KB
Mime Type
text/x-python
Expires
Tue, Sep 3, 10:19 (2 d)
Engine
blob
Format
Raw Data
Handle
20396163
Attached To
rNIETZSCHEPYTHON nietzsche-python
test_lineNumber.py
View Options
import
unittest
from
os
import
sep
,
path
from
os.path
import
isdir
,
dirname
import
lxml.etree
as
ET
import
sys
import
sys
sys
.
path
.
append
(
'svgscripts'
)
dir_changed
=
False
if
not
isdir
(
'datatypes'
):
sys
.
path
.
append
(
dirname
(
sys
.
path
[
0
]))
dir_changed
=
True
from
datatypes.lineNumber
import
LineNumber
,
get_bottoms
from
datatypes.transkriptionField
import
TranskriptionField
class
TestLineNumber
(
unittest
.
TestCase
):
def
setUp
(
self
):
DATADIR
=
dirname
(
__file__
)
+
sep
+
'test_data'
if
not
isdir
(
DATADIR
):
DATADIR
=
dirname
(
dirname
(
__file__
))
+
sep
+
'test_data'
self
.
test_target_file
=
DATADIR
+
sep
+
'test.xml'
self
.
test_source_file
=
DATADIR
+
sep
+
'test_ai.svg'
self
.
test_file
=
DATADIR
+
sep
+
'Mp_XIV_1_mytest_421.svg'
self
.
small_lines
=
f
'{DATADIR}{sep}pdfsvg{sep}csv{sep}45.svg'
self
.
id
=
24
def
test_init
(
self
):
lnr
=
LineNumber
(
id
=
self
.
id
)
self
.
assertEqual
(
lnr
.
id
,
self
.
id
)
svg_tree
=
ET
.
parse
(
self
.
test_source_file
)
tf
=
TranskriptionField
(
self
.
test_source_file
)
raw_text_node
=
svg_tree
.
getroot
()
.
xpath
(
'.//text[@transform="
%s
"]'
%
'matrix(1 0 0 1 180.8755 386.7129)'
)[
0
]
lnr
=
LineNumber
(
raw_text_node
=
raw_text_node
,
transkription_field
=
tf
)
self
.
assertEqual
(
lnr
.
id
,
self
.
id
)
self
.
assertEqual
(
lnr
.
bottom
,
311.8129
)
tree
=
ET
.
ElementTree
(
ET
.
Element
(
'svg'
))
node
=
ET
.
SubElement
(
tree
.
getroot
(),
'text'
,
attrib
=
{
'transform'
:
'matrix(1 0 0 1 180.8755 386.7129)'
})
node
.
text
=
str
(
self
.
id
)
lnr
=
LineNumber
(
raw_text_node
=
node
,
transkription_field
=
tf
)
self
.
assertEqual
(
lnr
.
id
,
self
.
id
)
self
.
assertEqual
(
lnr
.
bottom
,
311.8129
)
node
=
ET
.
SubElement
(
tree
.
getroot
(),
'text'
,
attrib
=
{
'transform'
:
'matrix(1 0 0 1 180.8755 386.7129)'
})
subnode
=
ET
.
SubElement
(
node
,
'tspan'
)
subnode
.
text
=
'1'
subnode
=
ET
.
SubElement
(
node
,
'tspan'
)
subnode
.
text
=
'0'
lnr
=
LineNumber
(
raw_text_node
=
node
,
transkription_field
=
tf
)
self
.
assertEqual
(
lnr
.
id
,
10
)
self
.
assertEqual
(
lnr
.
bottom
,
311.8129
)
def
test_init_from_xml
(
self
):
xml_tree
=
ET
.
parse
(
self
.
test_target_file
)
line_numbers
=
[
LineNumber
(
xml_text_node
=
node
)
for
node
in
xml_tree
.
getroot
()
.
xpath
(
'.//line-number'
)
]
self
.
assertEqual
(
len
(
line_numbers
),
49
)
self
.
assertEqual
(
line_numbers
[
0
]
.
id
,
1
)
self
.
assertEqual
(
line_numbers
[
48
]
.
id
,
49
)
def
test_attach_object_to_tree
(
self
):
empty_tree
=
ET
.
ElementTree
(
ET
.
Element
(
'page'
))
lnr
=
LineNumber
(
id
=
self
.
id
)
lnr
.
attach_object_to_tree
(
empty_tree
)
lnr_nodes
=
empty_tree
.
getroot
()
.
xpath
(
'//'
+
LineNumber
.
XML_TAG
+
'[@id="
%s
"]'
%
self
.
id
)
self
.
assertEqual
(
len
(
lnr_nodes
),
1
)
self
.
assertEqual
(
lnr_nodes
[
0
]
.
get
(
'id'
),
str
(
self
.
id
))
def
test_extract_line_numbers
(
self
):
svg_tree
=
ET
.
parse
(
self
.
test_file
)
tf
=
TranskriptionField
(
self
.
test_file
)
line_numbers
=
LineNumber
.
extract_line_numbers
(
svg_tree
,
tf
,
set_to_text_field_zero
=
False
)
self
.
assertEqual
(
line_numbers
[
0
]
.
id
,
2
)
self
.
assertEqual
(
len
(
line_numbers
),
24
)
self
.
assertEqual
(
line_numbers
[
0
]
.
top
,
45.163
+
tf
.
ymin
)
svg_tree
=
ET
.
parse
(
self
.
small_lines
)
tf
=
TranskriptionField
(
self
.
small_lines
)
line_numbers
=
LineNumber
.
extract_line_numbers
(
svg_tree
,
tf
,
set_to_text_field_zero
=
False
)
def
test_IS_A_LINE_NUMBER
(
self
):
tree
=
ET
.
ElementTree
(
ET
.
Element
(
'svg'
))
node
=
ET
.
SubElement
(
tree
.
getroot
(),
'text'
)
node
.
text
=
'2'
self
.
assertEqual
(
LineNumber
.
IS_A_LINE_NUMBER
(
node
),
True
)
node
=
ET
.
SubElement
(
tree
.
getroot
(),
'text'
)
subnode
=
ET
.
SubElement
(
node
,
'tspan'
)
subnode
.
text
=
'1'
subnode
=
ET
.
SubElement
(
node
,
'tspan'
)
subnode
.
text
=
'0'
self
.
assertEqual
(
LineNumber
.
IS_A_LINE_NUMBER
(
node
),
True
)
subnode
.
text
=
'x'
self
.
assertEqual
(
LineNumber
.
IS_A_LINE_NUMBER
(
node
),
False
)
def
test_get_semanticAndDataDict
(
self
):
xml_tree
=
ET
.
parse
(
self
.
test_target_file
)
line_number
=
[
LineNumber
(
xml_text_node
=
node
)
for
node
in
xml_tree
.
getroot
()
.
xpath
(
'.//line-number'
)
][
1
]
#self.assertEqual(line_number.get_data_dictionary()['body'].get('id'), 2)
#print(LineNumber.get_semantic_dictionary())
def
test_get_bottoms
(
self
):
svg_tree
=
ET
.
parse
(
self
.
test_file
)
mybottoms
=
get_bottoms
(
svg_tree
.
getroot
())
self
.
assertEqual
(
mybottoms
[
0
],
57.1914
)
self
.
assertEqual
(
len
(
mybottoms
),
106
)
self
.
assertEqual
(
mybottoms
[
-
1
],
1155.6899
)
mybottoms
=
get_bottoms
(
svg_tree
.
getroot
(),
from_position
=
100.0
,
to_position
=
800.0
)
self
.
assertEqual
(
mybottoms
[
0
],
100.5132
)
self
.
assertEqual
(
len
(
mybottoms
),
84
)
self
.
assertEqual
(
mybottoms
[
-
1
],
792.8218
)
tf
=
TranskriptionField
(
self
.
test_file
)
mybottoms
=
get_bottoms
(
svg_tree
.
getroot
(),
transkription_field
=
tf
)
self
.
assertEqual
(
mybottoms
[
0
],
91.7134
)
self
.
assertEqual
(
len
(
mybottoms
),
75
)
self
.
assertEqual
(
mybottoms
[
-
1
],
681.7134
)
if
__name__
==
"__main__"
:
unittest
.
main
()
Event Timeline
Log In to Comment