saphes 0.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.
- saphes-0.1.0/LICENSE +21 -0
- saphes-0.1.0/PKG-INFO +196 -0
- saphes-0.1.0/README.md +157 -0
- saphes-0.1.0/pyproject.toml +132 -0
- saphes-0.1.0/pyproject.toml.orig +111 -0
- saphes-0.1.0/src/saphes/__init__.py +60 -0
- saphes-0.1.0/src/saphes/_types.py +60 -0
- saphes-0.1.0/src/saphes/calibration.py +694 -0
- saphes-0.1.0/src/saphes/datasets/__init__.py +147 -0
- saphes-0.1.0/src/saphes/datasets/_lix_calibration.py +63 -0
- saphes-0.1.0/src/saphes/datasets/_samples.py +71 -0
- saphes-0.1.0/src/saphes/diversity.py +356 -0
- saphes-0.1.0/src/saphes/py.typed +0 -0
- saphes-0.1.0/src/saphes/readability.py +564 -0
- saphes-0.1.0/src/saphes/segment.py +178 -0
saphes-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Crow Intelligence
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
saphes-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: saphes
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Readability (LIX) and lexical diversity (TTR/MATTR) with the parameters other implementations hardcode
|
|
5
|
+
Keywords: nlp,readability,lix,lexical-diversity,type-token-ratio,mattr,corpus-linguistics,computational-humanities
|
|
6
|
+
Author: Zoltan Varju, Orsolya Putz
|
|
7
|
+
Author-email: Zoltan Varju <zoltan.varju@crowintelligence.org>, Orsolya Putz <orsolya.putz@crowintelligence.org>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
17
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
18
|
+
Requires-Dist: pytest>=8 ; extra == 'dev'
|
|
19
|
+
Requires-Dist: pytest-cov ; extra == 'dev'
|
|
20
|
+
Requires-Dist: hypothesis>=6.100 ; extra == 'dev'
|
|
21
|
+
Requires-Dist: ruff>=0.4 ; extra == 'dev'
|
|
22
|
+
Requires-Dist: ty ; extra == 'dev'
|
|
23
|
+
Requires-Dist: mutmut>=3 ; extra == 'dev'
|
|
24
|
+
Requires-Dist: textstat>=0.7 ; extra == 'dev'
|
|
25
|
+
Requires-Dist: matplotlib>=3.8 ; extra == 'dev'
|
|
26
|
+
Requires-Dist: jupyter>=1.0 ; extra == 'dev'
|
|
27
|
+
Requires-Dist: ipykernel>=6.29 ; extra == 'dev'
|
|
28
|
+
Requires-Dist: mkdocs-material ; extra == 'docs'
|
|
29
|
+
Requires-Dist: mkdocstrings[python] ; extra == 'docs'
|
|
30
|
+
Requires-Dist: nltk>=3.8 ; extra == 'punkt'
|
|
31
|
+
Requires-Python: >=3.11
|
|
32
|
+
Project-URL: Homepage, https://crowintelligence.org/
|
|
33
|
+
Project-URL: Repository, https://github.com/crow-intelligence/saphes
|
|
34
|
+
Project-URL: Documentation, https://saphes.readthedocs.io
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Provides-Extra: docs
|
|
37
|
+
Provides-Extra: punkt
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
39
|
+
|
|
40
|
+
<p align="center">
|
|
41
|
+
<img src="https://raw.githubusercontent.com/crow-intelligence/saphes/main/img/saphes_logo.png" alt="saphes logo" width="480">
|
|
42
|
+
</p>
|
|
43
|
+
|
|
44
|
+
# saphes
|
|
45
|
+
|
|
46
|
+
Readability and lexical diversity — two metrics, done carefully, with the parameters other
|
|
47
|
+
implementations hardcode.
|
|
48
|
+
|
|
49
|
+
*saphes* — σαφής, "clear, plain, distinct". Aristotle makes clarity the chief virtue of λέξις
|
|
50
|
+
(style); the other classical axis is ποικιλία, variety. The two metrics here are exactly those
|
|
51
|
+
axes: **LIX measures clarity, TTR measures variety.**
|
|
52
|
+
|
|
53
|
+
## Why this exists
|
|
54
|
+
|
|
55
|
+
`textstat`, `textdescriptives`, `lexicalrichness` and `taaled` already cover this ground. Two
|
|
56
|
+
reasons to still build it:
|
|
57
|
+
|
|
58
|
+
1. **The LIX long-word threshold is hardcoded at 6 everywhere.** That 6 comes from Björnsson's
|
|
59
|
+
Swedish original, and it is wrong for the languages we work on. Hungarian is agglutinative
|
|
60
|
+
and Ancient Greek heavily inflected, so at threshold 6 nearly every token counts as "long"
|
|
61
|
+
and the index saturates into a flat line. Measured over the full Hungarian Webcorpus,
|
|
62
|
+
**44.5% of running tokens are "long" at threshold 6**, against **25.7%** in Swedish. On
|
|
63
|
+
real Hungarian prose that pushes LIX to 60.4 — "very difficult" — where the calibrated
|
|
64
|
+
threshold gives 43.4. Parameterising the threshold is the whole point.
|
|
65
|
+
2. **Implementations disagree.** They count words, sentences and long words differently, so
|
|
66
|
+
they rank the same texts differently. So: expose the counts, document every choice, make
|
|
67
|
+
results auditable.
|
|
68
|
+
|
|
69
|
+
Non-goal: becoming another kitchen-sink readability library.
|
|
70
|
+
|
|
71
|
+
## Installation
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
uv add saphes
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The core has **no dependencies** — plain Python and the standard library.
|
|
78
|
+
|
|
79
|
+
## Quickstart
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from saphes import lix, lexical_diversity
|
|
83
|
+
|
|
84
|
+
# LIX takes SURFACE FORMS. Word length is the signal.
|
|
85
|
+
result = lix("The cat sat on it. Complicated sentences generally frighten us.")
|
|
86
|
+
result.score # 45.0
|
|
87
|
+
result.words, result.sentences, result.long_words # (10, 2, 4) -> A, B, C
|
|
88
|
+
result.band # 'standard'
|
|
89
|
+
|
|
90
|
+
# Raise the threshold for inflected languages, where 6 saturates.
|
|
91
|
+
hu = "A gyermekeknek megmutatták a településeken található nevezetességeket. Elutaztak."
|
|
92
|
+
lix(hu).score # 79.0 — an ordinary sentence, "very difficult"
|
|
93
|
+
lix(hu).long_word_share # 0.75 — three words in four are "long"
|
|
94
|
+
lix(hu, long_word_threshold=9).score # 54.0 — discrimination restored
|
|
95
|
+
lix(hu, long_word_threshold=9).long_word_share # 0.50
|
|
96
|
+
|
|
97
|
+
# Or use the calibrated default, with its provenance attached.
|
|
98
|
+
from saphes import recommended_threshold
|
|
99
|
+
rec = recommended_threshold("hu")
|
|
100
|
+
rec.threshold # 8
|
|
101
|
+
rec.matched_share # 0.273 — against Swedish 0.257 at threshold 6
|
|
102
|
+
lix(hu, long_word_threshold=int(rec))
|
|
103
|
+
|
|
104
|
+
# Diversity takes LEMMAS, and `unit` is required — no default.
|
|
105
|
+
lexical_diversity(["ház", "kutya", "ház"], unit="lemma")
|
|
106
|
+
|
|
107
|
+
# Comparing texts of different lengths? Use MATTR, not TTR.
|
|
108
|
+
lexical_diversity(lemmas, unit="lemma", window=100).mattr
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## The data contract
|
|
112
|
+
|
|
113
|
+
The single most important thing in this package: **the two metrics require opposite token
|
|
114
|
+
streams.**
|
|
115
|
+
|
|
116
|
+
| Metric | Required input | Why |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| `lexical_diversity` | **lemmas** | Surface variation is *noise* — it measures morphology, not vocabulary. Hungarian `ház / házak / házban / házakat` is four types and one lemma. |
|
|
119
|
+
| `lix` | **surface forms** | Word length *is* the signal. `házakban` is 8 characters; its lemma `ház` is 3. Lemmatising erases the measurement. |
|
|
120
|
+
|
|
121
|
+
Feed the same list to both and exactly one of them is silently wrong — no error, no NaN, just
|
|
122
|
+
a plausible number. Four guards exist against that:
|
|
123
|
+
|
|
124
|
+
- `unit` is a **required** keyword on `lexical_diversity`, with no default.
|
|
125
|
+
- The parameter names differ: `lemmas=` versus `words=`. Crossing them raises.
|
|
126
|
+
- A raw string is **refused** by `lexical_diversity` unless you pass `unit="surface"`, since a
|
|
127
|
+
string can only ever yield surface forms.
|
|
128
|
+
- Every result records the `unit` it measured, so any serialised table says which stream
|
|
129
|
+
produced each number.
|
|
130
|
+
|
|
131
|
+
**saphes consumes lemmas; it does not produce them.** Lemmatisation is language-specific and
|
|
132
|
+
heavy — CLTK or a treebank for Greek, huspacy for Hungarian. The caller lemmatises; saphes
|
|
133
|
+
measures.
|
|
134
|
+
|
|
135
|
+
## Features
|
|
136
|
+
|
|
137
|
+
- **`lix(...)`** — the readability index, with `long_word_threshold` as a first-class
|
|
138
|
+
parameter. A long word is `len(word) > threshold`, so the default 6 means **seven letters or
|
|
139
|
+
more**, per Björnsson's "more than six letters".
|
|
140
|
+
- **Three ways to supply the sentence count *B*** — segmented from raw text with a pluggable
|
|
141
|
+
splitter, pre-split, or an explicit integer. Neither a treebank that drops punctuation nor a
|
|
142
|
+
spaCy pipeline with the parser disabled can give you sentences, so the explicit path is not
|
|
143
|
+
a corner case. The result records which was used.
|
|
144
|
+
- **`lexical_diversity(...)`** — TTR and MATTR, with the token unit declared and recorded.
|
|
145
|
+
- **`mattr(tokens, window=100)`** — the length-corrected moving average, as a bare float, for
|
|
146
|
+
drop-in use.
|
|
147
|
+
- **Calibrated thresholds, with their provenance.** `recommended_threshold("hu")` returns the
|
|
148
|
+
empirically matched threshold *and* the shares behind it, the runner-up, which of six
|
|
149
|
+
independently computed curves agreed, and the caveats. The number ships; the corpus does
|
|
150
|
+
not. The study is in `experiments/lix_calibration/`.
|
|
151
|
+
- **Counts, not just scores.** Every function returns *A*, *B*, *C* (or types and tokens) plus
|
|
152
|
+
every parameter used and the saphes version. A bare float is unauditable.
|
|
153
|
+
- **Explicit length policy.** Decomposed Unicode otherwise inflates every word length, which
|
|
154
|
+
hits polytonic Greek and accented Hungarian hardest. `length_policy` defaults to NFC
|
|
155
|
+
normalisation and can be swapped for grapheme counting, raw code points, or your own
|
|
156
|
+
callable.
|
|
157
|
+
|
|
158
|
+
> [!WARNING]
|
|
159
|
+
> **TTR is inversely related to text length.** TTR values from texts of different lengths are
|
|
160
|
+
> **not comparable** — a raw TTR over corpora of different sizes mostly ranks them by size.
|
|
161
|
+
> This matters directly for per-decade and per-book work, where lengths always differ. Pass
|
|
162
|
+
> `window=` and read `.mattr` instead.
|
|
163
|
+
|
|
164
|
+
## Documentation
|
|
165
|
+
|
|
166
|
+
Full docs at [saphes.readthedocs.io](https://saphes.readthedocs.io); sources in `docs/`.
|
|
167
|
+
Worked examples live in `examples/` and are included in the docs verbatim, so they cannot
|
|
168
|
+
drift.
|
|
169
|
+
|
|
170
|
+
## Roadmap
|
|
171
|
+
|
|
172
|
+
- [x] LIX with a parameterised long-word threshold
|
|
173
|
+
- [x] TTR and MATTR with a required, recorded token unit
|
|
174
|
+
- [x] RIX (long words per sentence)
|
|
175
|
+
- [x] Empirically calibrated per-language thresholds, from token-weighted word-length
|
|
176
|
+
distributions — Hungarian ships as `recommended_threshold("hu")`
|
|
177
|
+
- [ ] The same study for Ancient Greek, for the Homer project
|
|
178
|
+
- [ ] POS-filtered diversity, once lemmas carry tags
|
|
179
|
+
- [ ] MTLD, HD-D, vocd-D, Maas
|
|
180
|
+
|
|
181
|
+
**Maintenance**
|
|
182
|
+
|
|
183
|
+
- [x] Logo and README banner
|
|
184
|
+
- [ ] Mutation-testing baseline
|
|
185
|
+
|
|
186
|
+
Explicitly out of scope: Flesch, Kincaid, SMOG and relatives. They need syllabification, which
|
|
187
|
+
is language-specific and a different project. LIX was chosen precisely because it needs only
|
|
188
|
+
word length and sentence count, so it travels across languages.
|
|
189
|
+
|
|
190
|
+
## Made by
|
|
191
|
+
|
|
192
|
+
saphes is made by [Crow Intelligence](https://crowintelligence.org/).
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
MIT
|
saphes-0.1.0/README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/crow-intelligence/saphes/main/img/saphes_logo.png" alt="saphes logo" width="480">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# saphes
|
|
6
|
+
|
|
7
|
+
Readability and lexical diversity — two metrics, done carefully, with the parameters other
|
|
8
|
+
implementations hardcode.
|
|
9
|
+
|
|
10
|
+
*saphes* — σαφής, "clear, plain, distinct". Aristotle makes clarity the chief virtue of λέξις
|
|
11
|
+
(style); the other classical axis is ποικιλία, variety. The two metrics here are exactly those
|
|
12
|
+
axes: **LIX measures clarity, TTR measures variety.**
|
|
13
|
+
|
|
14
|
+
## Why this exists
|
|
15
|
+
|
|
16
|
+
`textstat`, `textdescriptives`, `lexicalrichness` and `taaled` already cover this ground. Two
|
|
17
|
+
reasons to still build it:
|
|
18
|
+
|
|
19
|
+
1. **The LIX long-word threshold is hardcoded at 6 everywhere.** That 6 comes from Björnsson's
|
|
20
|
+
Swedish original, and it is wrong for the languages we work on. Hungarian is agglutinative
|
|
21
|
+
and Ancient Greek heavily inflected, so at threshold 6 nearly every token counts as "long"
|
|
22
|
+
and the index saturates into a flat line. Measured over the full Hungarian Webcorpus,
|
|
23
|
+
**44.5% of running tokens are "long" at threshold 6**, against **25.7%** in Swedish. On
|
|
24
|
+
real Hungarian prose that pushes LIX to 60.4 — "very difficult" — where the calibrated
|
|
25
|
+
threshold gives 43.4. Parameterising the threshold is the whole point.
|
|
26
|
+
2. **Implementations disagree.** They count words, sentences and long words differently, so
|
|
27
|
+
they rank the same texts differently. So: expose the counts, document every choice, make
|
|
28
|
+
results auditable.
|
|
29
|
+
|
|
30
|
+
Non-goal: becoming another kitchen-sink readability library.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
uv add saphes
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The core has **no dependencies** — plain Python and the standard library.
|
|
39
|
+
|
|
40
|
+
## Quickstart
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from saphes import lix, lexical_diversity
|
|
44
|
+
|
|
45
|
+
# LIX takes SURFACE FORMS. Word length is the signal.
|
|
46
|
+
result = lix("The cat sat on it. Complicated sentences generally frighten us.")
|
|
47
|
+
result.score # 45.0
|
|
48
|
+
result.words, result.sentences, result.long_words # (10, 2, 4) -> A, B, C
|
|
49
|
+
result.band # 'standard'
|
|
50
|
+
|
|
51
|
+
# Raise the threshold for inflected languages, where 6 saturates.
|
|
52
|
+
hu = "A gyermekeknek megmutatták a településeken található nevezetességeket. Elutaztak."
|
|
53
|
+
lix(hu).score # 79.0 — an ordinary sentence, "very difficult"
|
|
54
|
+
lix(hu).long_word_share # 0.75 — three words in four are "long"
|
|
55
|
+
lix(hu, long_word_threshold=9).score # 54.0 — discrimination restored
|
|
56
|
+
lix(hu, long_word_threshold=9).long_word_share # 0.50
|
|
57
|
+
|
|
58
|
+
# Or use the calibrated default, with its provenance attached.
|
|
59
|
+
from saphes import recommended_threshold
|
|
60
|
+
rec = recommended_threshold("hu")
|
|
61
|
+
rec.threshold # 8
|
|
62
|
+
rec.matched_share # 0.273 — against Swedish 0.257 at threshold 6
|
|
63
|
+
lix(hu, long_word_threshold=int(rec))
|
|
64
|
+
|
|
65
|
+
# Diversity takes LEMMAS, and `unit` is required — no default.
|
|
66
|
+
lexical_diversity(["ház", "kutya", "ház"], unit="lemma")
|
|
67
|
+
|
|
68
|
+
# Comparing texts of different lengths? Use MATTR, not TTR.
|
|
69
|
+
lexical_diversity(lemmas, unit="lemma", window=100).mattr
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## The data contract
|
|
73
|
+
|
|
74
|
+
The single most important thing in this package: **the two metrics require opposite token
|
|
75
|
+
streams.**
|
|
76
|
+
|
|
77
|
+
| Metric | Required input | Why |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| `lexical_diversity` | **lemmas** | Surface variation is *noise* — it measures morphology, not vocabulary. Hungarian `ház / házak / házban / házakat` is four types and one lemma. |
|
|
80
|
+
| `lix` | **surface forms** | Word length *is* the signal. `házakban` is 8 characters; its lemma `ház` is 3. Lemmatising erases the measurement. |
|
|
81
|
+
|
|
82
|
+
Feed the same list to both and exactly one of them is silently wrong — no error, no NaN, just
|
|
83
|
+
a plausible number. Four guards exist against that:
|
|
84
|
+
|
|
85
|
+
- `unit` is a **required** keyword on `lexical_diversity`, with no default.
|
|
86
|
+
- The parameter names differ: `lemmas=` versus `words=`. Crossing them raises.
|
|
87
|
+
- A raw string is **refused** by `lexical_diversity` unless you pass `unit="surface"`, since a
|
|
88
|
+
string can only ever yield surface forms.
|
|
89
|
+
- Every result records the `unit` it measured, so any serialised table says which stream
|
|
90
|
+
produced each number.
|
|
91
|
+
|
|
92
|
+
**saphes consumes lemmas; it does not produce them.** Lemmatisation is language-specific and
|
|
93
|
+
heavy — CLTK or a treebank for Greek, huspacy for Hungarian. The caller lemmatises; saphes
|
|
94
|
+
measures.
|
|
95
|
+
|
|
96
|
+
## Features
|
|
97
|
+
|
|
98
|
+
- **`lix(...)`** — the readability index, with `long_word_threshold` as a first-class
|
|
99
|
+
parameter. A long word is `len(word) > threshold`, so the default 6 means **seven letters or
|
|
100
|
+
more**, per Björnsson's "more than six letters".
|
|
101
|
+
- **Three ways to supply the sentence count *B*** — segmented from raw text with a pluggable
|
|
102
|
+
splitter, pre-split, or an explicit integer. Neither a treebank that drops punctuation nor a
|
|
103
|
+
spaCy pipeline with the parser disabled can give you sentences, so the explicit path is not
|
|
104
|
+
a corner case. The result records which was used.
|
|
105
|
+
- **`lexical_diversity(...)`** — TTR and MATTR, with the token unit declared and recorded.
|
|
106
|
+
- **`mattr(tokens, window=100)`** — the length-corrected moving average, as a bare float, for
|
|
107
|
+
drop-in use.
|
|
108
|
+
- **Calibrated thresholds, with their provenance.** `recommended_threshold("hu")` returns the
|
|
109
|
+
empirically matched threshold *and* the shares behind it, the runner-up, which of six
|
|
110
|
+
independently computed curves agreed, and the caveats. The number ships; the corpus does
|
|
111
|
+
not. The study is in `experiments/lix_calibration/`.
|
|
112
|
+
- **Counts, not just scores.** Every function returns *A*, *B*, *C* (or types and tokens) plus
|
|
113
|
+
every parameter used and the saphes version. A bare float is unauditable.
|
|
114
|
+
- **Explicit length policy.** Decomposed Unicode otherwise inflates every word length, which
|
|
115
|
+
hits polytonic Greek and accented Hungarian hardest. `length_policy` defaults to NFC
|
|
116
|
+
normalisation and can be swapped for grapheme counting, raw code points, or your own
|
|
117
|
+
callable.
|
|
118
|
+
|
|
119
|
+
> [!WARNING]
|
|
120
|
+
> **TTR is inversely related to text length.** TTR values from texts of different lengths are
|
|
121
|
+
> **not comparable** — a raw TTR over corpora of different sizes mostly ranks them by size.
|
|
122
|
+
> This matters directly for per-decade and per-book work, where lengths always differ. Pass
|
|
123
|
+
> `window=` and read `.mattr` instead.
|
|
124
|
+
|
|
125
|
+
## Documentation
|
|
126
|
+
|
|
127
|
+
Full docs at [saphes.readthedocs.io](https://saphes.readthedocs.io); sources in `docs/`.
|
|
128
|
+
Worked examples live in `examples/` and are included in the docs verbatim, so they cannot
|
|
129
|
+
drift.
|
|
130
|
+
|
|
131
|
+
## Roadmap
|
|
132
|
+
|
|
133
|
+
- [x] LIX with a parameterised long-word threshold
|
|
134
|
+
- [x] TTR and MATTR with a required, recorded token unit
|
|
135
|
+
- [x] RIX (long words per sentence)
|
|
136
|
+
- [x] Empirically calibrated per-language thresholds, from token-weighted word-length
|
|
137
|
+
distributions — Hungarian ships as `recommended_threshold("hu")`
|
|
138
|
+
- [ ] The same study for Ancient Greek, for the Homer project
|
|
139
|
+
- [ ] POS-filtered diversity, once lemmas carry tags
|
|
140
|
+
- [ ] MTLD, HD-D, vocd-D, Maas
|
|
141
|
+
|
|
142
|
+
**Maintenance**
|
|
143
|
+
|
|
144
|
+
- [x] Logo and README banner
|
|
145
|
+
- [ ] Mutation-testing baseline
|
|
146
|
+
|
|
147
|
+
Explicitly out of scope: Flesch, Kincaid, SMOG and relatives. They need syllabification, which
|
|
148
|
+
is language-specific and a different project. LIX was chosen precisely because it needs only
|
|
149
|
+
word length and sentence count, so it travels across languages.
|
|
150
|
+
|
|
151
|
+
## Made by
|
|
152
|
+
|
|
153
|
+
saphes is made by [Crow Intelligence](https://crowintelligence.org/).
|
|
154
|
+
|
|
155
|
+
## License
|
|
156
|
+
|
|
157
|
+
MIT
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "saphes"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Readability (LIX) and lexical diversity (TTR/MATTR) with the parameters other implementations hardcode"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
keywords = [
|
|
10
|
+
"nlp",
|
|
11
|
+
"readability",
|
|
12
|
+
"lix",
|
|
13
|
+
"lexical-diversity",
|
|
14
|
+
"type-token-ratio",
|
|
15
|
+
"mattr",
|
|
16
|
+
"corpus-linguistics",
|
|
17
|
+
"computational-humanities",
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 3 - Alpha",
|
|
21
|
+
"Intended Audience :: Science/Research",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Information Analysis",
|
|
27
|
+
"Topic :: Text Processing :: Linguistic",
|
|
28
|
+
]
|
|
29
|
+
dependencies = []
|
|
30
|
+
|
|
31
|
+
[[project.authors]]
|
|
32
|
+
name = "Zoltan Varju"
|
|
33
|
+
email = "zoltan.varju@crowintelligence.org"
|
|
34
|
+
|
|
35
|
+
[[project.authors]]
|
|
36
|
+
name = "Orsolya Putz"
|
|
37
|
+
email = "orsolya.putz@crowintelligence.org"
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://crowintelligence.org/"
|
|
41
|
+
Repository = "https://github.com/crow-intelligence/saphes"
|
|
42
|
+
Documentation = "https://saphes.readthedocs.io"
|
|
43
|
+
|
|
44
|
+
[project.optional-dependencies]
|
|
45
|
+
punkt = ["nltk>=3.8"]
|
|
46
|
+
dev = [
|
|
47
|
+
"pytest>=8",
|
|
48
|
+
"pytest-cov",
|
|
49
|
+
"hypothesis>=6.100",
|
|
50
|
+
"ruff>=0.4",
|
|
51
|
+
"ty",
|
|
52
|
+
"mutmut>=3",
|
|
53
|
+
"textstat>=0.7",
|
|
54
|
+
"matplotlib>=3.8",
|
|
55
|
+
"jupyter>=1.0",
|
|
56
|
+
"ipykernel>=6.29",
|
|
57
|
+
]
|
|
58
|
+
docs = [
|
|
59
|
+
"mkdocs-material",
|
|
60
|
+
"mkdocstrings[python]",
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
[build-system]
|
|
64
|
+
requires = ["uv_build>=0.9.22,<0.10.0"]
|
|
65
|
+
build-backend = "uv_build"
|
|
66
|
+
|
|
67
|
+
[tool.ruff]
|
|
68
|
+
line-length = 88
|
|
69
|
+
target-version = "py311"
|
|
70
|
+
|
|
71
|
+
[tool.ruff.lint]
|
|
72
|
+
select = [
|
|
73
|
+
"E",
|
|
74
|
+
"F",
|
|
75
|
+
"I",
|
|
76
|
+
"N",
|
|
77
|
+
"UP",
|
|
78
|
+
"ANN",
|
|
79
|
+
"D",
|
|
80
|
+
]
|
|
81
|
+
ignore = [
|
|
82
|
+
"D105",
|
|
83
|
+
"D107",
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
[tool.ruff.lint.per-file-ignores]
|
|
87
|
+
"tests/**" = [
|
|
88
|
+
"D100",
|
|
89
|
+
"D102",
|
|
90
|
+
"D103",
|
|
91
|
+
"D104",
|
|
92
|
+
"ANN",
|
|
93
|
+
]
|
|
94
|
+
"examples/**" = [
|
|
95
|
+
"D100",
|
|
96
|
+
"ANN",
|
|
97
|
+
]
|
|
98
|
+
"experiments/**" = [
|
|
99
|
+
"D100",
|
|
100
|
+
"D103",
|
|
101
|
+
"ANN",
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
[tool.ruff.lint.pydocstyle]
|
|
105
|
+
convention = "google"
|
|
106
|
+
|
|
107
|
+
[tool.pytest.ini_options]
|
|
108
|
+
addopts = "--doctest-modules --tb=short"
|
|
109
|
+
testpaths = [
|
|
110
|
+
"src",
|
|
111
|
+
"tests",
|
|
112
|
+
]
|
|
113
|
+
|
|
114
|
+
[tool.mutmut]
|
|
115
|
+
source_paths = ["src/saphes"]
|
|
116
|
+
only_mutate = [
|
|
117
|
+
"src/saphes/readability.py",
|
|
118
|
+
"src/saphes/diversity.py",
|
|
119
|
+
"src/saphes/segment.py",
|
|
120
|
+
]
|
|
121
|
+
pytest_add_cli_args_test_selection = [
|
|
122
|
+
"tests/test_readability.py",
|
|
123
|
+
"tests/test_diversity.py",
|
|
124
|
+
"tests/test_segment.py",
|
|
125
|
+
"tests/test_contracts.py",
|
|
126
|
+
]
|
|
127
|
+
pytest_add_cli_args = [
|
|
128
|
+
"-p",
|
|
129
|
+
"no:cacheprovider",
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
[tool.ty]
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "saphes"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Readability (LIX) and lexical diversity (TTR/MATTR) with the parameters other implementations hardcode"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Zoltan Varju", email = "zoltan.varju@crowintelligence.org" },
|
|
8
|
+
{ name = "Orsolya Putz", email = "orsolya.putz@crowintelligence.org" },
|
|
9
|
+
]
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
keywords = [
|
|
14
|
+
"nlp",
|
|
15
|
+
"readability",
|
|
16
|
+
"lix",
|
|
17
|
+
"lexical-diversity",
|
|
18
|
+
"type-token-ratio",
|
|
19
|
+
"mattr",
|
|
20
|
+
"corpus-linguistics",
|
|
21
|
+
"computational-humanities",
|
|
22
|
+
]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 3 - Alpha",
|
|
25
|
+
"Intended Audience :: Science/Research",
|
|
26
|
+
# No "License ::" classifier: deprecated by PEP 639 in favour of the
|
|
27
|
+
# `license` SPDX expression above.
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3.11",
|
|
30
|
+
"Programming Language :: Python :: 3.12",
|
|
31
|
+
"Programming Language :: Python :: 3.13",
|
|
32
|
+
"Topic :: Scientific/Engineering :: Information Analysis",
|
|
33
|
+
"Topic :: Text Processing :: Linguistic",
|
|
34
|
+
]
|
|
35
|
+
# The core is deliberately dependency-free: plain Python and the standard
|
|
36
|
+
# library only. Lemmatisation, tagging, and heavyweight sentence splitting are
|
|
37
|
+
# the caller's job — saphes measures what it is given.
|
|
38
|
+
dependencies = []
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://crowintelligence.org/"
|
|
42
|
+
Repository = "https://github.com/crow-intelligence/saphes"
|
|
43
|
+
Documentation = "https://saphes.readthedocs.io"
|
|
44
|
+
|
|
45
|
+
[project.optional-dependencies]
|
|
46
|
+
# Optional NLTK Punkt sentence splitter for `segment.sentences(..., punkt=True)`.
|
|
47
|
+
# The core never imports it; the bundled regex splitter is the default.
|
|
48
|
+
punkt = [
|
|
49
|
+
"nltk>=3.8",
|
|
50
|
+
]
|
|
51
|
+
dev = [
|
|
52
|
+
"pytest>=8",
|
|
53
|
+
"pytest-cov",
|
|
54
|
+
"hypothesis>=6.100",
|
|
55
|
+
"ruff>=0.4",
|
|
56
|
+
"ty",
|
|
57
|
+
"mutmut>=3",
|
|
58
|
+
# Reference implementation for the LIX cross-check. Pulls in nltk.
|
|
59
|
+
"textstat>=0.7",
|
|
60
|
+
# experiment deps -- not part of the published package
|
|
61
|
+
"matplotlib>=3.8",
|
|
62
|
+
"jupyter>=1.0",
|
|
63
|
+
"ipykernel>=6.29",
|
|
64
|
+
]
|
|
65
|
+
docs = [
|
|
66
|
+
"mkdocs-material",
|
|
67
|
+
"mkdocstrings[python]",
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
[build-system]
|
|
71
|
+
requires = ["uv_build>=0.9.22,<0.10.0"]
|
|
72
|
+
build-backend = "uv_build"
|
|
73
|
+
|
|
74
|
+
[tool.ruff]
|
|
75
|
+
line-length = 88
|
|
76
|
+
target-version = "py311"
|
|
77
|
+
|
|
78
|
+
[tool.ruff.lint]
|
|
79
|
+
select = ["E", "F", "I", "N", "UP", "ANN", "D"]
|
|
80
|
+
ignore = ["D105", "D107"]
|
|
81
|
+
|
|
82
|
+
[tool.ruff.lint.per-file-ignores]
|
|
83
|
+
"tests/**" = ["D100", "D102", "D103", "D104", "ANN"]
|
|
84
|
+
"examples/**" = ["D100", "ANN"]
|
|
85
|
+
"experiments/**" = ["D100", "D103", "ANN"]
|
|
86
|
+
|
|
87
|
+
[tool.ruff.lint.pydocstyle]
|
|
88
|
+
convention = "google"
|
|
89
|
+
|
|
90
|
+
[tool.pytest.ini_options]
|
|
91
|
+
addopts = "--doctest-modules --tb=short"
|
|
92
|
+
testpaths = ["src", "tests"]
|
|
93
|
+
|
|
94
|
+
# Mutation testing (dev-only, not run in CI). Scoped to the arithmetic core and
|
|
95
|
+
# its fast test files. Run with `SAPHES_MUTATION=1 uv run mutmut run`.
|
|
96
|
+
[tool.mutmut]
|
|
97
|
+
source_paths = ["src/saphes"]
|
|
98
|
+
only_mutate = [
|
|
99
|
+
"src/saphes/readability.py",
|
|
100
|
+
"src/saphes/diversity.py",
|
|
101
|
+
"src/saphes/segment.py",
|
|
102
|
+
]
|
|
103
|
+
pytest_add_cli_args_test_selection = [
|
|
104
|
+
"tests/test_readability.py",
|
|
105
|
+
"tests/test_diversity.py",
|
|
106
|
+
"tests/test_segment.py",
|
|
107
|
+
"tests/test_contracts.py",
|
|
108
|
+
]
|
|
109
|
+
pytest_add_cli_args = ["-p", "no:cacheprovider"]
|
|
110
|
+
|
|
111
|
+
[tool.ty]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Saphes — readability (LIX) and lexical diversity (TTR/MATTR), made auditable.
|
|
2
|
+
|
|
3
|
+
Two metrics, done carefully, with the parameters other implementations hardcode.
|
|
4
|
+
|
|
5
|
+
The one thing to get right: **the two metrics need opposite token streams.**
|
|
6
|
+
:func:`~saphes.diversity.lexical_diversity` wants lemmas, because surface
|
|
7
|
+
variation is morphology rather than vocabulary.
|
|
8
|
+
:func:`~saphes.readability.lix` requires surface forms, because word length is
|
|
9
|
+
its signal and lemmatising erases it. Feed one stream to both and exactly one of
|
|
10
|
+
them is silently wrong.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
__version__ = version("saphes")
|
|
17
|
+
except PackageNotFoundError: # pragma: no cover - only when running uninstalled
|
|
18
|
+
__version__ = "0.0.0+unknown"
|
|
19
|
+
|
|
20
|
+
from saphes.calibration import ( # noqa: E402 - __version__ must precede these
|
|
21
|
+
ThresholdRecommendation,
|
|
22
|
+
hungarian_letter_count,
|
|
23
|
+
recommended_threshold,
|
|
24
|
+
)
|
|
25
|
+
from saphes.diversity import ( # noqa: E402 - __version__ must precede these
|
|
26
|
+
DiversityResult,
|
|
27
|
+
lexical_diversity,
|
|
28
|
+
mattr,
|
|
29
|
+
ttr_from_counts,
|
|
30
|
+
)
|
|
31
|
+
from saphes.readability import ( # noqa: E402 - __version__ must precede these
|
|
32
|
+
LIX_BANDS,
|
|
33
|
+
LixResult,
|
|
34
|
+
interpret_lix,
|
|
35
|
+
lix,
|
|
36
|
+
lix_from_counts,
|
|
37
|
+
rix,
|
|
38
|
+
word_length,
|
|
39
|
+
)
|
|
40
|
+
from saphes.segment import sentences, words # noqa: E402 - as above
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"LIX_BANDS",
|
|
44
|
+
"DiversityResult",
|
|
45
|
+
"LixResult",
|
|
46
|
+
"ThresholdRecommendation",
|
|
47
|
+
"__version__",
|
|
48
|
+
"hungarian_letter_count",
|
|
49
|
+
"interpret_lix",
|
|
50
|
+
"lexical_diversity",
|
|
51
|
+
"lix",
|
|
52
|
+
"lix_from_counts",
|
|
53
|
+
"mattr",
|
|
54
|
+
"recommended_threshold",
|
|
55
|
+
"rix",
|
|
56
|
+
"sentences",
|
|
57
|
+
"ttr_from_counts",
|
|
58
|
+
"word_length",
|
|
59
|
+
"words",
|
|
60
|
+
]
|