Metadata-Version: 2.4
Name: spellcure
Version: 7.0.0
Summary: SpellCure v7 - fourteen-channel spelling correction engine with learned dual-expert reranking; corrects heavily scrambled text and typos at sub-millisecond latency. Devloped by Saheban Khan
Author-email: Saheban Khan <sahebankhan21@gmail.com>
Maintainer: Tohid Khan
License: MIT
Project-URL: Homepage, https://github.com/Lsaheban/SpellCure
Project-URL: Repository, https://github.com/Lsaheban/SpellCure
Keywords: spell,spelling,spellcheck,spellchecker,correction,typo,scrambled,fuzzy-matching,string-matching,noisy-channel,nlp,text-processing
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=2.0
Requires-Dist: jellyfish>=0.8
Requires-Dist: wordfreq>=3.0
Requires-Dist: nltk>=3.8
Dynamic: license-file

# SpellCure

**SpellCure** is a mathematical, dictionary-driven spelling-correction engine for heavily scrambled or distorted text, created by **Saheban Khan** *(GitHub: [Lsaheban](https://github.com/Lsaheban))* and maintained by **Tohid Khan** *(GitHub: Tohid096)*.

Version **7.0.0** ships the **v7 engine**: a fourteen-channel scorer with learned dual-expert reranking that recovers both extreme scrambles (`aplpe → apple`) and realistic typos (`recieve → receive`, `truely → truly`) at sub-millisecond latency per token on a single thread.

---

## What's new in 7.0.0

The jump from 1.0.x to 7.0.0 mirrors the engine's research line (v1 → v7):

- **Fourteen scoring channels** — presence, position, lensim, frequency prior, OSA similarity, Jaro-Winkler, prefix, keyboard-adjacency, anagram, OSA-1 nearest neighbour, distinct-count, multiset distance, anagram-count, and the new *esig* (pure-insertion) channel.
- **Two learned softmax experts** — every candidate is scored by an *edit* expert and a *structural* expert; a observable gate routes each query. Weights shipped in this package were trained on 17,963 corrupted/true pairs and selected on 4,493 held-out validation pairs (**val top-1 0.8429**, miss rate 2.34 %).
- **Vectorized delete-2 signature gather** over a packed CSR storage layer with a dominant-candidate fast path — **mean 0.18 ms / median 0.10 ms / p95 0.49 ms** per token (50k lexicon, single thread).
- **Bundled assets** — the exact 50,000-word frequency-ranked lexicon and the deployed training checkpoint used for every number in the SpellCure research paper.
- **Legacy API unchanged** — the 1.0.x `corrector` class is still here, byte-for-byte.

---

## Installation

```bash
pip install spellcure
```

Requires Python ≥ 3.9. Dependencies: `numpy`, `jellyfish`, `wordfreq`, `nltk`.

---

## Quick start (v7 engine)

The default engine loads the bundled 50k lexicon and trained weights on first use (index build takes a few seconds; it is cached process-wide):

```python
import spellcure

spellcure.suggest("truely")
# ['truly', 'rudely', 'cruelty']

spellcure.suggest("recieve")
# ['receive', 'relieve', 'received']

spellcure.correct("aplpe tihs wrod is msesdy")
# 'apple/maple/ample this/thus/tips word/road/wood is/his/its messy/messed/missed'
```

`suggest(word)` returns up to three ranked candidates for one word. `correct(text)` applies it per token and joins each token's candidates with `/` (best candidate first).

### Own the engine object

For custom vocabularies, tuning knobs, or training, build the engine directly:

```python
from spellcure import SpellCureV7

engine = SpellCureV7(my_vocab_list)          # any list of case-folded words
engine = SpellCureV7(my_vocab_list,
                     extra_vocab=hunspell_words,   # optional extra lexicon
                     use_wordfreq=True)            # zipf frequency prior

engine.suggest("speling")   # ['spelling', 'sperling', 'spewing'] (50k lexicon)
engine.fit(train_pairs, val_pairs=val_pairs)  # (corrupt, true) pairs, trains in place
```

`spellcure.load_default(**kwargs)` builds a fresh engine over the bundled lexicon; `spellcure.get_default_engine()` returns the cached singleton used by the module-level `suggest`/`correct` helpers.

### Legacy API (unchanged from 1.0.x)

```python
from spellcure import corrector

model = corrector(mode="small")          # ~10k curated built-in word bank
model.correct("olve is evryetign")       # 'love is everything'

model = corrector(mode="large")          # ~236k NLTK words
model = corrector(custom_vocab=["spell", "cure", "engine"])
```

---

## The fourteen channels

| # | Channel | Measures |
|---|---------|----------|
| 1 | `presence` | shared-character coverage (multiset intersection / query length) |
| 2 | `position` | position-weighted character agreement |
| 3 | `lensim` | length similarity |
| 4 | `prior` | word-frequency prior (wordfreq zipf) |
| 5 | `osa_sim` | optimal-string-alignment similarity |
| 6 | `jw` | Jaro-Winkler similarity |
| 7 | `prefix4` | shared 4-character prefix |
| 8 | `keyboard` | QWERTY key-adjacency plausibility |
| 9 | `anagram` | sorted-signature equality (full anagram) |
| 10 | `osa1nn` | OSA-1 nearest-neighbour signal from the index |
| 11 | `dcount` | shared delete-2 signature count (dominance) |
| 12 | `m1` | multiset distance ≤ 1 |
| 13 | `anac` | anagram candidate count signal |
| 14 | `esig` | exact-signature / pure-insertion signal |

Candidates are gathered by delete-2 signature lookup, cheap pools are verified by a dominant-candidate fast path, and the final ranking is produced by the two learned softmax experts (`spellcure.feature_names()` lists the channel order).

---

## Accuracy and latency

From the SpellCure research paper (50k lexicon, deployed weights; see the bundled checkpoint for the exact values):

| Metric | Value |
|--------|-------|
| Validation top-1 | **0.8429** |
| Miss rate | **2.34 %** |
| Mean latency / token | 0.181 ms |
| Median latency / token | 0.098 ms |
| p95 latency / token | 0.491 ms |
| Execution model | single thread, pure Python + NumPy |

Latency is hardware-dependent; the accuracy figures reproduce exactly with the bundled lexicon, weights, and protocol.

---

## Package assets

| Path | Contents |
|------|----------|
| `spellcure/data/vocab_50k.txt` | 50,000-word frequency-ranked English lexicon |
| `spellcure/weights/v7_train.json` | deployed dual-expert checkpoint (edit + structural weights, validation metrics) |
| `spellcure/engine/` | verbatim research engines: `v5.py`, `v6.py`, `v7.py` |

```python
import spellcure
spellcure.load_vocab()      # bundled 50k lexicon as a list
spellcure.load_weights()    # deployed checkpoint as a dict
spellcure.apply_trained_weights(engine, path=None, variant=None)
```

---

## How it works

For every query token SpellCure (1) enumerates the query's delete-≤2 signature set, (2) gathers candidate words sharing signatures from packed CSR indexes, (3) attempts a dominant-candidate fast path verified by an OSA ≤ 2 band check, and (4) otherwise assembles edit/structural pools and reranks them with fourteen features under two learned softmax experts. The search, tie-breaks, and determinism are identical to the published research engines, which ship verbatim in `spellcure.engine` for comparison and ablation.

---

## Credits

SpellCure was created by **Saheban Khan** and is maintained by **Tohid Khan**. The v5–v7 engine line, its training protocol, and every benchmark number in this README are documented in the accompanying SpellCure research papers.

## License

MIT — see `LICENSE`.
