pyhfst 1.1.0__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.
- pyhfst-1.1.0/LICENCE +201 -0
- pyhfst-1.1.0/PKG-INFO +58 -0
- pyhfst-1.1.0/README.md +40 -0
- pyhfst-1.1.0/c_pyhfst/__init__.pyx +22 -0
- pyhfst-1.1.0/c_pyhfst/analyzer.pxd +30 -0
- pyhfst-1.1.0/c_pyhfst/analyzer.pyx +293 -0
- pyhfst-1.1.0/c_pyhfst/byte_array.pxd +18 -0
- pyhfst-1.1.0/c_pyhfst/byte_array.pyx +89 -0
- pyhfst-1.1.0/c_pyhfst/common.pxd +55 -0
- pyhfst-1.1.0/c_pyhfst/common.pyx +136 -0
- pyhfst-1.1.0/c_pyhfst/constants.h +4 -0
- pyhfst-1.1.0/c_pyhfst/flag_diacritic_operation.pxd +9 -0
- pyhfst-1.1.0/c_pyhfst/flag_diacritic_operation.pyx +28 -0
- pyhfst-1.1.0/c_pyhfst/flag_diacritic_operator.pxd +8 -0
- pyhfst-1.1.0/c_pyhfst/transducer.pxd +14 -0
- pyhfst-1.1.0/c_pyhfst/transducer.pyx +27 -0
- pyhfst-1.1.0/c_pyhfst/transducer_alphabet.pxd +8 -0
- pyhfst-1.1.0/c_pyhfst/transducer_alphabet.pyx +75 -0
- pyhfst-1.1.0/c_pyhfst/transducer_header.pxd +26 -0
- pyhfst-1.1.0/c_pyhfst/transducer_header.pyx +98 -0
- pyhfst-1.1.0/pyhfst/__init__.py +80 -0
- pyhfst-1.1.0/pyhfst/analyzer.py +305 -0
- pyhfst-1.1.0/pyhfst/byte_array.py +90 -0
- pyhfst-1.1.0/pyhfst/common.py +211 -0
- pyhfst-1.1.0/pyhfst/flag_diacritic_operation.py +46 -0
- pyhfst-1.1.0/pyhfst/transducer.py +26 -0
- pyhfst-1.1.0/pyhfst/transducer_alphabet.py +78 -0
- pyhfst-1.1.0/pyhfst/transducer_header.py +98 -0
- pyhfst-1.1.0/pyhfst.egg-info/PKG-INFO +58 -0
- pyhfst-1.1.0/pyhfst.egg-info/SOURCES.txt +34 -0
- pyhfst-1.1.0/pyhfst.egg-info/dependency_links.txt +1 -0
- pyhfst-1.1.0/pyhfst.egg-info/not-zip-safe +1 -0
- pyhfst-1.1.0/pyhfst.egg-info/top_level.txt +2 -0
- pyhfst-1.1.0/setup.cfg +7 -0
- pyhfst-1.1.0/setup.py +109 -0
pyhfst-1.1.0/LICENCE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023 Rootroo Ltd
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
pyhfst-1.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyhfst
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: A pure Python implementation of HFST for using HFST optimized lookup transducers (with or without weights)
|
|
5
|
+
Home-page: https://github.com/Rootroo-ltd/pyhfst
|
|
6
|
+
Author: Khalid Alnajjar and Mika Hämäläinen
|
|
7
|
+
Author-email: hello@rootroo.com
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Project-URL: Bug Reports, https://github.com/Rootroo-ltd/pyhfst/issues
|
|
10
|
+
Project-URL: Developer, https://rootroo.com/
|
|
11
|
+
Keywords: hfst,fst,optimized lookup,hfstol,transducers
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENCE
|
|
18
|
+
|
|
19
|
+
# Pyhfst
|
|
20
|
+
|
|
21
|
+
[](https://doi.org/10.5281/zenodo.7791470)
|
|
22
|
+
|
|
23
|
+
Pyhfst is a pure Python implementation of HFST. The library makes it possible to use HFST optimized lookup FSTs without any C dependencies. Both weighted and unweighted FSTs are supported.
|
|
24
|
+
|
|
25
|
+
The library will run on all operting systems that support Python 3.
|
|
26
|
+
|
|
27
|
+
# Installation
|
|
28
|
+
|
|
29
|
+
pip install pyhfst
|
|
30
|
+
|
|
31
|
+
Pyhfst can run way faster if you have Cython installed. After installing Cython, you must reinstall Pyhfst
|
|
32
|
+
|
|
33
|
+
pip install cython
|
|
34
|
+
pip install --upgrade --force-reinstall pyhfst
|
|
35
|
+
|
|
36
|
+
# Usage
|
|
37
|
+
|
|
38
|
+
import pyhfst
|
|
39
|
+
|
|
40
|
+
input_stream = pyhfst.HfstInputStream("./analyser")
|
|
41
|
+
tr = input_stream.read()
|
|
42
|
+
print(tr.lookup("voi"))
|
|
43
|
+
|
|
44
|
+
>> [['voida+V+Act+Ind+Prs+Sg3', 0.0], ['voida+V+Act+Ind+Prs+ConNeg', 0.0], ['voida+V+Act+Ind+Prt+Sg3', 0.0], ['voida+V+Act+Imprt+Prs+ConNeg+Sg2', 0.0], ['voida+V+Act+Imprt+Sg2', 0.0], ['voi+N+Sg+Nom', 0.0], ['voi+Pcle', 0.0], ['voi+Interj', 0.0]]
|
|
45
|
+
|
|
46
|
+
# Citation
|
|
47
|
+
|
|
48
|
+
Please cite the library as follows:
|
|
49
|
+
|
|
50
|
+
Hämäläinen, Mika, & Alnajjar, Khalid. (2023). Pyhfst: A Pure Python Implementation of HFST. Zenodo. https://doi.org/10.5281/zenodo.7791470
|
|
51
|
+
|
|
52
|
+
@article{pyhfst_2023,
|
|
53
|
+
title={Pyhfst: A Pure Python Implementation of HFST},
|
|
54
|
+
DOI={10.5281/zenodo.7791470},
|
|
55
|
+
publisher={Zenodo},
|
|
56
|
+
author={Hämäläinen, Mika and Alnajjar, Khalid},
|
|
57
|
+
year={2023}
|
|
58
|
+
}
|
pyhfst-1.1.0/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Pyhfst
|
|
2
|
+
|
|
3
|
+
[](https://doi.org/10.5281/zenodo.7791470)
|
|
4
|
+
|
|
5
|
+
Pyhfst is a pure Python implementation of HFST. The library makes it possible to use HFST optimized lookup FSTs without any C dependencies. Both weighted and unweighted FSTs are supported.
|
|
6
|
+
|
|
7
|
+
The library will run on all operting systems that support Python 3.
|
|
8
|
+
|
|
9
|
+
# Installation
|
|
10
|
+
|
|
11
|
+
pip install pyhfst
|
|
12
|
+
|
|
13
|
+
Pyhfst can run way faster if you have Cython installed. After installing Cython, you must reinstall Pyhfst
|
|
14
|
+
|
|
15
|
+
pip install cython
|
|
16
|
+
pip install --upgrade --force-reinstall pyhfst
|
|
17
|
+
|
|
18
|
+
# Usage
|
|
19
|
+
|
|
20
|
+
import pyhfst
|
|
21
|
+
|
|
22
|
+
input_stream = pyhfst.HfstInputStream("./analyser")
|
|
23
|
+
tr = input_stream.read()
|
|
24
|
+
print(tr.lookup("voi"))
|
|
25
|
+
|
|
26
|
+
>> [['voida+V+Act+Ind+Prs+Sg3', 0.0], ['voida+V+Act+Ind+Prs+ConNeg', 0.0], ['voida+V+Act+Ind+Prt+Sg3', 0.0], ['voida+V+Act+Imprt+Prs+ConNeg+Sg2', 0.0], ['voida+V+Act+Imprt+Sg2', 0.0], ['voi+N+Sg+Nom', 0.0], ['voi+Pcle', 0.0], ['voi+Interj', 0.0]]
|
|
27
|
+
|
|
28
|
+
# Citation
|
|
29
|
+
|
|
30
|
+
Please cite the library as follows:
|
|
31
|
+
|
|
32
|
+
Hämäläinen, Mika, & Alnajjar, Khalid. (2023). Pyhfst: A Pure Python Implementation of HFST. Zenodo. https://doi.org/10.5281/zenodo.7791470
|
|
33
|
+
|
|
34
|
+
@article{pyhfst_2023,
|
|
35
|
+
title={Pyhfst: A Pure Python Implementation of HFST},
|
|
36
|
+
DOI={10.5281/zenodo.7791470},
|
|
37
|
+
publisher={Zenodo},
|
|
38
|
+
author={Hämäläinen, Mika and Alnajjar, Khalid},
|
|
39
|
+
year={2023}
|
|
40
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typing import Union, List
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from io import BufferedReader
|
|
4
|
+
from .transducer_header cimport TransducerHeader
|
|
5
|
+
from .transducer_alphabet cimport TransducerAlphabet
|
|
6
|
+
from .transducer cimport Transducer as Transducer
|
|
7
|
+
from .analyzer import Analyzer
|
|
8
|
+
|
|
9
|
+
cpdef Transducer get_transducer(transducer_path: Union[str, Path]):
|
|
10
|
+
"""
|
|
11
|
+
Creates a Transducer instance from the given transducer file path.
|
|
12
|
+
|
|
13
|
+
:param transducer_path: The path to the transducer file.
|
|
14
|
+
:return: A Transducer instance.
|
|
15
|
+
"""
|
|
16
|
+
with open(transducer_path, "rb") as transducer_file:
|
|
17
|
+
char_stream = BufferedReader(transducer_file)
|
|
18
|
+
transducer_header = TransducerHeader(char_stream)
|
|
19
|
+
transducer_alphabet = TransducerAlphabet(
|
|
20
|
+
char_stream, transducer_header.get_symbol_count()
|
|
21
|
+
)
|
|
22
|
+
return Transducer(char_stream, transducer_header, transducer_alphabet, is_weighted=transducer_header.is_weighted())
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# cython: language_level=3
|
|
2
|
+
from .common cimport *
|
|
3
|
+
from .flag_diacritic_operation cimport FlagDiacriticOperation
|
|
4
|
+
from .transducer import Transducer
|
|
5
|
+
import cython
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
cdef class Analyzer:
|
|
9
|
+
cdef Transducer transducer
|
|
10
|
+
cdef str input_str
|
|
11
|
+
cdef State state
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
cpdef cython.longlong pivot(self, cython.longlong i)
|
|
15
|
+
cpdef void try_epsilon_indices(self, cython.longlong index)
|
|
16
|
+
cpdef void try_epsilon_transitions(self, cython.longlong index)
|
|
17
|
+
cpdef void find_index(self, cython.longlong index)
|
|
18
|
+
cpdef void handle_epsilon_transition(self, cython.longlong index)
|
|
19
|
+
cpdef void find_transitions(self, cython.longlong index)
|
|
20
|
+
cpdef void get_analyses(self, cython.longlong idx)
|
|
21
|
+
cpdef void update_output_string(self, cython.longlong output_symbol)
|
|
22
|
+
cpdef void handle_end_of_input_string(self, cython.longlong index, bint is_transition)
|
|
23
|
+
cpdef void reset_output_pointer(self)
|
|
24
|
+
cpdef tuple get_final_and_weight(self, cython.longlong index, bint is_transition)
|
|
25
|
+
cpdef void update_and_note_analysis(self, float weight)
|
|
26
|
+
cpdef list get_symbols(self)
|
|
27
|
+
cpdef void note_analysis(self)
|
|
28
|
+
cpdef list get_alphabet(self)
|
|
29
|
+
cpdef public list analyze(self)
|
|
30
|
+
cpdef bint push_state(self, FlagDiacriticOperation flag)
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# cython: language_level=3
|
|
2
|
+
from .common cimport *
|
|
3
|
+
from .flag_diacritic_operation cimport FlagDiacriticOperation, FlagDiacriticOperator
|
|
4
|
+
from .transducer import Transducer
|
|
5
|
+
|
|
6
|
+
cdef class Analyzer:
|
|
7
|
+
|
|
8
|
+
def __cinit__(self, transducer: Transducer, input_str: str):
|
|
9
|
+
self.transducer = transducer
|
|
10
|
+
self.input_str = input_str
|
|
11
|
+
self.state = State(input_str, self.transducer)
|
|
12
|
+
|
|
13
|
+
cpdef cython.longlong pivot(self, cython.longlong i):
|
|
14
|
+
"""
|
|
15
|
+
Computes the pivot for the given index.
|
|
16
|
+
|
|
17
|
+
:param i: The index to compute the pivot for.
|
|
18
|
+
:return: The pivot value.
|
|
19
|
+
"""
|
|
20
|
+
if i >= TRANSITION_TARGET_TABLE_START:
|
|
21
|
+
return (i - TRANSITION_TARGET_TABLE_START) % TRANSITION_TARGET_TABLE_START
|
|
22
|
+
return i
|
|
23
|
+
|
|
24
|
+
cpdef void try_epsilon_indices(self, cython.longlong index):
|
|
25
|
+
"""
|
|
26
|
+
Tries epsilon indices for the given index and state.
|
|
27
|
+
|
|
28
|
+
:param index: The index to try epsilon indices for.
|
|
29
|
+
:param state: The current state of the transducer.
|
|
30
|
+
"""
|
|
31
|
+
if self.transducer.index_table.get_input(index) == 0:
|
|
32
|
+
self.try_epsilon_transitions(self.pivot(
|
|
33
|
+
self.transducer.index_table.get_target(index)))
|
|
34
|
+
|
|
35
|
+
cpdef void try_epsilon_transitions(self, cython.longlong index):
|
|
36
|
+
"""
|
|
37
|
+
Tries epsilon transitions for the given index and state.
|
|
38
|
+
|
|
39
|
+
:param index: The index to try epsilon transitions for.
|
|
40
|
+
:param state: The current state of the transducer.
|
|
41
|
+
"""
|
|
42
|
+
cdef cython.longlong input_symbol
|
|
43
|
+
|
|
44
|
+
while True:
|
|
45
|
+
input_symbol = self.transducer.transition_table.get_input(index)
|
|
46
|
+
if self.transducer.operations.get(input_symbol):
|
|
47
|
+
if self.push_state(self.transducer.operations[input_symbol]):
|
|
48
|
+
self.handle_epsilon_transition(index)
|
|
49
|
+
index += 1
|
|
50
|
+
self.state.state_stack.pop()
|
|
51
|
+
continue
|
|
52
|
+
elif input_symbol == 0:
|
|
53
|
+
self.handle_epsilon_transition(index)
|
|
54
|
+
index += 1
|
|
55
|
+
continue
|
|
56
|
+
else:
|
|
57
|
+
break
|
|
58
|
+
|
|
59
|
+
cpdef void find_index(self, cython.longlong index):
|
|
60
|
+
"""
|
|
61
|
+
Finds the index in the transducer for the given index and state.
|
|
62
|
+
|
|
63
|
+
:param index: The index to find in the transducer.
|
|
64
|
+
:param state: The current state of the transducer.
|
|
65
|
+
"""
|
|
66
|
+
if self.transducer.index_table.get_input(index + (self.state.input_string[self.state.input_pointer - 1])) == self.state.input_string[self.state.input_pointer - 1]:
|
|
67
|
+
self.find_transitions(self.pivot(self.transducer.index_table.get_target(
|
|
68
|
+
index + self.state.input_string[self.state.input_pointer - 1])))
|
|
69
|
+
|
|
70
|
+
cpdef void handle_epsilon_transition(self, cython.longlong index):
|
|
71
|
+
"""
|
|
72
|
+
Handles epsilon transitions for the given index and state.
|
|
73
|
+
|
|
74
|
+
:param index: The index to handle epsilon transitions for.
|
|
75
|
+
:param state: The current state of the transducer.
|
|
76
|
+
"""
|
|
77
|
+
self.update_output_string(self.transducer.transition_table.get_output(index))
|
|
78
|
+
self.state.output_pointer += 1
|
|
79
|
+
|
|
80
|
+
if self.transducer.is_weighted:
|
|
81
|
+
self.state.current_weight += self.transducer.transition_table.get_weight(index)
|
|
82
|
+
|
|
83
|
+
self.get_analyses(self.transducer.transition_table.get_target(index))
|
|
84
|
+
|
|
85
|
+
if self.transducer.is_weighted:
|
|
86
|
+
self.state.current_weight -= self.transducer.transition_table.get_weight(index)
|
|
87
|
+
|
|
88
|
+
self.state.output_pointer -= 1
|
|
89
|
+
|
|
90
|
+
cpdef void find_transitions(self, cython.longlong index):
|
|
91
|
+
"""
|
|
92
|
+
Finds the transitions in the transducer for the given index and state.
|
|
93
|
+
|
|
94
|
+
:param index: The index to find transitions for.
|
|
95
|
+
:param state: The current state of the transducer.
|
|
96
|
+
"""
|
|
97
|
+
for idx in range(index, self.transducer.transition_table.size()):
|
|
98
|
+
input_symbol = self.transducer.transition_table.get_input(idx)
|
|
99
|
+
if input_symbol == NO_SYMBOL_NUMBER:
|
|
100
|
+
break
|
|
101
|
+
|
|
102
|
+
if input_symbol == self.state.input_string[self.state.input_pointer - 1]:
|
|
103
|
+
self.update_output_string(self.transducer.transition_table.get_output(idx))
|
|
104
|
+
self.state.output_pointer += 1
|
|
105
|
+
|
|
106
|
+
if self.transducer.is_weighted:
|
|
107
|
+
self.state.current_weight += self.transducer.transition_table.get_weight(
|
|
108
|
+
idx)
|
|
109
|
+
|
|
110
|
+
self.get_analyses(self.transducer.transition_table.get_target(idx))
|
|
111
|
+
|
|
112
|
+
if self.transducer.is_weighted:
|
|
113
|
+
self.state.current_weight -= self.transducer.transition_table.get_weight(
|
|
114
|
+
idx)
|
|
115
|
+
|
|
116
|
+
self.state.output_pointer -= 1
|
|
117
|
+
|
|
118
|
+
cpdef void get_analyses(self, cython.longlong idx):
|
|
119
|
+
"""
|
|
120
|
+
Gets the analyses for the given index and state.
|
|
121
|
+
|
|
122
|
+
:param idx: The index to get the analyses for.
|
|
123
|
+
:param state: The current state of the transducer.
|
|
124
|
+
"""
|
|
125
|
+
cdef cython.longlong index = self.pivot(idx)
|
|
126
|
+
is_transition = idx >= TRANSITION_TARGET_TABLE_START
|
|
127
|
+
if is_transition:
|
|
128
|
+
self.try_epsilon_transitions(index + 1)
|
|
129
|
+
else:
|
|
130
|
+
self.try_epsilon_indices(index + 1)
|
|
131
|
+
|
|
132
|
+
if self.state.input_string[self.state.input_pointer] == NO_SYMBOL_NUMBER:
|
|
133
|
+
self.handle_end_of_input_string(index, is_transition)
|
|
134
|
+
return
|
|
135
|
+
|
|
136
|
+
self.state.input_pointer += 1
|
|
137
|
+
|
|
138
|
+
if is_transition:
|
|
139
|
+
self.find_transitions(index + 1)
|
|
140
|
+
else:
|
|
141
|
+
self.find_index(index + 1)
|
|
142
|
+
|
|
143
|
+
self.state.input_pointer -= 1
|
|
144
|
+
self.reset_output_pointer()
|
|
145
|
+
|
|
146
|
+
cpdef void update_output_string(self, cython.longlong output_symbol):
|
|
147
|
+
"""
|
|
148
|
+
Updates the output string in the state based on the output symbol.
|
|
149
|
+
|
|
150
|
+
:param state: The current state of the transducer.
|
|
151
|
+
:param output_symbol: The output symbol to append or update in the output string.
|
|
152
|
+
"""
|
|
153
|
+
if self.state.output_pointer == len(self.state.output_string):
|
|
154
|
+
self.state.output_string.append(output_symbol)
|
|
155
|
+
else:
|
|
156
|
+
self.state.output_string[self.state.output_pointer] = output_symbol
|
|
157
|
+
|
|
158
|
+
cpdef void handle_end_of_input_string(self, cython.longlong index, bint is_transition):
|
|
159
|
+
"""
|
|
160
|
+
Handles the end of the input string.
|
|
161
|
+
|
|
162
|
+
:param state: The current state of the transducer.
|
|
163
|
+
:param index: The index being processed.
|
|
164
|
+
:param is_transition: A boolean indicating whether it's a transition table or an index table.
|
|
165
|
+
"""
|
|
166
|
+
self.reset_output_pointer()
|
|
167
|
+
|
|
168
|
+
is_final, weight = self.get_final_and_weight(index, is_transition)
|
|
169
|
+
if is_final:
|
|
170
|
+
self.update_and_note_analysis(weight)
|
|
171
|
+
|
|
172
|
+
cpdef void reset_output_pointer(self):
|
|
173
|
+
"""
|
|
174
|
+
Resets the output pointer in the state.
|
|
175
|
+
|
|
176
|
+
:param state: The current state of the transducer.
|
|
177
|
+
"""
|
|
178
|
+
if self.state.output_pointer == len(self.state.output_string):
|
|
179
|
+
self.state.output_string.append(NO_SYMBOL_NUMBER)
|
|
180
|
+
else:
|
|
181
|
+
self.state.output_string[self.state.output_pointer] = NO_SYMBOL_NUMBER
|
|
182
|
+
|
|
183
|
+
cpdef tuple get_final_and_weight(self, cython.longlong index, bint is_transition):
|
|
184
|
+
"""
|
|
185
|
+
Gets the final state and weight based on the index and transition flag.
|
|
186
|
+
|
|
187
|
+
:param index: The index being processed.
|
|
188
|
+
:param is_transition: A boolean indicating whether it's a transition table or an index table.
|
|
189
|
+
:return: A tuple containing a boolean for final state and a float for the weight.
|
|
190
|
+
"""
|
|
191
|
+
if is_transition:
|
|
192
|
+
is_final = self.transducer.transition_table.size(
|
|
193
|
+
) > index and self.transducer.transition_table.is_final(index)
|
|
194
|
+
weight = self.transducer.transition_table.get_weight(
|
|
195
|
+
index) if self.transducer.is_weighted else 0
|
|
196
|
+
else:
|
|
197
|
+
is_final = self.transducer.index_table.is_final(index)
|
|
198
|
+
weight = self.transducer.index_table.get_final_weight(
|
|
199
|
+
index) if self.transducer.is_weighted else 0
|
|
200
|
+
|
|
201
|
+
return is_final, weight
|
|
202
|
+
|
|
203
|
+
cpdef void update_and_note_analysis(self, float weight):
|
|
204
|
+
"""
|
|
205
|
+
Updates the state's current weight and notes the analysis.
|
|
206
|
+
|
|
207
|
+
:param state: The current state of the transducer.
|
|
208
|
+
:param weight: The weight to be
|
|
209
|
+
"""
|
|
210
|
+
if self.transducer.is_weighted:
|
|
211
|
+
self.state.current_weight += weight
|
|
212
|
+
self.note_analysis()
|
|
213
|
+
if self.transducer.is_weighted:
|
|
214
|
+
self.state.current_weight -= weight
|
|
215
|
+
|
|
216
|
+
cpdef list get_symbols(self):
|
|
217
|
+
"""
|
|
218
|
+
Gets the symbols for the given state.
|
|
219
|
+
|
|
220
|
+
:param state: The current state of the transducer.
|
|
221
|
+
:return: A list of symbols.
|
|
222
|
+
"""
|
|
223
|
+
symbols = [self.transducer.alphabet.keyTable[self.state.output_string[i]] for i in range(
|
|
224
|
+
len(self.state.output_string)) if self.state.output_string[i] != NO_SYMBOL_NUMBER]
|
|
225
|
+
return symbols
|
|
226
|
+
|
|
227
|
+
cpdef void note_analysis(self):
|
|
228
|
+
"""
|
|
229
|
+
Notes the analysis for the given state.
|
|
230
|
+
|
|
231
|
+
:param state: The current state of the transducer.
|
|
232
|
+
"""
|
|
233
|
+
self.state.display_vector.append(Result(self.get_symbols(), self.state.current_weight if self.transducer.is_weighted else 1.0))
|
|
234
|
+
|
|
235
|
+
cpdef list get_alphabet(self):
|
|
236
|
+
"""
|
|
237
|
+
Gets the alphabet of the transducer.
|
|
238
|
+
|
|
239
|
+
:return: A list of alphabet symbols.
|
|
240
|
+
"""
|
|
241
|
+
return self.transducer.alphabet.keyTable
|
|
242
|
+
|
|
243
|
+
cpdef public list analyze(self):
|
|
244
|
+
if self.state.input_string[0] == NO_SYMBOL_NUMBER:
|
|
245
|
+
return []
|
|
246
|
+
else:
|
|
247
|
+
self.get_analyses(0)
|
|
248
|
+
return self.state.display_vector
|
|
249
|
+
|
|
250
|
+
cpdef bint push_state(self, FlagDiacriticOperation flag):
|
|
251
|
+
cdef dict top = self.state.state_stack[-1]
|
|
252
|
+
if flag.op == FlagDiacriticOperator.P: # positive set
|
|
253
|
+
self.state.state_stack.append(top)
|
|
254
|
+
self.state.state_stack[-1][flag.feature] = flag.value
|
|
255
|
+
return True
|
|
256
|
+
elif flag.op == FlagDiacriticOperator.N: # negative set
|
|
257
|
+
self.state.state_stack.append(top)
|
|
258
|
+
self.state.state_stack[-1][flag.feature] = -1 * flag.value
|
|
259
|
+
return True
|
|
260
|
+
elif flag.op == FlagDiacriticOperator.R: # require
|
|
261
|
+
if flag.value == 0: # empty require
|
|
262
|
+
if self.state.state_stack[-1][flag.feature] == 0:
|
|
263
|
+
return False
|
|
264
|
+
else:
|
|
265
|
+
self.state.state_stack.append(top)
|
|
266
|
+
return True
|
|
267
|
+
elif self.state.state_stack[-1][flag.feature] == flag.value:
|
|
268
|
+
self.state.state_stack.append(top)
|
|
269
|
+
return True
|
|
270
|
+
return False
|
|
271
|
+
elif flag.op == FlagDiacriticOperator.D: # disallow
|
|
272
|
+
if flag.value == 0: # empty disallow
|
|
273
|
+
if self.state.state_stack[-1][flag.feature] != 0:
|
|
274
|
+
return False
|
|
275
|
+
else:
|
|
276
|
+
self.state.state_stack.append(top)
|
|
277
|
+
return True
|
|
278
|
+
elif self.state.state_stack[-1][flag.feature] == flag.value:
|
|
279
|
+
return False
|
|
280
|
+
else:
|
|
281
|
+
self.state.state_stack.append(top)
|
|
282
|
+
return True
|
|
283
|
+
elif flag.op == FlagDiacriticOperator.C: # clear
|
|
284
|
+
self.state.state_stack.append(top)
|
|
285
|
+
self.state.state_stack[-1][flag.feature] = 0
|
|
286
|
+
return True
|
|
287
|
+
elif flag.op == FlagDiacriticOperator.U: # unification
|
|
288
|
+
if (self.state.state_stack[-1][flag.feature] == 0) or (self.state.state_stack[-1][flag.feature] == flag.value) or (self.state.state_stack[-1][flag.feature] != flag.value and self.state.state_stack[-1][flag.feature] < 0):
|
|
289
|
+
self.state.state_stack.append(top)
|
|
290
|
+
self.state.state_stack[-1][flag.feature] = flag.value
|
|
291
|
+
return True
|
|
292
|
+
return False
|
|
293
|
+
return False # compiler sanity
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# cython: language_level=3
|
|
2
|
+
|
|
3
|
+
from libc.stdint cimport uint8_t, uint16_t, uint32_t
|
|
4
|
+
|
|
5
|
+
cdef class ByteArray:
|
|
6
|
+
|
|
7
|
+
cdef bytearray bytes
|
|
8
|
+
cdef int size
|
|
9
|
+
cdef int index
|
|
10
|
+
|
|
11
|
+
cpdef public int get_size(self)
|
|
12
|
+
cpdef public bytes get(self, int i)
|
|
13
|
+
cpdef public bytearray get_bytes(self)
|
|
14
|
+
cpdef public uint8_t get_ubyte(self)
|
|
15
|
+
cpdef public uint16_t get_ushort(self)
|
|
16
|
+
cpdef public uint32_t get_uint(self)
|
|
17
|
+
cpdef public bint get_bool(self)
|
|
18
|
+
cpdef public float get_float(self)
|