LanguageStatisticsLibPy 1.0.3__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.
- languagestatisticslibpy-1.0.3/LICENSE +201 -0
- languagestatisticslibpy-1.0.3/PKG-INFO +124 -0
- languagestatisticslibpy-1.0.3/README.md +106 -0
- languagestatisticslibpy-1.0.3/pyproject.toml +27 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Bigrams.py +124 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Grams.py +134 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/GramsType.py +38 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Hexagrams.py +130 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/LanguageStatistics.py +313 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/LanguageStatisticsFile.py +108 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Node.py +89 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Pentagrams.py +128 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Tetragrams.py +125 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Trigrams.py +125 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/Unigrams.py +110 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/WordTree.py +162 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/__init__.py +0 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/test1.py +26 -0
- languagestatisticslibpy-1.0.3/src/languagestatisticslibpy/test2.py +80 -0
|
@@ -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 [yyyy] [name of copyright owner]
|
|
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.
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: LanguageStatisticsLibPy
|
|
3
|
+
Version: 1.0.3
|
|
4
|
+
Summary: Quick and robust Python functions to do cryptanalysis using language statistics data for 15 languages
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Author: Nils Kopal and Bernhard Esslinger
|
|
7
|
+
Author-email: kopal@cryptool.org
|
|
8
|
+
Requires-Python: >=3.10,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Project-URL: Homepage, https://github.com/CrypToolProject/LanguageStatisticsLibPy
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# LanguageStatisticsLibPy
|
|
20
|
+
|
|
21
|
+
LanguageStatisticsLibPy is a Python library designed to facilitate the fast analysis and manipulation of language statistics data. It originates from a C# library that was first used in the widespread cryptography e-learning software CrypTool 2. From now on "CrypTool 2" is abbreviated "CT2". CT2 is an open-source e-learning program for Windows to do cryptography and cryptanalysis (https://www.cryptool.org/en/ct2/).
|
|
22
|
+
|
|
23
|
+
This Python library supports 15 different languages and offers functionality for generating and handling n-gram data, specifically for calculating n-gram frequencies using the language statistics files from CT2.
|
|
24
|
+
Additionally, it facilitates the use of CT2's dictionaries through a "Word Tree", an efficient data structure for rapid word searches within a language.
|
|
25
|
+
|
|
26
|
+
The language statistics files (for example, `en-5gram-nocs.gz` indicates an English 5-gram file that is not case-sensitive and excludes spaces) can be found in the "LanguageStatistics" subdirectory of CT2, if you have installed CT2 on Windows.
|
|
27
|
+
If you don't have a Windows machine or you don't want to install CT2, you may download the language statistics files and dictionaries from the CT2 Github repo: [Language Statistics](https://github.com/CrypToolProject/CrypTool-2/tree/main/LanguageStatistics).
|
|
28
|
+
|
|
29
|
+
Remark: This package contains the implemented algorithms without the language statistics files. These files have to be downloaded separately as they occupy many megabytes.
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- **Support for multiple languages**: The library includes predefined support for 15 languages, including English, German, Spanish, French -- each with its own set of unigram frequencies and alphabets.
|
|
34
|
+
|
|
35
|
+
- **N-gram loading**: Users can load unigrams, bigrams, trigrams, tetragrams, pentagrams, and hexagrams as n-gram objects in supported languages, with the option to include or exclude spaces. The n-grams delivered in the language statistics files range from 1 to 5 (we don't deliver 6-grams within CT2, since these files are too big). All language statistics files delivered are case-insensitive, denoted as "nocs" in the filename. Each language statistics is available in two forms: with space/blank ("sp" in the filename) and without space/blank (indicated by the absence of "sp" in the filename) within the alphabet.
|
|
36
|
+
|
|
37
|
+
- **Index of Coincidence calculation**: It offers a method to calculate the index of coincidence (IoC) for a given plaintext, which is useful for cryptanalysis and language pattern recognition.
|
|
38
|
+
|
|
39
|
+
- **Alphabet and number mapping**: The library provides functionality to map characters to their respective positions in a language's alphabet and vice versa, supporting operations on encoded messages or language data.
|
|
40
|
+
|
|
41
|
+
- **Dynamic n-gram support**: Depending on the available data, the library dynamically supports various n-gram types.
|
|
42
|
+
- **Word tree data structure**: It supports a word tree data structure for fast word lookups (true = part of language, false = not part of language) of a specific language.
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
Prerequisites: LanguageStatisticsLibPy is installed on your computer via
|
|
47
|
+
**$ pip3 install LanguageStatisticsLibPy**
|
|
48
|
+
|
|
49
|
+
1. **Initialization**: Start by importing the `LanguageStatistics` class and specify the language code for your analysis.
|
|
50
|
+
2. **Loading n-grams**: To load n-grams of your chosen type (e.g., unigrams, bigrams) for a specific language, use the `create_grams` method with the appropriate .gz file from the LanguageStatistics directory in CT2. For instance, to load English 4-grams that are case-insensitive and include the space/blank symbol, use the file named `en-4gram-nocs-sp.gz`.
|
|
51
|
+
3. **Calculating IoC**: Calculate the index of coincidence for a given plaintext using the `calculate_ioc` method.
|
|
52
|
+
4. **Word tree loading**: For advanced language analysis, load a pre-built word tree for a specific language using the `load_word_tree` method.
|
|
53
|
+
|
|
54
|
+
Sample usage (from file `test1.py`):
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from languagestatisticslibpy.LanguageStatistics import LanguageStatistics as LS
|
|
58
|
+
|
|
59
|
+
plaintext = LS.map_text_into_number_space("HELLOWORD", LS.alphabets['en'])
|
|
60
|
+
ioc = LS.calculate_ioc(plaintext)
|
|
61
|
+
|
|
62
|
+
print(ioc)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
You can find further example usages in the file `test2.py` within the package.
|
|
66
|
+
|
|
67
|
+
## Supported languages
|
|
68
|
+
|
|
69
|
+
The library includes predefined configurations for the following languages:
|
|
70
|
+
- English (en)
|
|
71
|
+
- German (de)
|
|
72
|
+
- Spanish (es)
|
|
73
|
+
- French (fr)
|
|
74
|
+
- Italian (it)
|
|
75
|
+
- Hungarian (hu)
|
|
76
|
+
- Russian (ru)
|
|
77
|
+
- Czech (cs)
|
|
78
|
+
- Greek (el)
|
|
79
|
+
- Latin (la)
|
|
80
|
+
- Dutch (nl)
|
|
81
|
+
- Swedish (sv)
|
|
82
|
+
- Portuguese (pt)
|
|
83
|
+
- Polish (pl)
|
|
84
|
+
- Turkish (tr)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
## Some more technical details
|
|
88
|
+
|
|
89
|
+
### Where are the package files stored after installing the package and how to find this out
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
% pip3 list | grep LanguageStatisticsLibPy
|
|
93
|
+
% pip3 show LanguageStatisticsLibPy
|
|
94
|
+
|
|
95
|
+
# show package content (including the test files) for example on Mac
|
|
96
|
+
% tree /Users/be/Library/Python/3.13/lib/python/site-packages/LanguageStatisticsLibPy
|
|
97
|
+
# on Linux this could be in:
|
|
98
|
+
# /home/user/.local/lib/python3.10/site-packages/LanguageStatisticsLibPy/
|
|
99
|
+
...
|
|
100
|
+
# show content of a directory where the statistics files had been copied to
|
|
101
|
+
tree /Users/be/Documents/Python/LanguageStatisticsLibPy_PIP-Test/LSLP
|
|
102
|
+
...
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### How to call the test files
|
|
106
|
+
```bash
|
|
107
|
+
% pwd
|
|
108
|
+
/Users/be/Documents/Python/LanguageStatisticsLibPy_PIP-Test/testen2
|
|
109
|
+
|
|
110
|
+
% ls -l
|
|
111
|
+
-rwx------ 1 be staff 956 27 Dez 09:44 test1.py
|
|
112
|
+
-rwx------ 1 be staff 2944 27 Dez 09:42 test2.py
|
|
113
|
+
|
|
114
|
+
% python3 test1.py
|
|
115
|
+
0.08333333333333333
|
|
116
|
+
|
|
117
|
+
% python3 test2.py
|
|
118
|
+
Grams size: 1
|
|
119
|
+
Grams loaded in 0:00:00.000097
|
|
120
|
+
Grams normalized in 0:00:00.000007
|
|
121
|
+
Text: HELLOWORLDTHISISATEST
|
|
122
|
+
Cost value: 771793.56
|
|
123
|
+
...
|
|
124
|
+
```
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# LanguageStatisticsLibPy
|
|
2
|
+
|
|
3
|
+
LanguageStatisticsLibPy is a Python library designed to facilitate the fast analysis and manipulation of language statistics data. It originates from a C# library that was first used in the widespread cryptography e-learning software CrypTool 2. From now on "CrypTool 2" is abbreviated "CT2". CT2 is an open-source e-learning program for Windows to do cryptography and cryptanalysis (https://www.cryptool.org/en/ct2/).
|
|
4
|
+
|
|
5
|
+
This Python library supports 15 different languages and offers functionality for generating and handling n-gram data, specifically for calculating n-gram frequencies using the language statistics files from CT2.
|
|
6
|
+
Additionally, it facilitates the use of CT2's dictionaries through a "Word Tree", an efficient data structure for rapid word searches within a language.
|
|
7
|
+
|
|
8
|
+
The language statistics files (for example, `en-5gram-nocs.gz` indicates an English 5-gram file that is not case-sensitive and excludes spaces) can be found in the "LanguageStatistics" subdirectory of CT2, if you have installed CT2 on Windows.
|
|
9
|
+
If you don't have a Windows machine or you don't want to install CT2, you may download the language statistics files and dictionaries from the CT2 Github repo: [Language Statistics](https://github.com/CrypToolProject/CrypTool-2/tree/main/LanguageStatistics).
|
|
10
|
+
|
|
11
|
+
Remark: This package contains the implemented algorithms without the language statistics files. These files have to be downloaded separately as they occupy many megabytes.
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Support for multiple languages**: The library includes predefined support for 15 languages, including English, German, Spanish, French -- each with its own set of unigram frequencies and alphabets.
|
|
16
|
+
|
|
17
|
+
- **N-gram loading**: Users can load unigrams, bigrams, trigrams, tetragrams, pentagrams, and hexagrams as n-gram objects in supported languages, with the option to include or exclude spaces. The n-grams delivered in the language statistics files range from 1 to 5 (we don't deliver 6-grams within CT2, since these files are too big). All language statistics files delivered are case-insensitive, denoted as "nocs" in the filename. Each language statistics is available in two forms: with space/blank ("sp" in the filename) and without space/blank (indicated by the absence of "sp" in the filename) within the alphabet.
|
|
18
|
+
|
|
19
|
+
- **Index of Coincidence calculation**: It offers a method to calculate the index of coincidence (IoC) for a given plaintext, which is useful for cryptanalysis and language pattern recognition.
|
|
20
|
+
|
|
21
|
+
- **Alphabet and number mapping**: The library provides functionality to map characters to their respective positions in a language's alphabet and vice versa, supporting operations on encoded messages or language data.
|
|
22
|
+
|
|
23
|
+
- **Dynamic n-gram support**: Depending on the available data, the library dynamically supports various n-gram types.
|
|
24
|
+
- **Word tree data structure**: It supports a word tree data structure for fast word lookups (true = part of language, false = not part of language) of a specific language.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
Prerequisites: LanguageStatisticsLibPy is installed on your computer via
|
|
29
|
+
**$ pip3 install LanguageStatisticsLibPy**
|
|
30
|
+
|
|
31
|
+
1. **Initialization**: Start by importing the `LanguageStatistics` class and specify the language code for your analysis.
|
|
32
|
+
2. **Loading n-grams**: To load n-grams of your chosen type (e.g., unigrams, bigrams) for a specific language, use the `create_grams` method with the appropriate .gz file from the LanguageStatistics directory in CT2. For instance, to load English 4-grams that are case-insensitive and include the space/blank symbol, use the file named `en-4gram-nocs-sp.gz`.
|
|
33
|
+
3. **Calculating IoC**: Calculate the index of coincidence for a given plaintext using the `calculate_ioc` method.
|
|
34
|
+
4. **Word tree loading**: For advanced language analysis, load a pre-built word tree for a specific language using the `load_word_tree` method.
|
|
35
|
+
|
|
36
|
+
Sample usage (from file `test1.py`):
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from languagestatisticslibpy.LanguageStatistics import LanguageStatistics as LS
|
|
40
|
+
|
|
41
|
+
plaintext = LS.map_text_into_number_space("HELLOWORD", LS.alphabets['en'])
|
|
42
|
+
ioc = LS.calculate_ioc(plaintext)
|
|
43
|
+
|
|
44
|
+
print(ioc)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
You can find further example usages in the file `test2.py` within the package.
|
|
48
|
+
|
|
49
|
+
## Supported languages
|
|
50
|
+
|
|
51
|
+
The library includes predefined configurations for the following languages:
|
|
52
|
+
- English (en)
|
|
53
|
+
- German (de)
|
|
54
|
+
- Spanish (es)
|
|
55
|
+
- French (fr)
|
|
56
|
+
- Italian (it)
|
|
57
|
+
- Hungarian (hu)
|
|
58
|
+
- Russian (ru)
|
|
59
|
+
- Czech (cs)
|
|
60
|
+
- Greek (el)
|
|
61
|
+
- Latin (la)
|
|
62
|
+
- Dutch (nl)
|
|
63
|
+
- Swedish (sv)
|
|
64
|
+
- Portuguese (pt)
|
|
65
|
+
- Polish (pl)
|
|
66
|
+
- Turkish (tr)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
## Some more technical details
|
|
70
|
+
|
|
71
|
+
### Where are the package files stored after installing the package and how to find this out
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
% pip3 list | grep LanguageStatisticsLibPy
|
|
75
|
+
% pip3 show LanguageStatisticsLibPy
|
|
76
|
+
|
|
77
|
+
# show package content (including the test files) for example on Mac
|
|
78
|
+
% tree /Users/be/Library/Python/3.13/lib/python/site-packages/LanguageStatisticsLibPy
|
|
79
|
+
# on Linux this could be in:
|
|
80
|
+
# /home/user/.local/lib/python3.10/site-packages/LanguageStatisticsLibPy/
|
|
81
|
+
...
|
|
82
|
+
# show content of a directory where the statistics files had been copied to
|
|
83
|
+
tree /Users/be/Documents/Python/LanguageStatisticsLibPy_PIP-Test/LSLP
|
|
84
|
+
...
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### How to call the test files
|
|
88
|
+
```bash
|
|
89
|
+
% pwd
|
|
90
|
+
/Users/be/Documents/Python/LanguageStatisticsLibPy_PIP-Test/testen2
|
|
91
|
+
|
|
92
|
+
% ls -l
|
|
93
|
+
-rwx------ 1 be staff 956 27 Dez 09:44 test1.py
|
|
94
|
+
-rwx------ 1 be staff 2944 27 Dez 09:42 test2.py
|
|
95
|
+
|
|
96
|
+
% python3 test1.py
|
|
97
|
+
0.08333333333333333
|
|
98
|
+
|
|
99
|
+
% python3 test2.py
|
|
100
|
+
Grams size: 1
|
|
101
|
+
Grams loaded in 0:00:00.000097
|
|
102
|
+
Grams normalized in 0:00:00.000007
|
|
103
|
+
Text: HELLOWORLDTHISISATEST
|
|
104
|
+
Cost value: 771793.56
|
|
105
|
+
...
|
|
106
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "LanguageStatisticsLibPy"
|
|
3
|
+
version = "1.0.3"
|
|
4
|
+
description = "Quick and robust Python functions to do cryptanalysis using language statistics data for 15 languages"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = ["Nils Kopal and Bernhard Esslinger <kopal@cryptool.org>"]
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Programming Language :: Python :: 3",
|
|
10
|
+
"License :: OSI Approved :: Apache Software License",
|
|
11
|
+
"Operating System :: OS Independent",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[tool.poetry.urls]
|
|
15
|
+
Homepage = "https://github.com/CrypToolProject/LanguageStatisticsLibPy"
|
|
16
|
+
|
|
17
|
+
[tool.poetry.dependencies]
|
|
18
|
+
python = "^3.10"
|
|
19
|
+
|
|
20
|
+
[tool.poetry.dev-dependencies]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"numpy==2.2.1"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["poetry-core>=1.0.0"]
|
|
27
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
'''
|
|
2
|
+
Copyright 2024 Nils Kopal, Bernhard Esslinger, CrypTool Team
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
'''
|
|
16
|
+
import numpy as np
|
|
17
|
+
import os
|
|
18
|
+
from languagestatisticslibpy.Grams import Grams
|
|
19
|
+
from languagestatisticslibpy.GramsType import GramsType
|
|
20
|
+
from languagestatisticslibpy.LanguageStatisticsFile import LanguageStatisticsFile
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Bigrams(Grams):
|
|
24
|
+
def __init__(self, language, language_statistics_directory, use_spaces=False):
|
|
25
|
+
"""
|
|
26
|
+
Initializes the Bigrams class by calling the parent class (Grams) initializer.
|
|
27
|
+
|
|
28
|
+
Parameters:
|
|
29
|
+
- language (str): The language of the bigram statistics.
|
|
30
|
+
- language_statistics_directory (str): Path to the directory containing language statistics files.
|
|
31
|
+
- use_spaces (bool): Whether to include spaces in the analysis (default: False).
|
|
32
|
+
"""
|
|
33
|
+
super().__init__(language, language_statistics_directory, use_spaces)
|
|
34
|
+
|
|
35
|
+
def load_gz(self, filename, language_statistics_directory):
|
|
36
|
+
"""
|
|
37
|
+
Loads a gzip-compressed file containing bigram frequencies.
|
|
38
|
+
|
|
39
|
+
Parameters:
|
|
40
|
+
- filename (str): The name of the file to load.
|
|
41
|
+
- language_statistics_directory (str): The directory where the statistics file is located.
|
|
42
|
+
|
|
43
|
+
Sets:
|
|
44
|
+
- self.frequencies (np.ndarray): A 2D array of bigram frequencies.
|
|
45
|
+
- self.alphabet (list): The alphabet used in the statistics file.
|
|
46
|
+
- self.max_value (float): The maximum value in the frequencies array, or -∞ if the array is empty.
|
|
47
|
+
"""
|
|
48
|
+
file_path = os.path.join(language_statistics_directory, filename)
|
|
49
|
+
language_statistics_file = LanguageStatisticsFile(file_path)
|
|
50
|
+
self.frequencies = language_statistics_file.load_frequencies(2)
|
|
51
|
+
self.alphabet = language_statistics_file.alphabet
|
|
52
|
+
self.max_value = np.max(self.frequencies) if self.frequencies.size > 0 else float('-inf')
|
|
53
|
+
|
|
54
|
+
def calculate_cost(self, text):
|
|
55
|
+
"""
|
|
56
|
+
Calculates the cost of a given text based on bigram frequencies.
|
|
57
|
+
|
|
58
|
+
Parameters:
|
|
59
|
+
- text (str): The text to analyze.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
- float: The average cost of bigrams in the text. Returns 0 if the text length is less than 2.
|
|
63
|
+
|
|
64
|
+
Notes:
|
|
65
|
+
- Skips bigrams containing characters outside the defined alphabet.
|
|
66
|
+
- If `add_letter_indices` is defined, modifies indices of the characters before computing the cost.
|
|
67
|
+
"""
|
|
68
|
+
if len(text) < 2:
|
|
69
|
+
return 0
|
|
70
|
+
|
|
71
|
+
value = 0
|
|
72
|
+
alphabet_length = len(self.alphabet)
|
|
73
|
+
end = len(text) - 1
|
|
74
|
+
|
|
75
|
+
for i in range(end):
|
|
76
|
+
a = text[i]
|
|
77
|
+
b = text[i + 1]
|
|
78
|
+
|
|
79
|
+
if self.add_letter_indices:
|
|
80
|
+
a += self.add_letter_indices[a]
|
|
81
|
+
b += self.add_letter_indices[b]
|
|
82
|
+
|
|
83
|
+
if a >= alphabet_length or b >= alphabet_length or a < 0 or b < 0:
|
|
84
|
+
continue
|
|
85
|
+
value += self.frequencies[a, b]
|
|
86
|
+
|
|
87
|
+
return value / end
|
|
88
|
+
|
|
89
|
+
def gram_size(self):
|
|
90
|
+
"""
|
|
91
|
+
Returns the size of the grams being analyzed (bigrams in this case).
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
- int: The size of the grams (always 2 for bigrams).
|
|
95
|
+
"""
|
|
96
|
+
return 2
|
|
97
|
+
|
|
98
|
+
def grams_type(self):
|
|
99
|
+
"""
|
|
100
|
+
Returns the type of grams being analyzed.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
- GramsType: An enum value representing the type of grams (GramsType.Bigrams).
|
|
104
|
+
"""
|
|
105
|
+
return GramsType.Bigrams
|
|
106
|
+
|
|
107
|
+
def normalize(self, max_value):
|
|
108
|
+
"""
|
|
109
|
+
Normalizes the bigram frequencies based on the provided maximum value.
|
|
110
|
+
|
|
111
|
+
Parameters:
|
|
112
|
+
- max_value (float): The maximum value used for normalization.
|
|
113
|
+
|
|
114
|
+
Notes:
|
|
115
|
+
- Adjusts all frequencies proportionally to the new maximum value.
|
|
116
|
+
- Updates `self.max_value` to the new maximum after normalization.
|
|
117
|
+
"""
|
|
118
|
+
super().normalize(max_value)
|
|
119
|
+
adjust_value = self.max_value * max_value
|
|
120
|
+
for a in range(len(self.alphabet)):
|
|
121
|
+
for b in range(len(self.alphabet)):
|
|
122
|
+
self.frequencies[a, b] = adjust_value / self.frequencies[a, b]
|
|
123
|
+
self.max_value = np.max(self.frequencies) if self.frequencies.size > 0 else float('-inf')
|
|
124
|
+
|