Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F62358761
test_word.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, May 12, 15:57
Size
7 KB
Mime Type
text/x-python
Expires
Tue, May 14, 15:57 (2 d)
Engine
blob
Format
Raw Data
Handle
17637501
Attached To
rNIETZSCHEPYTHON nietzsche-python
test_word.py
View Options
import
unittest
from
os
import
sep
,
path
import
lxml.etree
as
ET
import
sys
sys
.
path
.
append
(
'svgscripts'
)
from
datatypes.matrix
import
Matrix
from
datatypes.positional_word_part
import
PositionalWordPart
from
datatypes.transkriptionField
import
TranskriptionField
from
datatypes.transkription_position
import
TranskriptionPosition
from
datatypes.word
import
Word
from
datatypes.word_position
import
WordPosition
class
Page
:
def
__init__
(
self
):
self
.
svg_file
=
None
def
get_line_number
(
self
,
input
=
0
):
return
-
1
def
get_biggest_fontSize4styles
(
self
,
style_set
=
{}):
return
7
class
TestWord
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
word_part_objs
=
[{
'text'
:
'a'
},
{
'text'
:
'b'
},
{
'text'
:
'c'
}]
x
=
0
for
dict
in
self
.
word_part_objs
:
dict
[
'class'
]
=
'st22'
dict
[
'x'
]
=
x
dict
[
'y'
]
=
11
x
+=
1
mylist
=
{
'text'
:
'abc'
,
'id'
:
'0'
,
'line-number'
:
'2'
,
'deleted'
:
'true'
}
word_position
=
TranskriptionPosition
(
x
=
0
,
y
=
1
,
height
=
10
,
width
=
10
,
matrix
=
Matrix
(
'matrix(0.94 0.342 -0.342 0.94 0 0)'
))
self
.
transkription_positions
=
[
word_position
]
self
.
word_node
=
ET
.
Element
(
'word'
,
attrib
=
mylist
)
word_position
.
attach_object_to_tree
(
self
.
word_node
)
x
=
0
for
char
in
mylist
[
'text'
]:
ET
.
SubElement
(
self
.
word_node
,
'part'
,
attrib
=
{
'text'
:
char
,
'x'
:
str
(
x
),
'y'
:
'11'
,
'class'
:
'st22'
})
x
+=
1
def
test_Word_with_word_part_objs
(
self
):
word
=
Word
.
CREATE_WORD
(
word_part_objs
=
self
.
word_part_objs
,
height
=
10
,
endX
=
10
)
self
.
assertEqual
(
word
.
id
,
0
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
bottom
,
13
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
height
,
10
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
top
,
3
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
left
,
0
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
width
,
10
)
self
.
assertEqual
(
word
.
text
,
'abc'
)
def
test_Word_with_word_node
(
self
):
word
=
Word
.
CREATE_WORD
(
word_node
=
self
.
word_node
)
self
.
assertEqual
(
word
.
id
,
0
)
self
.
assertEqual
(
word
.
deleted
,
True
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
bottom
,
11
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
height
,
10
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
top
,
1
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
left
,
0
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
width
,
10
)
self
.
assertEqual
(
word
.
text
,
'abc'
)
self
.
assertEqual
(
word
.
line_number
,
2
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
transform
.
isRotationMatrix
(),
True
)
def
test_attach_word_to_tree
(
self
):
newWord
=
Word
.
CREATE_WORD
(
word_part_objs
=
self
.
word_part_objs
,
height
=
10
,
endX
=
10
)
empty_tree
=
ET
.
ElementTree
(
ET
.
Element
(
'page'
))
newWord
.
attach_word_to_tree
(
empty_tree
)
for
word_node
in
empty_tree
.
getroot
()
.
xpath
(
'//word'
):
word
=
Word
.
CREATE_WORD
(
word_node
=
word_node
)
self
.
assertEqual
(
word
.
id
,
0
)
self
.
assertEqual
(
word
.
deleted
,
False
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
bottom
,
13
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
height
,
10
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
top
,
3
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
left
,
0
)
self
.
assertEqual
(
word
.
transkription_positions
[
0
]
.
width
,
10
)
self
.
assertEqual
(
word
.
text
,
'abc'
)
def
test_split
(
self
):
page
=
Page
()
pwps
=
PositionalWordPart
.
CREATE_SIMPLE_POSITIONAL_WORD_PART_LIST
(
page
,
self
.
word_part_objs
)
transkription_positions
=
TranskriptionPosition
.
CREATE_TRANSKRIPTION_POSITION_LIST_FROM_PWPS
(
page
,
pwps
)
word
=
Word
(
text
=
''
.
join
([
pwp
.
text
for
pwp
in
pwps
]),
transkription_positions
=
transkription_positions
)
previousWord
,
currentWord
,
nextWord
=
word
.
split
(
page
,
'b'
)
self
.
assertEqual
(
previousWord
.
id
,
0
)
self
.
assertEqual
(
previousWord
.
text
,
'a'
)
self
.
assertEqual
(
currentWord
.
id
,
1
)
self
.
assertEqual
(
nextWord
.
id
,
2
)
word
=
Word
(
text
=
''
.
join
([
pwp
.
text
for
pwp
in
pwps
]),
transkription_positions
=
transkription_positions
)
previousWord
,
currentWord
,
nextWord
=
word
.
split
(
page
,
'bc'
)
self
.
assertEqual
(
previousWord
.
id
,
0
)
self
.
assertEqual
(
previousWord
.
text
,
'a'
)
self
.
assertEqual
(
currentWord
.
id
,
1
)
word
=
Word
(
text
=
''
.
join
([
pwp
.
text
for
pwp
in
pwps
]),
transkription_positions
=
transkription_positions
)
previousWord
,
currentWord
,
nextWord
=
word
.
split
(
page
,
'ab'
,
start_id
=
10
)
self
.
assertEqual
(
currentWord
.
id
,
10
)
self
.
assertEqual
(
currentWord
.
text
,
'ab'
)
self
.
assertEqual
(
currentWord
.
transkription_positions
[
0
]
.
width
,
2.1
)
self
.
assertEqual
(
nextWord
.
id
,
11
)
self
.
assertEqual
(
nextWord
.
transkription_positions
[
0
]
.
width
,
5.2
)
word_part_objs
=
[{
'text'
:
'x'
,
'class'
:
'st22'
,
'x'
:
0
,
'y'
:
0
},
\
{
'text'
:
'Insofern'
,
'class'
:
'st22'
,
'x'
:
1
,
'y'
:
0
},
\
{
'text'
:
'x'
,
'class'
:
'st22'
,
'x'
:
10
,
'y'
:
0
}]
pwps
=
PositionalWordPart
.
CREATE_SIMPLE_POSITIONAL_WORD_PART_LIST
(
page
,
word_part_objs
)
transkription_positions
=
TranskriptionPosition
.
CREATE_TRANSKRIPTION_POSITION_LIST_FROM_PWPS
(
page
,
pwps
)
word
=
Word
(
text
=
''
.
join
([
pwp
.
text
for
pwp
in
pwps
]),
transkription_positions
=
transkription_positions
)
with
self
.
assertWarns
(
Warning
):
previousWord
,
currentWord
,
nextWord
=
word
.
split
(
page
,
'Insofer'
)
word_part_objs
=
[{
'text'
:
'xInsofern'
,
'class'
:
'st22'
,
'x'
:
0
,
'y'
:
0
}]
pwps
=
PositionalWordPart
.
CREATE_SIMPLE_POSITIONAL_WORD_PART_LIST
(
page
,
word_part_objs
)
transkription_positions
=
TranskriptionPosition
.
CREATE_TRANSKRIPTION_POSITION_LIST_FROM_PWPS
(
page
,
pwps
)
word
=
Word
(
text
=
''
.
join
([
pwp
.
text
for
pwp
in
pwps
]),
transkription_positions
=
transkription_positions
)
with
self
.
assertWarns
(
Warning
):
previousWord
,
currentWord
,
nextWord
=
word
.
split
(
page
,
'Insofern'
)
def
test_join
(
self
):
word
=
Word
.
CREATE_WORD
(
word_part_objs
=
self
.
word_part_objs
,
height
=
10
,
endX
=
10
)
other_word
=
Word
.
CREATE_WORD
(
word_part_objs
=
[{
'text'
:
'.'
,
'class'
:
'st22'
,
'x'
:
3
,
'y'
:
11
}])
word
.
join
(
other_word
)
self
.
assertEqual
(
word
.
text
,
'abc.'
)
other_word
=
Word
.
CREATE_WORD
(
word_part_objs
=
[{
'text'
:
'.'
,
'class'
:
'st22'
,
'x'
:
3
,
'y'
:
11
}])
word
.
join
(
other_word
,
append_at_end_of_new_word
=
False
)
self
.
assertEqual
(
word
.
text
,
'.abc.'
)
"""
tree = ET.ElementTree(ET.Element('page'))
word.attach_word_to_tree(tree)
print(ET.dump(tree.getroot()))
"""
def
test_get_semanticAndDataDict
(
self
):
word
=
Word
.
CREATE_WORD
(
word_node
=
self
.
word_node
)
empty_tree
=
ET
.
ElementTree
(
ET
.
Element
(
'page'
))
word
.
attach_word_to_tree
(
empty_tree
)
dictionary
=
Word
.
get_semantic_dictionary
()
for
key
in
dictionary
[
'properties'
]
.
keys
():
xpath
=
dictionary
[
'properties'
]
.
get
(
key
)
.
get
(
'xpath'
)
\
if
type
(
dictionary
[
'properties'
]
.
get
(
key
))
is
dict
\
else
dictionary
[
'properties'
]
.
get
(
key
)[
2
]
results
=
empty_tree
.
xpath
(
xpath
)
self
.
assertEqual
(
len
(
results
),
1
)
#print('{}: {}'.format(key, results[0]))
#self.assertEqual(word.get_data_dictionary()['body'].get('text'), 'abc')
if
__name__
==
"__main__"
:
unittest
.
main
()
Event Timeline
Log In to Comment