taxonomic-entity-augmentation 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. taxonomic_entity_augmentation-1.0.2/LICENSE +182 -0
  2. taxonomic_entity_augmentation-1.0.2/NOTICE +8 -0
  3. taxonomic_entity_augmentation-1.0.2/PKG-INFO +30 -0
  4. taxonomic_entity_augmentation-1.0.2/README.md +15 -0
  5. taxonomic_entity_augmentation-1.0.2/pyproject.toml +43 -0
  6. taxonomic_entity_augmentation-1.0.2/setup.cfg +4 -0
  7. taxonomic_entity_augmentation-1.0.2/src/bio_tea/__init__.py +20 -0
  8. taxonomic_entity_augmentation-1.0.2/src/bio_tea/augment/__init__.py +21 -0
  9. taxonomic_entity_augmentation-1.0.2/src/bio_tea/augment/labels.py +48 -0
  10. taxonomic_entity_augmentation-1.0.2/src/bio_tea/augment/pipeline.py +97 -0
  11. taxonomic_entity_augmentation-1.0.2/src/bio_tea/augment/windowing.py +104 -0
  12. taxonomic_entity_augmentation-1.0.2/src/bio_tea/augment/words.py +30 -0
  13. taxonomic_entity_augmentation-1.0.2/src/bio_tea/core.py +177 -0
  14. taxonomic_entity_augmentation-1.0.2/src/bio_tea/data/__init__.py +17 -0
  15. taxonomic_entity_augmentation-1.0.2/src/bio_tea/data/attribution.txt +10 -0
  16. taxonomic_entity_augmentation-1.0.2/src/bio_tea/data/species.txt +20510 -0
  17. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/__init__.py +18 -0
  18. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/claims.py +760 -0
  19. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/classify.py +126 -0
  20. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/cli.py +809 -0
  21. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/curated_root.py +135 -0
  22. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/curation.py +97 -0
  23. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/example_set_tools.py +430 -0
  24. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/exp3.py +71 -0
  25. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/indexing.py +134 -0
  26. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/io.py +100 -0
  27. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/lexicons.py +224 -0
  28. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/licenses.py +378 -0
  29. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/materialize.py +489 -0
  30. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/model_profiles.py +77 -0
  31. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/output_adapters.py +61 -0
  32. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/pool.py +56 -0
  33. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/reporting.py +369 -0
  34. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/results_layout.py +99 -0
  35. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/species.py +188 -0
  36. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/splitting.py +501 -0
  37. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/token_budget.py +261 -0
  38. taxonomic_entity_augmentation-1.0.2/src/bio_tea/datasets/tokenizer_loader.py +225 -0
  39. taxonomic_entity_augmentation-1.0.2/src/bio_tea/resources/__init__.py +14 -0
  40. taxonomic_entity_augmentation-1.0.2/src/bio_tea/resources/species.py +57 -0
  41. taxonomic_entity_augmentation-1.0.2/src/bio_tea/tea.py +18 -0
  42. taxonomic_entity_augmentation-1.0.2/src/bio_tea/transforms/__init__.py +14 -0
  43. taxonomic_entity_augmentation-1.0.2/src/bio_tea/transforms/scramble.py +103 -0
  44. taxonomic_entity_augmentation-1.0.2/src/bio_tea/transforms/species_switch.py +81 -0
  45. taxonomic_entity_augmentation-1.0.2/src/taxonomic_entity_augmentation.egg-info/PKG-INFO +30 -0
  46. taxonomic_entity_augmentation-1.0.2/src/taxonomic_entity_augmentation.egg-info/SOURCES.txt +48 -0
  47. taxonomic_entity_augmentation-1.0.2/src/taxonomic_entity_augmentation.egg-info/dependency_links.txt +1 -0
  48. taxonomic_entity_augmentation-1.0.2/src/taxonomic_entity_augmentation.egg-info/entry_points.txt +7 -0
  49. taxonomic_entity_augmentation-1.0.2/src/taxonomic_entity_augmentation.egg-info/requires.txt +1 -0
  50. taxonomic_entity_augmentation-1.0.2/src/taxonomic_entity_augmentation.egg-info/top_level.txt +1 -0
@@ -0,0 +1,182 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction, and
10
+ distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright
13
+ owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other entities
16
+ that control, are controlled by, or are under common control with that entity.
17
+ For the purposes of this definition, "control" means (i) the power, direct or
18
+ indirect, to cause the direction or management of such entity, whether by
19
+ contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20
+ outstanding shares, or (iii) beneficial ownership of such entity.
21
+
22
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
23
+ permissions granted by this License.
24
+
25
+ "Source" form shall mean the preferred form for making modifications, including
26
+ but not limited to software source code, documentation source, and configuration
27
+ files.
28
+
29
+ "Object" form shall mean any form resulting from mechanical transformation or
30
+ translation of a Source form, including but not limited to compiled object code,
31
+ generated documentation, and conversions to other media types.
32
+
33
+ "Work" shall mean the work of authorship, whether in Source or Object form,
34
+ made available under the License, as indicated by a copyright notice that is
35
+ included in or attached to the work (an example is provided in the Appendix
36
+ below).
37
+
38
+ "Derivative Works" shall mean any work, whether in Source or Object form, that
39
+ is based on (or derived from) the Work and for which the editorial revisions,
40
+ annotations, elaborations, or other modifications represent, as a whole, an
41
+ original work of authorship. For the purposes of this License, Derivative Works
42
+ shall not include works that remain separable from, or merely link (or bind by
43
+ name) to the interfaces of, the Work and Derivative Works thereof.
44
+
45
+ "Contribution" shall mean any work of authorship, including the original version
46
+ of the Work and any modifications or additions to that Work or Derivative Works
47
+ thereof, that is intentionally submitted to Licensor for inclusion in the Work
48
+ by the copyright owner or by an individual or Legal Entity authorized to submit
49
+ on behalf of the copyright owner. For the purposes of this definition,
50
+ "submitted" means any form of electronic, verbal, or written communication sent
51
+ to the Licensor or its representatives, including but not limited to
52
+ communication on electronic mailing lists, source code control systems, and
53
+ issue tracking systems that are managed by, or on behalf of, the Licensor for
54
+ the purpose of discussing and improving the Work, but excluding communication
55
+ that is conspicuously marked or otherwise designated in writing by the copyright
56
+ owner as "Not a Contribution." "Contributor" shall mean Licensor and any
57
+ individual or Legal Entity on behalf of whom a Contribution has been received by
58
+ Licensor and subsequently incorporated within the Work.
59
+
60
+ 2. Grant of Copyright License. Subject to the terms and conditions of this
61
+ License, each Contributor hereby grants to You a perpetual, worldwide,
62
+ non-exclusive, no-charge, royalty-free, irrevocable copyright license to
63
+ reproduce, prepare Derivative Works of, publicly display, publicly perform,
64
+ sublicense, and distribute the Work and such Derivative Works in Source or
65
+ Object form.
66
+
67
+ 3. Grant of Patent License. Subject to the terms and conditions of this License,
68
+ each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
69
+ no-charge, royalty-free, irrevocable (except as stated in this section) patent
70
+ license to make, have made, use, offer to sell, sell, import, and otherwise
71
+ transfer the Work, where such license applies only to those patent claims
72
+ licensable by such Contributor that are necessarily infringed by their
73
+ Contribution(s) alone or by combination of their Contribution(s) with the Work
74
+ to which such Contribution(s) was submitted. If You institute patent litigation
75
+ against any entity (including a cross-claim or counterclaim in a lawsuit)
76
+ alleging that the Work or a Contribution incorporated within the Work
77
+ constitutes direct or contributory patent infringement, then any patent licenses
78
+ granted to You under this License for that Work shall terminate as of the date
79
+ such litigation is filed.
80
+
81
+ 4. Redistribution. You may reproduce and distribute copies of the Work or
82
+ Derivative Works thereof in any medium, with or without modifications, and in
83
+ Source or Object form, provided that You meet the following conditions:
84
+
85
+ (a) You must give any other recipients of the Work or Derivative Works a copy of
86
+ this License; and
87
+
88
+ (b) You must cause any modified files to carry prominent notices stating that
89
+ You changed the files; and
90
+
91
+ (c) You must retain, in the Source form of any Derivative Works that You
92
+ distribute, all copyright, patent, trademark, and attribution notices from
93
+ the Source form of the Work, excluding those notices that do not pertain to
94
+ any part of the Derivative Works; and
95
+
96
+ (d) If the Work includes a "NOTICE" text file as part of its distribution, then
97
+ any Derivative Works that You distribute must include a readable copy of the
98
+ attribution notices contained within such NOTICE file, excluding those
99
+ notices that do not pertain to any part of the Derivative Works, in at least
100
+ one of the following places: within a NOTICE text file distributed as part
101
+ of the Derivative Works; within the Source form or documentation, if
102
+ provided along with the Derivative Works; or, within a display generated by
103
+ the Derivative Works, if and wherever such third-party notices normally
104
+ appear. The contents of the NOTICE file are for informational purposes only
105
+ and do not modify the License. You may add Your own attribution notices
106
+ within Derivative Works that You distribute, alongside or as an addendum to
107
+ the NOTICE text from the Work, provided that such additional attribution
108
+ notices cannot be construed as modifying the License.
109
+
110
+ You may add Your own copyright statement to Your modifications and may provide
111
+ additional or different license terms and conditions for use, reproduction, or
112
+ distribution of Your modifications, or for any such Derivative Works as a whole,
113
+ provided Your use, reproduction, and distribution of the Work otherwise complies
114
+ with the conditions stated in this License.
115
+
116
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any
117
+ Contribution intentionally submitted for inclusion in the Work by You to the
118
+ Licensor shall be under the terms and conditions of this License, without any
119
+ additional terms or conditions. Notwithstanding the above, nothing herein shall
120
+ supersede or modify the terms of any separate license agreement you may have
121
+ executed with Licensor regarding such Contributions.
122
+
123
+ 6. Trademarks. This License does not grant permission to use the trade names,
124
+ trademarks, service marks, or product names of the Licensor, except as required
125
+ for reasonable and customary use in describing the origin of the Work and
126
+ reproducing the content of the NOTICE file.
127
+
128
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
129
+ writing, Licensor provides the Work (and each Contributor provides its
130
+ Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
131
+ KIND, either express or implied, including, without limitation, any warranties or
132
+ conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
133
+ PARTICULAR PURPOSE. You are solely responsible for determining the
134
+ appropriateness of using or redistributing the Work and assume any risks
135
+ associated with Your exercise of permissions under this License.
136
+
137
+ 8. Limitation of Liability. In no event and under no legal theory, whether in
138
+ tort (including negligence), contract, or otherwise, unless required by
139
+ applicable law (such as deliberate and grossly negligent acts) or agreed to in
140
+ writing, shall any Contributor be liable to You for damages, including any
141
+ direct, indirect, special, incidental, or consequential damages of any character
142
+ arising as a result of this License or out of the use or inability to use the
143
+ Work (including but not limited to damages for loss of goodwill, work stoppage,
144
+ computer failure or malfunction, or any and all other commercial damages or
145
+ losses), even if such Contributor has been advised of the possibility of such
146
+ damages.
147
+
148
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or
149
+ Derivative Works thereof, You may choose to offer, and charge a fee for,
150
+ acceptance of support, warranty, indemnity, or other liability obligations
151
+ and/or rights consistent with this License. However, in accepting such
152
+ obligations, You may act only on Your own behalf and on Your sole
153
+ responsibility, not on behalf of any other Contributor, and only if You agree to
154
+ indemnify, defend, and hold each Contributor harmless for any liability incurred
155
+ by, or claims asserted against, such Contributor by reason of your accepting any
156
+ such warranty or additional liability.
157
+
158
+ END OF TERMS AND CONDITIONS
159
+
160
+ APPENDIX: How to apply the Apache License to your work.
161
+
162
+ To apply the Apache License to your work, attach the following boilerplate
163
+ notice, with the fields enclosed by brackets "[]" replaced with your own
164
+ identifying information. (Don't include the brackets!) The text should be
165
+ enclosed in the appropriate comment syntax for the file format. We also
166
+ recommend that a file or class name and description of purpose be included on
167
+ the same "printed page" as the copyright notice for easier identification within
168
+ third-party archives.
169
+
170
+ Copyright [yyyy] [name of copyright owner]
171
+
172
+ Licensed under the Apache License, Version 2.0 (the "License");
173
+ you may not use this file except in compliance with the License.
174
+ You may obtain a copy of the License at
175
+
176
+ http://www.apache.org/licenses/LICENSE-2.0
177
+
178
+ Unless required by applicable law or agreed to in writing, software
179
+ distributed under the License is distributed on an "AS IS" BASIS,
180
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
181
+ See the License for the specific language governing permissions and
182
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ bio-tea
2
+ Copyright 2026 tznurmin
3
+
4
+ This package includes a modified UniProt organism list derived from speclist.txt.
5
+ The vendored organism list is distributed under the Creative Commons Attribution
6
+ 4.0 License (CC BY 4.0). See src/bio_tea/data/attribution.txt.
7
+
8
+ Downloaded TEA_curated_data material is external and separately licensed.
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.4
2
+ Name: taxonomic-entity-augmentation
3
+ Version: 1.0.2
4
+ Summary: Taxonomic Entity Augmentation
5
+ Author: tznurmin
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Repository, https://github.com/tznurmin/bio-tea
8
+ Project-URL: Issues, https://github.com/tznurmin/bio-tea/issues
9
+ Requires-Python: >=3.9
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ License-File: NOTICE
13
+ Requires-Dist: PyYAML>=6.0
14
+ Dynamic: license-file
15
+
16
+ # Taxonomic Entity Augmentation (TEA)
17
+
18
+ Taxonomic Entity Augmentation for biological texts.
19
+
20
+ Install:
21
+
22
+ ```bash
23
+ python -m pip install taxonomic-entity-augmentation
24
+ ```
25
+
26
+ Use:
27
+
28
+ ```python
29
+ from bio_tea import TEA
30
+ ```
@@ -0,0 +1,15 @@
1
+ # Taxonomic Entity Augmentation (TEA)
2
+
3
+ Taxonomic Entity Augmentation for biological texts.
4
+
5
+ Install:
6
+
7
+ ```bash
8
+ python -m pip install taxonomic-entity-augmentation
9
+ ```
10
+
11
+ Use:
12
+
13
+ ```python
14
+ from bio_tea import TEA
15
+ ```
@@ -0,0 +1,43 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77.0.3"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "taxonomic-entity-augmentation"
7
+ version = "1.0.2"
8
+ authors = [{ name = "tznurmin" }]
9
+ description = "Taxonomic Entity Augmentation"
10
+ readme = "README.md"
11
+ requires-python = ">=3.9"
12
+ license = "Apache-2.0"
13
+ license-files = ["LICENSE", "NOTICE"]
14
+ dependencies = [
15
+ "PyYAML>=6.0",
16
+ ]
17
+
18
+ [project.urls]
19
+ Repository = "https://github.com/tznurmin/bio-tea"
20
+ Issues = "https://github.com/tznurmin/bio-tea/issues"
21
+
22
+ [project.scripts]
23
+ bio-tea-inspect = "bio_tea.datasets.cli:inspect_main"
24
+ bio-tea-sample = "bio_tea.datasets.cli:sample_main"
25
+ bio-tea-validate = "bio_tea.datasets.cli:validate_main"
26
+ bio-tea-stats = "bio_tea.datasets.cli:stats_main"
27
+ bio-tea-qa = "bio_tea.datasets.cli:qa_main"
28
+ bio-tea-manifest-compare = "bio_tea.datasets.cli:manifest_compare_main"
29
+
30
+ [tool.setuptools]
31
+ include-package-data = true
32
+ package-dir = { "" = "src" }
33
+ packages = [
34
+ "bio_tea",
35
+ "bio_tea.data",
36
+ "bio_tea.augment",
37
+ "bio_tea.resources",
38
+ "bio_tea.transforms",
39
+ "bio_tea.datasets",
40
+ ]
41
+
42
+ [tool.setuptools.package-data]
43
+ "bio_tea.data" = ["species.txt", "attribution.txt"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,20 @@
1
+ # Copyright 2026 tznurmin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Top-level TEA package API."""
15
+
16
+ from .core import TEA
17
+
18
+ __version__ = "1.0.2"
19
+
20
+ __all__ = ["TEA", "__version__"]
@@ -0,0 +1,21 @@
1
+ # Copyright 2026 tznurmin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Augmentation helpers.
15
+
16
+ This package contains lower-level routines used by TEA's augmentation pipeline.
17
+ """
18
+
19
+ from .windowing import is_stop, extract_sentence, maximise
20
+
21
+ __all__ = ["is_stop", "extract_sentence", "maximise"]
@@ -0,0 +1,48 @@
1
+ # Copyright 2026 tznurmin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Label construction for TEA augmentation.
15
+
16
+ Input curation data uses:
17
+ - curation_data: dict[str, list[str]]
18
+ - location strings in "start+len" token-index format
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ from collections.abc import Mapping
24
+
25
+
26
+ def build_labels(tokens: list[str], curation_data: Mapping[str, list[str]], l2l: dict[str, str] | None = None) -> list[str]:
27
+ labels = ['O'] * len(tokens)
28
+
29
+ for t, locations in curation_data.items():
30
+ t0 = t.split('/')[0]
31
+ label = t0[0:4].upper()
32
+ if l2l is not None:
33
+ label = l2l.get(label, 'O')
34
+
35
+ if label == 'O':
36
+ continue
37
+
38
+ for loc in locations:
39
+ t_sta, t_len = loc.split('+')
40
+ t_sta_i = int(t_sta)
41
+ t_len_i = int(t_len)
42
+ for idx in range(t_sta_i, t_sta_i + t_len_i):
43
+ if idx == t_sta_i:
44
+ labels[idx] = f"B-{label}"
45
+ else:
46
+ labels[idx] = f"I-{label}"
47
+
48
+ return labels
@@ -0,0 +1,97 @@
1
+ # Copyright 2026 tznurmin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Token-level entity augmentation pipeline."""
15
+
16
+ from __future__ import annotations
17
+
18
+ from collections.abc import Callable, Mapping
19
+
20
+ from .labels import build_labels
21
+ from .words import extract_span_phrases
22
+
23
+
24
+ def augment_tokens(
25
+ tokens: list[str],
26
+ curation_data: Mapping[str, list[str]],
27
+ *,
28
+ maximise: Callable[[int, list[str]], tuple[int, int]],
29
+ switch: Callable[[str], str],
30
+ scramble: Callable[..., str],
31
+ num_tokens: Callable[[str], int],
32
+ max_final_len: int,
33
+ scramble_tags: list[str] | None = None,
34
+ l2l: dict[str, str] | None = None,
35
+ ) -> dict:
36
+ scramble_tags = scramble_tags or []
37
+
38
+ # Scramble wordlist must be deterministic; using a set directly makes output
39
+ # depend on hash iteration order.
40
+ words: set[str] = set()
41
+ results: dict = {
42
+ 'original': {},
43
+ 'scrambled': {},
44
+ 'switched': {},
45
+ 'all': {},
46
+ }
47
+
48
+ # Labels are constructed once over the full token sequence.
49
+ labels = build_labels(tokens, curation_data, l2l=l2l)
50
+
51
+ # Determine which phrases are eligible for scrambling.
52
+ for t, locations in curation_data.items():
53
+ t0 = t.split('/')[0]
54
+ if t0 in scramble_tags:
55
+ words.update(extract_span_phrases(tokens, {t0: locations}))
56
+
57
+ for loc in locations:
58
+ s0 = int(loc.split('+')[0])
59
+ s, e = maximise(s0, tokens)
60
+ results['original'][f"{s},{e}"] = ' '.join(tokens[s:e + 1])
61
+
62
+ results['labels'] = labels
63
+
64
+ # Deterministic ordering for scramble: longer phrases first avoids partial
65
+ # replacements when phrases overlap.
66
+ words_list = sorted(words, key=lambda s: (-len(s), s))
67
+
68
+ for pos, window_text in results['original'].items():
69
+ transformations = {
70
+ 'switched': [switch],
71
+ 'scrambled': [scramble],
72
+ 'all': [switch, scramble],
73
+ }
74
+ extra_params = {scramble: [words_list]}
75
+
76
+ for out_key, tfs in transformations.items():
77
+ current = window_text
78
+
79
+ for idx, tf in enumerate(tfs):
80
+ base = current
81
+ tries = 5
82
+
83
+ while tries > 0:
84
+ params = extra_params.get(tf, [])
85
+ candidate = tf(base, *params)
86
+ if num_tokens(candidate) <= max_final_len:
87
+ current = candidate
88
+ break
89
+ tries -= 1
90
+
91
+ if tries == 0:
92
+ break
93
+
94
+ if idx == len(tfs) - 1:
95
+ results[out_key][pos] = current
96
+
97
+ return results
@@ -0,0 +1,104 @@
1
+ # Copyright 2026 tznurmin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Sentence windowing for TEA.
15
+
16
+ This module operates on TEA's token stream and provides sentence-boundary and
17
+ window-expansion helpers used by the augmentation pipeline.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ from typing import Callable, Iterable, List, Sequence, Tuple
23
+
24
+
25
+ def is_stop(word: str, non_stops: Iterable[str]) -> bool:
26
+ """Return True when the token is treated as a sentence boundary."""
27
+
28
+ if not word or word[-1] != ".":
29
+ return False
30
+
31
+ # Special-case: treat '].' as a stop even though it's short.
32
+ if word != "]." and (len(word) < 3 or word.lower() in set(map(str.lower, non_stops))):
33
+ return False
34
+
35
+ return True
36
+
37
+
38
+ def extract_sentence(sp: int, tokens: Sequence[str], non_stops: Iterable[str]) -> Tuple[int, int]:
39
+ """Extract the sentence span (s, e) containing token index sp.
40
+
41
+ A "sentence" is a maximal contiguous run of tokens not interrupted by is_stop.
42
+ """
43
+
44
+ s = sp
45
+ e = sp
46
+
47
+ while e < len(tokens) - 1 and not is_stop(tokens[e], non_stops):
48
+ e += 1
49
+
50
+ while s > 0 and not is_stop(tokens[s - 1], non_stops):
51
+ s -= 1
52
+
53
+ return (s, e)
54
+
55
+
56
+ def maximise(
57
+ loc: int,
58
+ tokens: Sequence[str],
59
+ maxlen: int,
60
+ token_counter: Callable[[str], int],
61
+ non_stops: Iterable[str],
62
+ rng,
63
+ ) -> Tuple[int, int]:
64
+ """Expand a token window by adding complete neighbouring sentences.
65
+
66
+ Expansion starts from the sentence containing the target span, attempts to
67
+ add left context first, then samples remaining left/right context while
68
+ preserving the token-budget constraint.
69
+ """
70
+
71
+ r_count = 0
72
+ f_count = 0
73
+ l_count = 0
74
+
75
+ s, e = extract_sentence(loc, tokens, non_stops)
76
+
77
+ s_done = s == 0
78
+ e_done = e == len(tokens) - 1
79
+
80
+ while not (s_done and e_done):
81
+ l_count += 1
82
+ if not s_done and (r_count < 2 or rng.randint(0, 1) == 0):
83
+ new_s, _ = extract_sentence(s - 1, tokens, non_stops)
84
+ if token_counter(" ".join(tokens[new_s : e + 1])) < maxlen:
85
+ r_count += 1
86
+ s = new_s
87
+ if s == 0:
88
+ s_done = True
89
+ else:
90
+ s_done = True
91
+ else:
92
+ if e >= len(tokens) - 1:
93
+ e_done = True
94
+ continue
95
+ _, new_e = extract_sentence(e + 1, tokens, non_stops)
96
+ if token_counter(" ".join(tokens[s : new_e + 1])) < maxlen:
97
+ f_count += 1
98
+ e = new_e
99
+ if e == len(tokens) - 1:
100
+ e_done = True
101
+ else:
102
+ e_done = True
103
+
104
+ return s, e
@@ -0,0 +1,30 @@
1
+ # Copyright 2026 tznurmin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Utilities for extracting word/phrase lists from curated spans."""
15
+
16
+ from __future__ import annotations
17
+
18
+ from collections.abc import Mapping
19
+
20
+
21
+ def extract_span_phrases(tokens: list[str], curation_subset: Mapping[str, list[str]]) -> set[str]:
22
+ """Return set of ' '.join(tokens[start:start+len]) for each curated location."""
23
+ word_list: set[str] = set()
24
+ for _, locs in curation_subset.items():
25
+ for loc in locs:
26
+ s, e = loc.split('+')
27
+ s_i = int(s)
28
+ e_i = int(e)
29
+ word_list.add(' '.join(tokens[s_i:s_i + e_i]))
30
+ return word_list