File size: 2,350 Bytes
fc55559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
license: apache-2.0
library_name: naptrap
language:
  - en
pipeline_tag: feature-extraction
tags:
  - grapheme-to-phoneme
  - g2p
  - keyword-spotting
  - wake-word
  - pytorch
  - safetensors
---

# g2p_en decoder: keyword text features for naptrap

These are the weights of the [g2p_en](https://github.com/Kyubyong/g2p)
grapheme-to-phoneme model (Kyubyong Park & Jongseok Kim, Apache-2.0, release
2.1.0), repackaged as safetensors for
[naptrap](https://github.com/androiddrew/naptrap). The values are unchanged.

naptrap does **not** use this model to output phoneme strings. The keyword
models, such as
[`androiddrew/naptrap-phonmatchnet-en`](https://huggingface.co/androiddrew/naptrap-phonmatchnet-en),
were trained on the decoder's **hidden states**. This is the "embedding" path
from the PhonMatchNet reference code:

1. Normalize the keyword: lowercase it, strip accents, and keep `[a-z']` words.
   Numbers must be spelled out.
2. Encode each word's characters with the grapheme GRU.
3. Decode greedily (up to 20 steps), keeping each emitted 256-D decoder state.
4. Join the words with the decoder's `<pad>` embedding as a separator.

The result is a `[positions, 256]` float32 matrix.

> Not affiliated with or endorsed by the g2p_en authors. See `LICENSE` and `NOTICE`.

## Usage

```python
from huggingface_hub import snapshot_download
from naptrap import G2PTextEncoder

encoder = G2PTextEncoder.from_pretrained(
    snapshot_download("androiddrew/g2p-en-decoder-pytorch", revision="v1")
)
features = encoder("hey computer")   # torch.float32 [positions, 256]
```

## Accuracy of the PyTorch port

We compared naptrap's PyTorch implementation with the original NumPy
`G2p.embedding()` on 545 keywords. They included Speech Commands words, wake
phrases, words with apostrophes and accents, 350 random CMUdict words and 150
random multi-word phrases. Every output length matched, and the maximum
absolute difference was **4.6e-6**.

## Limitations

The model was trained on CMUdict pronunciations of American English words.
Unusual names, brand names and non-English words get its best-guess
pronunciation, which may not match how people say them. naptrap does not use
the dictionary-lookup, homograph or number-expansion steps of the g2p_en
package.

`conversion/convert.py` reproduces `model.safetensors` from the g2p_en 2.1.0
wheel.