svlang 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.
- svlang-0.1.0/.gitignore +7 -0
- svlang-0.1.0/LICENSE +21 -0
- svlang-0.1.0/PKG-INFO +130 -0
- svlang-0.1.0/README.md +105 -0
- svlang-0.1.0/pyproject.toml +39 -0
- svlang-0.1.0/setup.cfg +4 -0
- svlang-0.1.0/src/svlang/__init__.py +3 -0
- svlang-0.1.0/src/svlang/checkers/__init__.py +9 -0
- svlang-0.1.0/src/svlang/checkers/compound.py +167 -0
- svlang-0.1.0/src/svlang/checkers/consistency.py +75 -0
- svlang-0.1.0/src/svlang/checkers/lexicon.py +74 -0
- svlang-0.1.0/src/svlang/checkers/readability.py +72 -0
- svlang-0.1.0/src/svlang/checkers/svengelska.py +229 -0
- svlang-0.1.0/src/svlang/cli.py +202 -0
- svlang-0.1.0/src/svlang/data/__init__.py +0 -0
- svlang-0.1.0/src/svlang/data/folkets_sv_en.tsv +36331 -0
- svlang-0.1.0/src/svlang/data/sv_wordlist.txt +148343 -0
- svlang-0.1.0/src/svlang.egg-info/PKG-INFO +130 -0
- svlang-0.1.0/src/svlang.egg-info/SOURCES.txt +26 -0
- svlang-0.1.0/src/svlang.egg-info/dependency_links.txt +1 -0
- svlang-0.1.0/src/svlang.egg-info/entry_points.txt +2 -0
- svlang-0.1.0/src/svlang.egg-info/requires.txt +7 -0
- svlang-0.1.0/src/svlang.egg-info/top_level.txt +1 -0
- svlang-0.1.0/tests/test_compound.py +41 -0
- svlang-0.1.0/tests/test_consistency.py +43 -0
- svlang-0.1.0/tests/test_lexicon.py +41 -0
- svlang-0.1.0/tests/test_readability.py +61 -0
- svlang-0.1.0/tests/test_svengelska.py +64 -0
svlang-0.1.0/.gitignore
ADDED
svlang-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Nylander
|
|
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.
|
svlang-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: svlang
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Swedish NLP toolkit for translators
|
|
5
|
+
Author-email: Daniel Nylander <daniel@danielnylander.se>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yeager/svlang
|
|
8
|
+
Project-URL: Issues, https://github.com/yeager/svlang/issues
|
|
9
|
+
Keywords: swedish,nlp,translation,localization,i18n,l10n
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Natural Language :: Swedish
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: po
|
|
20
|
+
Requires-Dist: polib>=1.2; extra == "po"
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
23
|
+
Requires-Dist: polib>=1.2; extra == "dev"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# 🇸🇪 svlang
|
|
27
|
+
|
|
28
|
+
**Swedish NLP toolkit for translators.**
|
|
29
|
+
|
|
30
|
+
Detect anglicisms, check translation consistency, and split compound words — all from the command line or Python.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install svlang
|
|
36
|
+
|
|
37
|
+
# With .po file support
|
|
38
|
+
pip install svlang[po]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## CLI Usage
|
|
42
|
+
|
|
43
|
+
### Svengelska detector
|
|
44
|
+
|
|
45
|
+
Find unnecessary anglicisms in Swedish text:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
$ svlang svengelska -t "Vi behöver implementera en ny approach"
|
|
49
|
+
⚠️ «implementera» → genomföra, införa
|
|
50
|
+
⚠️ «approach» → tillvägagångssätt, metod
|
|
51
|
+
|
|
52
|
+
$ svlang svengelska -f my_translation.txt
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Consistency checker
|
|
56
|
+
|
|
57
|
+
Find identical source strings with different translations:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
$ svlang consistency file1.po file2.po
|
|
61
|
+
⚠️ 2 inkonsistens(er) hittade:
|
|
62
|
+
|
|
63
|
+
Källa: «Save»
|
|
64
|
+
→ «Spara» (file1.po:12)
|
|
65
|
+
→ «Lagra» (file2.po:45)
|
|
66
|
+
|
|
67
|
+
# Also supports Qt .ts files
|
|
68
|
+
$ svlang consistency translations/*.ts
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Compound splitter
|
|
72
|
+
|
|
73
|
+
Split Swedish compound words into components:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
$ svlang compound barnbok solstol nattljus
|
|
77
|
+
barnbok → barn + bok
|
|
78
|
+
solstol → sol + stol
|
|
79
|
+
nattljus → natt + ljus
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Python API
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from svlang.checkers import SvengelskaChecker, ConsistencyChecker, CompoundSplitter
|
|
86
|
+
|
|
87
|
+
# Anglicisms
|
|
88
|
+
checker = SvengelskaChecker()
|
|
89
|
+
hits = checker.check("Ge mig lite feedback")
|
|
90
|
+
for h in hits:
|
|
91
|
+
print(f"{h.word} → {h.suggestion}")
|
|
92
|
+
|
|
93
|
+
# Consistency
|
|
94
|
+
con = ConsistencyChecker()
|
|
95
|
+
con.add("Save", "Spara", "file1.po:12")
|
|
96
|
+
con.add("Save", "Lagra", "file2.po:45")
|
|
97
|
+
for issue in con.check():
|
|
98
|
+
print(f"{issue.source}: {list(issue.translations.keys())}")
|
|
99
|
+
|
|
100
|
+
# Compound words
|
|
101
|
+
splitter = CompoundSplitter()
|
|
102
|
+
result = splitter.split("barnvagnshjul")
|
|
103
|
+
print(result.parts) # ["barn", "vagn", "hjul"]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Features
|
|
107
|
+
|
|
108
|
+
- 🔍 **90+ anglicisms** detected with Swedish alternatives (based on Språkrådet)
|
|
109
|
+
- 📊 **Consistency checking** across .po and .ts files
|
|
110
|
+
- 🧩 **Compound word splitting** with Swedish morphology (fogmorfem)
|
|
111
|
+
- 🖥️ **CLI** with colored output
|
|
112
|
+
- 📦 **Zero dependencies** (polib optional for .po files)
|
|
113
|
+
- 🐍 **Python 3.10+**
|
|
114
|
+
|
|
115
|
+
## Roadmap
|
|
116
|
+
|
|
117
|
+
- [ ] Spelling checker (Hunspell integration)
|
|
118
|
+
- [ ] Word frequency analysis
|
|
119
|
+
- [ ] Terminology extraction
|
|
120
|
+
- [ ] Gender-neutral language suggestions
|
|
121
|
+
- [ ] GitHub Action for CI validation
|
|
122
|
+
- [ ] LinguaEdit plugin
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT
|
|
127
|
+
|
|
128
|
+
## Author
|
|
129
|
+
|
|
130
|
+
**Daniel Nylander** — [danielnylander.se](https://www.danielnylander.se)
|
svlang-0.1.0/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# 🇸🇪 svlang
|
|
2
|
+
|
|
3
|
+
**Swedish NLP toolkit for translators.**
|
|
4
|
+
|
|
5
|
+
Detect anglicisms, check translation consistency, and split compound words — all from the command line or Python.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install svlang
|
|
11
|
+
|
|
12
|
+
# With .po file support
|
|
13
|
+
pip install svlang[po]
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## CLI Usage
|
|
17
|
+
|
|
18
|
+
### Svengelska detector
|
|
19
|
+
|
|
20
|
+
Find unnecessary anglicisms in Swedish text:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
$ svlang svengelska -t "Vi behöver implementera en ny approach"
|
|
24
|
+
⚠️ «implementera» → genomföra, införa
|
|
25
|
+
⚠️ «approach» → tillvägagångssätt, metod
|
|
26
|
+
|
|
27
|
+
$ svlang svengelska -f my_translation.txt
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Consistency checker
|
|
31
|
+
|
|
32
|
+
Find identical source strings with different translations:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
$ svlang consistency file1.po file2.po
|
|
36
|
+
⚠️ 2 inkonsistens(er) hittade:
|
|
37
|
+
|
|
38
|
+
Källa: «Save»
|
|
39
|
+
→ «Spara» (file1.po:12)
|
|
40
|
+
→ «Lagra» (file2.po:45)
|
|
41
|
+
|
|
42
|
+
# Also supports Qt .ts files
|
|
43
|
+
$ svlang consistency translations/*.ts
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Compound splitter
|
|
47
|
+
|
|
48
|
+
Split Swedish compound words into components:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
$ svlang compound barnbok solstol nattljus
|
|
52
|
+
barnbok → barn + bok
|
|
53
|
+
solstol → sol + stol
|
|
54
|
+
nattljus → natt + ljus
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Python API
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from svlang.checkers import SvengelskaChecker, ConsistencyChecker, CompoundSplitter
|
|
61
|
+
|
|
62
|
+
# Anglicisms
|
|
63
|
+
checker = SvengelskaChecker()
|
|
64
|
+
hits = checker.check("Ge mig lite feedback")
|
|
65
|
+
for h in hits:
|
|
66
|
+
print(f"{h.word} → {h.suggestion}")
|
|
67
|
+
|
|
68
|
+
# Consistency
|
|
69
|
+
con = ConsistencyChecker()
|
|
70
|
+
con.add("Save", "Spara", "file1.po:12")
|
|
71
|
+
con.add("Save", "Lagra", "file2.po:45")
|
|
72
|
+
for issue in con.check():
|
|
73
|
+
print(f"{issue.source}: {list(issue.translations.keys())}")
|
|
74
|
+
|
|
75
|
+
# Compound words
|
|
76
|
+
splitter = CompoundSplitter()
|
|
77
|
+
result = splitter.split("barnvagnshjul")
|
|
78
|
+
print(result.parts) # ["barn", "vagn", "hjul"]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Features
|
|
82
|
+
|
|
83
|
+
- 🔍 **90+ anglicisms** detected with Swedish alternatives (based on Språkrådet)
|
|
84
|
+
- 📊 **Consistency checking** across .po and .ts files
|
|
85
|
+
- 🧩 **Compound word splitting** with Swedish morphology (fogmorfem)
|
|
86
|
+
- 🖥️ **CLI** with colored output
|
|
87
|
+
- 📦 **Zero dependencies** (polib optional for .po files)
|
|
88
|
+
- 🐍 **Python 3.10+**
|
|
89
|
+
|
|
90
|
+
## Roadmap
|
|
91
|
+
|
|
92
|
+
- [ ] Spelling checker (Hunspell integration)
|
|
93
|
+
- [ ] Word frequency analysis
|
|
94
|
+
- [ ] Terminology extraction
|
|
95
|
+
- [ ] Gender-neutral language suggestions
|
|
96
|
+
- [ ] GitHub Action for CI validation
|
|
97
|
+
- [ ] LinguaEdit plugin
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
MIT
|
|
102
|
+
|
|
103
|
+
## Author
|
|
104
|
+
|
|
105
|
+
**Daniel Nylander** — [danielnylander.se](https://www.danielnylander.se)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "setuptools-scm"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "svlang"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Swedish NLP toolkit for translators"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{name = "Daniel Nylander", email = "daniel@danielnylander.se"}]
|
|
13
|
+
keywords = ["swedish", "nlp", "translation", "localization", "i18n", "l10n"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Natural Language :: Swedish",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Topic :: Text Processing :: Linguistic",
|
|
21
|
+
]
|
|
22
|
+
dependencies = []
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
po = ["polib>=1.2"]
|
|
26
|
+
dev = ["pytest>=7", "polib>=1.2"]
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
svlang = "svlang.cli:main"
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://github.com/yeager/svlang"
|
|
33
|
+
Issues = "https://github.com/yeager/svlang/issues"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
where = ["src"]
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.package-data]
|
|
39
|
+
svlang = ["data/*.txt", "data/*.tsv"]
|
svlang-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""Built-in checkers."""
|
|
2
|
+
|
|
3
|
+
from svlang.checkers.consistency import ConsistencyChecker
|
|
4
|
+
from svlang.checkers.svengelska import SvengelskaChecker
|
|
5
|
+
from svlang.checkers.compound import CompoundSplitter
|
|
6
|
+
from svlang.checkers.lexicon import SwedishLexicon
|
|
7
|
+
from svlang.checkers.readability import LixCalculator
|
|
8
|
+
|
|
9
|
+
__all__ = ["ConsistencyChecker", "SvengelskaChecker", "CompoundSplitter", "SwedishLexicon", "LixCalculator"]
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""Swedish compound word splitter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class CompoundSplit:
|
|
11
|
+
"""Result of splitting a compound word."""
|
|
12
|
+
word: str
|
|
13
|
+
parts: list[str]
|
|
14
|
+
is_compound: bool
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CompoundSplitter:
|
|
18
|
+
"""Split Swedish compound words into components.
|
|
19
|
+
|
|
20
|
+
Uses a dictionary-based approach: tries to find the longest prefix
|
|
21
|
+
that's a known word, then recursively splits the remainder.
|
|
22
|
+
|
|
23
|
+
Usage:
|
|
24
|
+
splitter = CompoundSplitter()
|
|
25
|
+
result = splitter.split("barnvagnshjul")
|
|
26
|
+
# → CompoundSplit(word="barnvagnshjul", parts=["barn", "vagn", "hjul"], is_compound=True)
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
# Common compound join letters (fogmorfem)
|
|
30
|
+
_JOINERS = ("s", "e", "o", "u", "")
|
|
31
|
+
|
|
32
|
+
def __init__(self, wordlist: set[str] | None = None):
|
|
33
|
+
if wordlist is not None:
|
|
34
|
+
self._words = wordlist
|
|
35
|
+
else:
|
|
36
|
+
self._words = self._load_default_wordlist()
|
|
37
|
+
self._min_part_len = 2
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def _load_default_wordlist() -> set[str]:
|
|
41
|
+
"""Load a basic Swedish wordlist."""
|
|
42
|
+
wl_path = Path(__file__).parent.parent / "data" / "sv_wordlist.txt"
|
|
43
|
+
if wl_path.exists():
|
|
44
|
+
return {
|
|
45
|
+
line.strip().lower()
|
|
46
|
+
for line in wl_path.read_text(encoding="utf-8").splitlines()
|
|
47
|
+
if line.strip() and not line.startswith("#")
|
|
48
|
+
}
|
|
49
|
+
# Fallback: minimal built-in set of common Swedish words
|
|
50
|
+
return _BUILTIN_WORDS
|
|
51
|
+
|
|
52
|
+
def split(self, word: str) -> CompoundSplit:
|
|
53
|
+
"""Try to split a compound word into parts."""
|
|
54
|
+
if not word or len(word) < 2:
|
|
55
|
+
return CompoundSplit(word=word, parts=[word] if word else [], is_compound=False)
|
|
56
|
+
word_lower = word.lower()
|
|
57
|
+
parts = self._try_split(word_lower)
|
|
58
|
+
if parts and len(parts) > 1:
|
|
59
|
+
return CompoundSplit(word=word, parts=parts, is_compound=True)
|
|
60
|
+
return CompoundSplit(word=word, parts=[word], is_compound=False)
|
|
61
|
+
|
|
62
|
+
def _try_split(self, word: str, depth: int = 0) -> list[str] | None:
|
|
63
|
+
"""Recursive compound splitting."""
|
|
64
|
+
if depth > 5:
|
|
65
|
+
return None
|
|
66
|
+
if len(word) < self._min_part_len * 2:
|
|
67
|
+
if word in self._words:
|
|
68
|
+
return [word]
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
# Try splitting at every position — longest prefix first
|
|
72
|
+
# This prefers "sjukhus+byggnad" over "sjuk+husbyggnad"
|
|
73
|
+
best: list[str] | None = None
|
|
74
|
+
for i in range(len(word) - self._min_part_len, self._min_part_len - 1, -1):
|
|
75
|
+
prefix = word[:i]
|
|
76
|
+
if prefix not in self._words:
|
|
77
|
+
continue
|
|
78
|
+
|
|
79
|
+
remainder = word[i:]
|
|
80
|
+
|
|
81
|
+
# Try with and without joiners
|
|
82
|
+
for joiner in self._JOINERS:
|
|
83
|
+
if joiner and remainder.startswith(joiner):
|
|
84
|
+
rest = remainder[len(joiner):]
|
|
85
|
+
else:
|
|
86
|
+
if joiner:
|
|
87
|
+
continue
|
|
88
|
+
rest = remainder
|
|
89
|
+
|
|
90
|
+
if not rest:
|
|
91
|
+
continue
|
|
92
|
+
|
|
93
|
+
# Is the rest a word?
|
|
94
|
+
if rest in self._words:
|
|
95
|
+
candidate = [prefix, rest]
|
|
96
|
+
# Prefer fewest parts, then longest first part
|
|
97
|
+
if best is None or len(candidate) < len(best):
|
|
98
|
+
best = candidate
|
|
99
|
+
continue
|
|
100
|
+
|
|
101
|
+
# Try splitting the rest recursively
|
|
102
|
+
sub = self._try_split(rest, depth + 1)
|
|
103
|
+
if sub:
|
|
104
|
+
candidate = [prefix] + sub
|
|
105
|
+
if best is None or len(candidate) < len(best):
|
|
106
|
+
best = candidate
|
|
107
|
+
|
|
108
|
+
# If we found a 2-part split with longest prefix, that's optimal
|
|
109
|
+
if best and len(best) == 2:
|
|
110
|
+
return best
|
|
111
|
+
|
|
112
|
+
return best
|
|
113
|
+
|
|
114
|
+
def is_valid_compound(self, word: str) -> bool:
|
|
115
|
+
"""Check if a word is a valid compound."""
|
|
116
|
+
return self.split(word).is_compound
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
# Basic Swedish words for fallback (no external file needed)
|
|
120
|
+
_BUILTIN_WORDS = {
|
|
121
|
+
# Common nouns
|
|
122
|
+
"barn", "bil", "bok", "bord", "brev", "bro", "dag", "dator", "del",
|
|
123
|
+
"djur", "dörr", "fisk", "flyg", "folk", "fot", "färg", "förening",
|
|
124
|
+
"gata", "glas", "golv", "gård", "hand", "hem", "hjul", "hund", "hus",
|
|
125
|
+
"hår", "jord", "katt", "klass", "klocka", "konst", "kraft", "kropp",
|
|
126
|
+
"kung", "kvinna", "källa", "lag", "land", "ljus", "luft", "lägenhet",
|
|
127
|
+
"man", "mat", "mark", "mor", "musik", "natt", "namn", "nyckel",
|
|
128
|
+
"ord", "papper", "plats", "polis", "program", "rum", "rätt",
|
|
129
|
+
"sjö", "skog", "skola", "sol", "stad", "stol", "ström", "system",
|
|
130
|
+
"tak", "tid", "trafik", "trä", "vagn", "vakt", "vatten", "vin",
|
|
131
|
+
"vind", "väg", "vägg", "värld", "växt", "yta", "öga", "öra",
|
|
132
|
+
# Buildings & places
|
|
133
|
+
"bana", "bank", "butik", "fabrik", "hus", "kyrka", "plan",
|
|
134
|
+
"sjukhus", "station", "torget", "tunnel",
|
|
135
|
+
# Compound-common nouns
|
|
136
|
+
"boll", "brand", "bruk", "by", "båt", "flod", "flyg", "hamn",
|
|
137
|
+
"hyra", "is", "järn", "krig", "kust", "köp", "leda", "mot",
|
|
138
|
+
"park", "resa", "ring", "sand", "sjuk", "snö", "sång", "torg",
|
|
139
|
+
"tåg", "vapen", "vård",
|
|
140
|
+
# Sports & activities
|
|
141
|
+
"fotboll", "handboll", "ishockey", "tennis",
|
|
142
|
+
# Government & society
|
|
143
|
+
"riksdag", "kommun", "ledamot", "minister", "samhälle", "stat",
|
|
144
|
+
# Transport
|
|
145
|
+
"flyg", "järnväg", "spår", "tåg", "tunnel", "buss",
|
|
146
|
+
# Common adjectives
|
|
147
|
+
"stor", "liten", "ny", "gammal", "ung", "lång", "kort", "bred",
|
|
148
|
+
"hög", "låg", "varm", "kall", "vit", "svart", "röd", "blå", "grön",
|
|
149
|
+
"snabb", "sen", "fin", "ren", "fri",
|
|
150
|
+
# Common verbs (stem)
|
|
151
|
+
"lek", "spel", "skriv", "läs", "kör", "spring", "tänk", "sov",
|
|
152
|
+
"sjung", "flytt", "bygg", "städ", "lär", "säg",
|
|
153
|
+
# Body parts
|
|
154
|
+
"arm", "ben", "finger", "huvud", "mage", "rygg", "tand",
|
|
155
|
+
# Nature
|
|
156
|
+
"berg", "blad", "blomma", "eld", "frukt", "gren", "sten", "träd",
|
|
157
|
+
# House/building
|
|
158
|
+
"dörr", "fönster", "golv", "kök", "rum", "trapp", "vägg",
|
|
159
|
+
"byggnad", "våning",
|
|
160
|
+
# Tech
|
|
161
|
+
"data", "fil", "kod", "nät", "webb", "server", "skärm",
|
|
162
|
+
# More common compound parts (prefixes)
|
|
163
|
+
"arbets", "efter", "flyg", "folk", "fot", "före", "grupp", "halv",
|
|
164
|
+
"huvud", "inne", "järn", "lands", "mellan", "mitt", "natt", "riks",
|
|
165
|
+
"sam", "sjuk", "slut", "snö", "sol", "stats", "stor", "trafik",
|
|
166
|
+
"tunnel", "under", "ute", "över",
|
|
167
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Consistency checker — find same source translated differently."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class Inconsistency:
|
|
10
|
+
"""A source string with multiple different translations."""
|
|
11
|
+
source: str
|
|
12
|
+
translations: dict[str, list[str]] # translation → list of locations
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ConsistencyChecker:
|
|
16
|
+
"""Check that identical source strings have identical translations.
|
|
17
|
+
|
|
18
|
+
Usage:
|
|
19
|
+
checker = ConsistencyChecker()
|
|
20
|
+
checker.add("Save", "Spara", "file_menu.po:12")
|
|
21
|
+
checker.add("Save", "Lagra", "other.po:45")
|
|
22
|
+
issues = checker.check()
|
|
23
|
+
# → [Inconsistency(source="Save", translations={"Spara": [...], "Lagra": [...]})]
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, *, case_sensitive: bool = True):
|
|
27
|
+
self._case_sensitive = case_sensitive
|
|
28
|
+
# source → {translation → [locations]}
|
|
29
|
+
self._entries: dict[str, dict[str, list[str]]] = {}
|
|
30
|
+
|
|
31
|
+
def _normalize(self, text: str) -> str:
|
|
32
|
+
if self._case_sensitive:
|
|
33
|
+
return text
|
|
34
|
+
return text.lower()
|
|
35
|
+
|
|
36
|
+
def add(self, source: str, translation: str, location: str = "") -> None:
|
|
37
|
+
"""Register a source→translation pair."""
|
|
38
|
+
key = self._normalize(source)
|
|
39
|
+
if key not in self._entries:
|
|
40
|
+
self._entries[key] = {}
|
|
41
|
+
norm_trans = self._normalize(translation)
|
|
42
|
+
if norm_trans not in self._entries[key]:
|
|
43
|
+
self._entries[key][norm_trans] = []
|
|
44
|
+
self._entries[key][norm_trans].append(location)
|
|
45
|
+
|
|
46
|
+
def check(self) -> list[Inconsistency]:
|
|
47
|
+
"""Return all source strings with inconsistent translations."""
|
|
48
|
+
issues = []
|
|
49
|
+
for source, translations in self._entries.items():
|
|
50
|
+
if len(translations) > 1:
|
|
51
|
+
issues.append(Inconsistency(source=source, translations=translations))
|
|
52
|
+
issues.sort(key=lambda i: i.source)
|
|
53
|
+
return issues
|
|
54
|
+
|
|
55
|
+
def add_po_file(self, path: str) -> None:
|
|
56
|
+
"""Load entries from a .po file (requires polib)."""
|
|
57
|
+
import polib
|
|
58
|
+
po = polib.pofile(path)
|
|
59
|
+
for entry in po.translated_entries():
|
|
60
|
+
loc = f"{path}:{entry.linenum}" if hasattr(entry, 'linenum') else path
|
|
61
|
+
self.add(entry.msgid, entry.msgstr, loc)
|
|
62
|
+
|
|
63
|
+
def add_ts_file(self, path: str) -> None:
|
|
64
|
+
"""Load entries from a Qt .ts file."""
|
|
65
|
+
import xml.etree.ElementTree as ET
|
|
66
|
+
tree = ET.parse(path)
|
|
67
|
+
for msg in tree.findall('.//message'):
|
|
68
|
+
src = msg.findtext('source', '')
|
|
69
|
+
trans = msg.findtext('translation', '')
|
|
70
|
+
if src and trans:
|
|
71
|
+
loc_elem = msg.find('location')
|
|
72
|
+
loc = f"{path}"
|
|
73
|
+
if loc_elem is not None:
|
|
74
|
+
loc = f"{loc_elem.get('filename', path)}:{loc_elem.get('line', '')}"
|
|
75
|
+
self.add(src, trans, loc)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Swedish↔English dictionary lookup based on Folkets lexikon."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class LookupResult:
|
|
11
|
+
"""A dictionary lookup result."""
|
|
12
|
+
word: str
|
|
13
|
+
translations: list[str]
|
|
14
|
+
found: bool
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class SwedishLexicon:
|
|
18
|
+
"""Swedish→English dictionary from Folkets lexikon (36k entries).
|
|
19
|
+
|
|
20
|
+
Usage:
|
|
21
|
+
lex = SwedishLexicon()
|
|
22
|
+
result = lex.lookup("hund")
|
|
23
|
+
# → LookupResult(word="hund", translations=["dog"], found=True)
|
|
24
|
+
|
|
25
|
+
results = lex.search("bil")
|
|
26
|
+
# → all words starting with "bil": bil, bildäck, bilkörning, ...
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self):
|
|
30
|
+
self._dict: dict[str, list[str]] = {}
|
|
31
|
+
self._load()
|
|
32
|
+
|
|
33
|
+
def _load(self):
|
|
34
|
+
tsv_path = Path(__file__).parent.parent / "data" / "folkets_sv_en.tsv"
|
|
35
|
+
if not tsv_path.exists():
|
|
36
|
+
return
|
|
37
|
+
for line in tsv_path.read_text(encoding="utf-8").splitlines():
|
|
38
|
+
if line.startswith("#") or "\t" not in line:
|
|
39
|
+
continue
|
|
40
|
+
sv, en = line.split("\t", 1)
|
|
41
|
+
self._dict[sv.strip()] = [t.strip() for t in en.split(",")]
|
|
42
|
+
|
|
43
|
+
def lookup(self, word: str) -> LookupResult:
|
|
44
|
+
"""Look up a Swedish word and get English translations."""
|
|
45
|
+
key = word.lower().strip()
|
|
46
|
+
if key in self._dict:
|
|
47
|
+
return LookupResult(word=word, translations=self._dict[key], found=True)
|
|
48
|
+
return LookupResult(word=word, translations=[], found=False)
|
|
49
|
+
|
|
50
|
+
def search(self, prefix: str, limit: int = 20) -> list[LookupResult]:
|
|
51
|
+
"""Find all words starting with prefix."""
|
|
52
|
+
prefix = prefix.lower().strip()
|
|
53
|
+
results = []
|
|
54
|
+
for sv, ens in self._dict.items():
|
|
55
|
+
if sv.startswith(prefix):
|
|
56
|
+
results.append(LookupResult(word=sv, translations=ens, found=True))
|
|
57
|
+
if len(results) >= limit:
|
|
58
|
+
break
|
|
59
|
+
return results
|
|
60
|
+
|
|
61
|
+
def reverse_lookup(self, english: str, limit: int = 20) -> list[LookupResult]:
|
|
62
|
+
"""Find Swedish words that translate to a given English word."""
|
|
63
|
+
eng_lower = english.lower().strip()
|
|
64
|
+
results = []
|
|
65
|
+
for sv, ens in self._dict.items():
|
|
66
|
+
if any(eng_lower in e.lower() for e in ens):
|
|
67
|
+
results.append(LookupResult(word=sv, translations=ens, found=True))
|
|
68
|
+
if len(results) >= limit:
|
|
69
|
+
break
|
|
70
|
+
return results
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def size(self) -> int:
|
|
74
|
+
return len(self._dict)
|