@@ -9,25 +9,24 @@ class BnF():
9
9
10
10
def __init__ (self , entry_list = [], apply_corrections = True ):
11
11
""" Initialize entire manuscript. If a list of IDs is given, narrow it down to them. """
12
- complete_manuscript = generate_complete_manuscript (complete = False , apply_corrections = apply_corrections )
12
+ complete_manuscript = generate_complete_manuscript (apply_corrections = apply_corrections )
13
+ # complete_manuscript2 = generate_complete_manuscript2(apply_corrections=apply_corrections)
13
14
if entry_list :
14
- self .entries = [ page for page in complete_manuscript if page .identity in entry_list ]
15
+ self .entries = { i : e for i , e in complete_manuscript . items () if e .identity in entry_list }
15
16
else :
16
17
self .entries = complete_manuscript
17
18
18
- def entry (self , identity : str ):
19
+ def entry (self , identity : str = '' ):
19
20
""" Return entry with the given identity. """
20
- for entry in self .entries :
21
- if entry .identity == identity :
22
- return entry
21
+ return self .entries .get (identity )
23
22
24
23
search_type = Optional [Union [str , bool ]]
25
24
def search (self , animal : search_type = None , body_part : search_type = None , currency : search_type = None ,
26
25
definition : search_type = None , environment : search_type = None , material : bool = None ,
27
26
medical : search_type = None , measurement : search_type = None , music : search_type = None ,
28
27
plant : search_type = None , place : search_type = None , personal_name : search_type = None ,
29
28
profession : search_type = None , sensory : search_type = None , tool : search_type = None ,
30
- time : search_type = None ) -> List [str ]:
29
+ time : search_type = None , weapon : search_type = None ) -> List [str ]:
31
30
"""
32
31
Search through each entry and return the identities that satisfy the criterion. Arguments are each of the element
33
32
types for a manuscript entry, which can be a string, bool, or None. If the argument is a string, then results must have
@@ -38,24 +37,28 @@ def search(self, animal: search_type = None, body_part: search_type = None, curr
38
37
args = {'animal' : animal , 'body_part' : body_part , 'currency' : currency , 'definition' : definition ,
39
38
'environment' : environment , 'material' : material , 'medical' : medical , 'measurement' : measurement ,
40
39
'music' : music , 'plant' : plant , 'place' : place , 'personal_name' : personal_name , 'profession' : profession ,
41
- 'sensory' : sensory , 'tool' : tool , 'time' : time }
40
+ 'sensory' : sensory , 'tool' : tool , 'time' : time , 'weapon' : weapon }
42
41
43
42
versions = ['tc' , 'tcn' , 'tl' ]
44
43
results = self .entries # initialize results
45
44
search_bools = [k for k , v in args .items () if isinstance (v , bool )] # select bool element categories
46
45
search_strings = [k for k , v in args .items () if isinstance (v , str )] # select string element categories
47
46
48
47
for s in search_bools : # filter by each bool
49
- results = [ r for r in results if any (r .get_prop (s , v ) for v in versions )]
48
+ results = { i : r for i , r in results . items () if any (r .get_prop (s , v ) for v in versions )}
50
49
for s in search_strings : # filter by each string
51
- results = [ r for r in results if any (args [s ] in r .get_prop (s , v ) for v in versions )]
50
+ results = { i : r for i , r in results if any (args [s ] in r .get_prop (s , v ) for v in versions )}
52
51
53
- return ([r .identity for r in results ]) # return identities
52
+ return ([i for i , r in results .items ()]) # return identities
53
+
54
+ def search_margins (self , version : str , term : str , placement : str ) -> List [str ]:
55
+ return [i for i , entry in self .entries .items ()
56
+ if any (margin [0 ] == placement and term in margin [1 ] for margin in entry .margins [version ])]
54
57
55
58
def tablefy (self ):
56
59
# id, head, no. words, category, amount of each tag, margins
57
60
# include figure margins?
58
- df = pd .DataFrame (columns = ['entry' ], data = self .entries )
61
+ df = pd .DataFrame (columns = ['entry' ], data = self .entries . values () )
59
62
df ['identity' ] = df .entry .apply (lambda x : x .identity )
60
63
df ['title' ] = df .entry .apply (lambda x : x .title ['tl' ])
61
64
df ['length' ] = df .entry .apply (lambda x : x .length ['tl' ])
@@ -64,3 +67,6 @@ def tablefy(self):
64
67
df ['del_tags' ] = df .entry .apply (lambda x : len (x .del_tags ))
65
68
df = df .drop (columns = ['entry' ])
66
69
return df
70
+
71
+ manuscript = BnF ()
72
+ # print(manuscript.search_margins('tl', 'gold', 'left-middle'))
0 commit comments