|
1 | 1 | # spell
|
2 | 2 |
|
3 |
| -[](https://godoc.org/github.com/eskriett/spell) |
| 3 | +[](https://godoc.org/github.com/eskriett/spell) |
4 | 4 | [](https://goreportcard.com/report/github.com/eskriett/spell)
|
5 | 5 |
|
6 | 6 | A blazing fast spell checker written in Go.
|
7 | 7 |
|
8 | 8 | __N.B.__ This library is still in early development and may change.
|
9 | 9 |
|
10 |
| -# Overview |
| 10 | +## Overview |
11 | 11 |
|
12 | 12 | ```go
|
13 | 13 | package main
|
14 | 14 |
|
15 | 15 | import (
|
16 |
| - "fmt" |
| 16 | + "fmt" |
17 | 17 |
|
18 |
| - "github.com/eskriett/spell" |
| 18 | + "github.com/eskriett/spell" |
19 | 19 | )
|
20 | 20 |
|
21 | 21 | func main() {
|
22 | 22 |
|
23 |
| - // Create a new instance of spell |
24 |
| - s := spell.New() |
25 |
| - |
26 |
| - // Add words to the dictionary. Words require a frequency, but can have |
27 |
| - // other arbitrary metadata associated with them |
28 |
| - s.AddEntry(spell.Entry{ |
29 |
| - Word: "two", |
30 |
| - WordData: spell.WordData{ |
31 |
| - "frequency": 100, |
32 |
| - "type": "number", |
33 |
| - }, |
34 |
| - }) |
35 |
| - s.AddEntry(spell.Entry{ |
36 |
| - Word: "town", |
37 |
| - WordData: spell.WordData{ |
38 |
| - "frequency": 10, |
39 |
| - "type": "noun", |
40 |
| - }, |
41 |
| - }) |
42 |
| - |
43 |
| - // Lookup a misspelling, by default the "best" suggestion will be returned |
44 |
| - suggestions, _ := s.Lookup("twon") |
45 |
| - fmt.Printf("%v\n", suggestions) |
46 |
| - // -> [two] |
47 |
| - |
48 |
| - // Get metadata from the suggestion |
49 |
| - suggestion := suggestions[0] |
50 |
| - fmt.Printf("%v\n", suggestion.WordData["type"]) |
51 |
| - // -> number |
52 |
| - |
53 |
| - // Get multiple suggestions during lookup |
54 |
| - suggestions, _ = s.Lookup("twon", spell.SuggestionLevel(spell.LevelAll)) |
55 |
| - fmt.Printf("%v\n", suggestions) |
56 |
| - // -> [two, town] |
57 |
| - |
58 |
| - // Save the dictionary |
59 |
| - s.Save("dict.spell") |
60 |
| - |
61 |
| - // Load the dictionary |
62 |
| - s2, _ := spell.Load("dict.spell") |
63 |
| - |
64 |
| - suggestions, _ = s2.Lookup("twon", spell.SuggestionLevel(spell.LevelAll)) |
65 |
| - fmt.Printf("%v\n", suggestions) |
66 |
| - // -> [two, town] |
67 |
| - |
68 |
| - // Spell supports word segmentation |
69 |
| - s3 := spell.New() |
70 |
| - |
71 |
| - wd := spell.WordData{"frequency": 1} |
72 |
| - s3.AddEntry(spell.Entry{Word: "the", WordData: wd}) |
73 |
| - s3.AddEntry(spell.Entry{Word: "quick", WordData: wd}) |
74 |
| - s3.AddEntry(spell.Entry{Word: "brown", WordData: wd}) |
75 |
| - s3.AddEntry(spell.Entry{Word: "fox", WordData: wd}) |
76 |
| - |
77 |
| - segmentResult, _ := s3.Segment("thequickbrownfox") |
78 |
| - fmt.Println(segmentResult) |
79 |
| - // -> the quick brown fox |
| 23 | + // Create a new instance of spell |
| 24 | + s := spell.New() |
| 25 | + |
| 26 | + // Add words to the dictionary. Words require a frequency, but can have |
| 27 | + // other arbitrary metadata associated with them |
| 28 | + s.AddEntry(spell.Entry{ |
| 29 | + Word: "two", |
| 30 | + WordData: spell.WordData{ |
| 31 | + "frequency": 100, |
| 32 | + "type": "number", |
| 33 | + }, |
| 34 | + }) |
| 35 | + s.AddEntry(spell.Entry{ |
| 36 | + Word: "town", |
| 37 | + WordData: spell.WordData{ |
| 38 | + "frequency": 10, |
| 39 | + "type": "noun", |
| 40 | + }, |
| 41 | + }) |
| 42 | + |
| 43 | + // Lookup a misspelling, by default the "best" suggestion will be returned |
| 44 | + suggestions, _ := s.Lookup("twon") |
| 45 | + fmt.Printf("%v\n", suggestions) |
| 46 | + // -> [two] |
| 47 | + |
| 48 | + // Get metadata from the suggestion |
| 49 | + suggestion := suggestions[0] |
| 50 | + fmt.Printf("%v\n", suggestion.WordData["type"]) |
| 51 | + // -> number |
| 52 | + |
| 53 | + // Get multiple suggestions during lookup |
| 54 | + suggestions, _ = s.Lookup("twon", spell.SuggestionLevel(spell.LevelAll)) |
| 55 | + fmt.Printf("%v\n", suggestions) |
| 56 | + // -> [two, town] |
| 57 | + |
| 58 | + // Save the dictionary |
| 59 | + s.Save("dict.spell") |
| 60 | + |
| 61 | + // Load the dictionary |
| 62 | + s2, _ := spell.Load("dict.spell") |
| 63 | + |
| 64 | + suggestions, _ = s2.Lookup("twon", spell.SuggestionLevel(spell.LevelAll)) |
| 65 | + fmt.Printf("%v\n", suggestions) |
| 66 | + // -> [two, town] |
| 67 | + |
| 68 | + // Spell supports word segmentation |
| 69 | + s3 := spell.New() |
| 70 | + |
| 71 | + wd := spell.WordData{"frequency": 1} |
| 72 | + s3.AddEntry(spell.Entry{Word: "the", WordData: wd}) |
| 73 | + s3.AddEntry(spell.Entry{Word: "quick", WordData: wd}) |
| 74 | + s3.AddEntry(spell.Entry{Word: "brown", WordData: wd}) |
| 75 | + s3.AddEntry(spell.Entry{Word: "fox", WordData: wd}) |
| 76 | + |
| 77 | + segmentResult, _ := s3.Segment("thequickbrownfox") |
| 78 | + fmt.Println(segmentResult) |
| 79 | + // -> the quick brown fox |
80 | 80 | }
|
81 | 81 | ```
|
82 | 82 |
|
83 |
| -# Credits |
| 83 | +## Credits |
84 | 84 |
|
85 | 85 | Spell makes use of a symmetric delete algorithm and is loosely based on the
|
86 | 86 | [SymSpell](https://github.com/wolfgarbe/SymSpell) implementation.
|
0 commit comments