utils
autocorpus.utils
¤
Utility script containing various functions used throughout AC in different use-cases.
Functions¤
config_anchors(value)
¤
Clean the regex anchors of an AC config rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
AC config anchor value |
required |
Returns:
Type | Description |
---|---|
str
|
Cleaned regex with missing ^ and $ characters added. |
Source code in autocorpus/utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
config_attr_block(block)
¤
Parse the attributes block of an AC config file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block
|
dict
|
attributes block of an AC config file |
required |
Returns:
Type | Description |
---|---|
(dict) regex compiled & cleaned attributes block |
Source code in autocorpus/utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
config_attrs(attrs)
¤
Clean and compile attributes block of an AC config file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attrs
|
list of dicts or dict
|
attributes block of an AC config file |
required |
Returns:
Type | Description |
---|---|
list
|
cleaned and compiled attributes block of an AC config file |
Source code in autocorpus/utils.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
config_tags(tags)
¤
Parse the tags block of an AC config file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags
|
list or str
|
tags block of an AC config file |
required |
Returns:
Type | Description |
---|---|
list
|
cleaned and compiled tags block of an AC config file |
Source code in autocorpus/utils.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
get_data_element_node(config, soup)
¤
Retrieve the matches for the data element node config rules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict
|
Parsed config rules to be used |
required |
soup
|
BeautifulSoup
|
BeautifulSoup object containing the input text to search |
required |
Returns:
Type | Description |
---|---|
list
|
Matches for the data element node config rules |
Source code in autocorpus/utils.py
248 249 250 251 252 253 254 255 256 257 258 259 |
|
get_files(base_dir, pattern='(.*).html')
¤
Recursively retrieve all PMC.html files from the directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_dir
|
base directory |
required | |
pattern
|
file name filter REGEX pattern (default *.html) |
'(.*).html'
|
Return
file_list: a list of filepath
Source code in autocorpus/utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
handle_defined_by(config, soup)
¤
Retrieve matching nodes for the 'defined-by' config rules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict[str, Any]
|
config file section used to parse |
required |
soup
|
BeautifulSoup
|
soup section to parse |
required |
Returns:
Type | Description |
---|---|
list[Tag]
|
A list of objects, each object being a matching node. Object of the form: { node: bs4Object, data:{ key: [values] } } |
list[Tag]
|
node is a bs4 object of a single result derived from bs4.find_all() |
list[Tag]
|
data is an object where the results from the config "data" sections is housed. |
list[Tag]
|
The key is the name of the data section and the values are all matches found |
list[Tag]
|
within any of the main matches which match the current data section definition. |
list[Tag]
|
The values is the response you get from get_text() on any found nodes, not the |
list[Tag]
|
nodes themselves. |
Source code in autocorpus/utils.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
handle_not_tables(config, soup)
¤
Executes a search on non-table bs4 soup objects based on provided config rules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict[str, Any]
|
Parsed config rules to be used |
required |
soup
|
BeautifulSoup
|
BeautifulSoup object containing the input text to search |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Tag | list[str]]]
|
A list of matches for the provided config rules. Either as a Tag or a list of strings. |
Source code in autocorpus/utils.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
handle_tables(config, soup)
¤
Parse the provided BeautifulSoup object containing tables using the provided config rules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict
|
Parsed config rules to be used |
required |
soup
|
BeautifulSoup
|
BeautifulSoup object containing the input tables to construct |
required |
Returns:
Type | Description |
---|---|
list
|
List of matches for the provided config rules |
Source code in autocorpus/utils.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
navigate_contents(item)
¤
Extract nested text recursively from the provided NavigableString/Tag item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
NavigableString or Tag
|
Root element/tag to extract nested text from. |
required |
Returns:
Type | Description |
---|---|
(str) Text nested within the provided item. |
Source code in autocorpus/utils.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
parse_configs(definition)
¤
Parse a top-level block of an AC config file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
definition
|
dict
|
top-level block of an AC config file. |
required |
Returns:
Type | Description |
---|---|
dict
|
cleaned and compiled block of an AC config file. |
Source code in autocorpus/utils.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|