spell 0.3.0 → 0.8.0
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.
- package/README.md +35 -149
- package/package.json +24 -21
- package/.npmignore +0 -4
- package/.travis.yml +0 -6
- package/LICENSE +0 -14
- package/spell.js +0 -347
- package/test/css/mocha.css +0 -110
- package/test/index.html +0 -34
- package/test/lib/JSONStream/.npmignore +0 -3
- package/test/lib/JSONStream/bench.js +0 -35
- package/test/lib/JSONStream/examples/all_docs.js +0 -13
- package/test/lib/JSONStream/index.js +0 -118
- package/test/lib/JSONStream/package.json +0 -13
- package/test/lib/JSONStream/readme.markdown +0 -120
- package/test/lib/JSONStream/test/fixtures/all_docs_include.json +0 -4245
- package/test/lib/JSONStream/test/fixtures/all_npm.json +0 -4030
- package/test/lib/JSONStream/test/parsejson.js +0 -25
- package/test/lib/JSONStream/test/stringify.js +0 -41
- package/test/lib/JSONStream/test/test.js +0 -35
- package/test/lib/JSONStream/test/test2.js +0 -29
- package/test/lib/JSONStream/test/two-ways.js +0 -41
- package/test/lib/jquery-1.7.1.min.js +0 -4
- package/test/lib/json2.js +0 -487
- package/test/lib/mocha-0.3.6.js +0 -2672
- package/test/lib/request/LICENSE +0 -55
- package/test/lib/request/README.md +0 -285
- package/test/lib/request/main.js +0 -618
- package/test/lib/request/mimetypes.js +0 -146
- package/test/lib/request/oauth.js +0 -34
- package/test/lib/request/package.json +0 -15
- package/test/lib/request/tests/googledoodle.png +0 -0
- package/test/lib/request/tests/run.sh +0 -6
- package/test/lib/request/tests/server.js +0 -57
- package/test/lib/request/tests/test-body.js +0 -90
- package/test/lib/request/tests/test-cookie.js +0 -29
- package/test/lib/request/tests/test-cookiejar.js +0 -90
- package/test/lib/request/tests/test-errors.js +0 -30
- package/test/lib/request/tests/test-oauth.js +0 -109
- package/test/lib/request/tests/test-pipes.js +0 -167
- package/test/lib/request/tests/test-proxy.js +0 -39
- package/test/lib/request/tests/test-timeout.js +0 -87
- package/test/lib/request/uuid.js +0 -19
- package/test/lib/request/vendor/cookie/index.js +0 -55
- package/test/lib/request/vendor/cookie/jar.js +0 -72
- package/test/perf.js +0 -44
- package/test/resources/big.js +0 -9
- package/test/resources/big.json +0 -29159
- package/test/resources/big.txt +0 -128457
- package/test/resources/big_browser.js +0 -29159
- package/test/resources/npm.js +0 -26
- package/test/resources/npm.json +0 -84701
- package/test/resources/perf1.js +0 -144
- package/test/resources/perf2.js +0 -366
- package/test/spell.js +0 -295
package/README.md
CHANGED
|
@@ -1,166 +1,52 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Spell
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Programs you can read out loud.**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+

|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
You just read a program.
|
|
8
|
+
Nobody taught you its syntax, and you could explain it to a friend.
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
It isn't pseudo-code, and it isn't an AI prompt:
|
|
11
|
+
- a rule-based parser reads Spell the same way every time
|
|
12
|
+
- it compiles to optimized, readable code you can run right on the web.
|
|
12
13
|
|
|
13
|
-
##
|
|
14
|
+
## Solitaire in Spell
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
2. load it into your webpage
|
|
16
|
+

|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
<p align="center"><em>Klondike Solitaire in the Spell editor, a card selected in the Inspector</em></p>
|
|
19
19
|
|
|
20
|
-
##
|
|
20
|
+
## All magic, no fuss!
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
// instantiate a new dictionary
|
|
24
|
-
var dict = spell();
|
|
25
|
-
// load text into dictionary so we can train the dictionary to know
|
|
26
|
-
// which words exists and which ones are more frequent than others
|
|
27
|
-
dict.load("I am going to the park with Theo today. It's going to be the bomb");
|
|
28
|
-
console.log(dict.suggest('thew'));
|
|
29
|
-
```
|
|
22
|
+

|
|
30
23
|
|
|
31
|
-
|
|
24
|
+
- **A language** that reads like **English** (and **Spanish**): types, properties and actions are sentences.
|
|
25
|
+
- **A compiler** to **JavaScript**, **TypeScript** and **Python**, so what you write runs where you need it.
|
|
26
|
+
- **An editor** where the program, the running app and its output sit side by side.
|
|
32
27
|
|
|
33
|
-
|
|
34
|
-
[{"word": "the", "score": 2}, {"word": "theo", "score": 1}]
|
|
35
|
-
```
|
|
28
|
+
## Spell UI
|
|
36
29
|
|
|
37
|
-
|
|
30
|
+
A full set of beautifully themed web components (`<ui-button>`, `<ui-menu>`, `<ui-table>` ...)
|
|
31
|
+
that work in any page, with or without the rest of Spell.
|
|
38
32
|
|
|
39
|
-
|
|
33
|
+

|
|
40
34
|
|
|
41
|
-
|
|
42
|
-
dict.lucky('thew');
|
|
43
|
-
```
|
|
35
|
+
## Status
|
|
44
36
|
|
|
45
|
-
|
|
37
|
+
Features working now:
|
|
38
|
+
- **Spell**, the language: **English** and **Spanish**, in sentences or an outline.
|
|
39
|
+
- Output to web apps in **JavaScript**, **TypeScript** and **Python**.
|
|
40
|
+
- **Spell Editor**: interactively write, run, and test a program in one window.
|
|
41
|
+
- **Spell UI**: powerful, friendly, fully-themed web components for any page.
|
|
42
|
+
- **VS Code** extension: syntax colouring, errors and help as you type.
|
|
43
|
+
- The `spell` command line: compile, run, test and translate projects.
|
|
44
|
+
- AI-written spells in VSCode, via Claude Code.
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
Coming soon:
|
|
47
|
+
- Use AI to write a spell on your computer, tablet or phone.
|
|
48
|
+
- Publish your Spell site on the web.
|
|
49
|
+
- More human languages.
|
|
50
|
+
- More programming languages.
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
``` js
|
|
54
|
-
dict.remove_word('park');
|
|
55
|
-
dict.add_word('nuno');
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
and you can `reset` the dictionary, making it empty:
|
|
59
|
-
|
|
60
|
-
``` js
|
|
61
|
-
dict.reset();
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
if you want to export the dictionary:
|
|
65
|
-
|
|
66
|
-
``` js
|
|
67
|
-
dict.export();
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## advanced
|
|
71
|
-
|
|
72
|
-
when loading you can provide a compiled dictionary instead of free form text
|
|
73
|
-
|
|
74
|
-
``` js
|
|
75
|
-
dict.load(
|
|
76
|
-
{ corpus:
|
|
77
|
-
{ "I" : 1
|
|
78
|
-
, "am" : 1
|
|
79
|
-
, "going" : 1
|
|
80
|
-
, "to" : 2
|
|
81
|
-
, "the" : 1
|
|
82
|
-
, "park" : 1
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
);
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
you can also provide options:
|
|
89
|
-
|
|
90
|
-
* `reset`, defaults to true, meaning it will reset the dictionary before running load
|
|
91
|
-
* `store`, defaults to true, meaning it will store the dictionary after running load
|
|
92
|
-
* `after_store`, defaults to empty function, the callback function to run after `store` is done
|
|
93
|
-
|
|
94
|
-
e.g. to add text to an existing `dict` you could do:
|
|
95
|
-
|
|
96
|
-
``` js
|
|
97
|
-
dict.load("Better yet, chocolate", { reset: false } );
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
finally when adding words you can optionally give it a score:
|
|
101
|
-
|
|
102
|
-
``` js
|
|
103
|
-
dict.add_word('beer', {score: 100});
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
if you are working with words that include punctuation in your dictionary you might need to override the alphabet that is being used. e.g. you might want to add `.` & `@` if you have a dictionary of email address. you can do that by:
|
|
107
|
-
|
|
108
|
-
``` js
|
|
109
|
-
// instantiate a new dictionary
|
|
110
|
-
var dict = spell();
|
|
111
|
-
// load text into dictionary so we can train the dictionary to know
|
|
112
|
-
// which words exists and which ones are more frequent than others
|
|
113
|
-
dict.load("nuno@dino.saur pedro@space.ship");
|
|
114
|
-
console.log(dict.suggest('nuno@dino.sau',
|
|
115
|
-
"abcdefghijklmnopqrstuvwxyz.@".split("")
|
|
116
|
-
));
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## storage
|
|
120
|
-
|
|
121
|
-
by default `dict` is stored in process (memory) for each dictionary instance you create. however if you feel like storing the dictionary externally, or use a shared dictionary, you can:
|
|
122
|
-
|
|
123
|
-
an example using `localStorage`:
|
|
124
|
-
|
|
125
|
-
``` js
|
|
126
|
-
var dict = spell(
|
|
127
|
-
{ "get" : function () {
|
|
128
|
-
JSON.parse(window.localStorage.getItem('dict'));
|
|
129
|
-
}
|
|
130
|
-
, "store" : function (dict,after_store) {
|
|
131
|
-
window.localStorage.setItem('dict', JSON.stringify(dict));
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
);
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
# roadmap
|
|
138
|
-
|
|
139
|
-
check [issues]
|
|
140
|
-
|
|
141
|
-
# contribute
|
|
142
|
-
|
|
143
|
-
everyone is welcome to contribute. patches, bug-fixes, new features
|
|
144
|
-
|
|
145
|
-
1. create an [issue][issues] so the community can comment on your idea
|
|
146
|
-
2. fork `spell`
|
|
147
|
-
3. create a new branch `git checkout -b my_branch`
|
|
148
|
-
4. create tests for the changes you made
|
|
149
|
-
5. make sure you pass both existing and newly inserted tests
|
|
150
|
-
6. commit your changes
|
|
151
|
-
7. push to your branch `git push origin my_branch`
|
|
152
|
-
8. create an pull request
|
|
153
|
-
|
|
154
|
-
# meta
|
|
155
|
-
|
|
156
|
-
* code: `git clone git://github.com/dscape/spell.git`
|
|
157
|
-
* home: <http://github.com/dscape/spell>
|
|
158
|
-
* bugs: <http://github.com/dscape/spell/issues>
|
|
159
|
-
* build: [](http://travis-ci.org/dscape/spell)
|
|
160
|
-
|
|
161
|
-
`(oO)--',-` in [caos]
|
|
162
|
-
|
|
163
|
-
[npm]: http://npmjs.org
|
|
164
|
-
[issues]: http://github.com/dscape/spell/issues
|
|
165
|
-
[caos]: http://caos.di.uminho.pt/
|
|
166
|
-
[norvig]: http://norvig.com/spell-correct.html
|
|
52
|
+
The first release is on its way, targeting **December 2026**.
|
package/package.json
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "spell",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Spell: write programs in plain English that compile, run and draw. Coming soon.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"spell",
|
|
7
|
+
"language",
|
|
8
|
+
"compiler",
|
|
9
|
+
"english",
|
|
10
|
+
"web-components",
|
|
11
|
+
"ui"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/spell-app/spell",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/spell-app/spell.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"files": [
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/spell-app/spell/issues"
|
|
20
24
|
}
|
|
21
|
-
|
|
22
|
-
}
|
|
25
|
+
}
|
package/.npmignore
DELETED
package/.travis.yml
DELETED
package/LICENSE
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
copyright 2011 nuno job <nunojob.com> (oO)--',--
|
|
2
|
-
& pedro teixeira <metaduck.com>
|
|
3
|
-
|
|
4
|
-
licensed under the apache license, version 2.0 (the "license");
|
|
5
|
-
you may not use this file except in compliance with the license.
|
|
6
|
-
you may obtain a copy of the license at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the license is distributed on an "as is" basis,
|
|
12
|
-
without warranties or conditions of any kind, either express or implied.
|
|
13
|
-
see the license for the specific language governing permissions and
|
|
14
|
-
limitations under the license.
|
package/spell.js
DELETED
|
@@ -1,347 +0,0 @@
|
|
|
1
|
-
/* javascript spell checker based on
|
|
2
|
-
* http://norvig.com/spell-correct.html
|
|
3
|
-
*
|
|
4
|
-
* copyright 2011 nuno job <nunojob.com> (oO)--',--
|
|
5
|
-
* pedro teixeira <metaduck.com>
|
|
6
|
-
*
|
|
7
|
-
* licensed under the apache license, version 2.0 (the "license");
|
|
8
|
-
* you may not use this file except in compliance with the license.
|
|
9
|
-
* you may obtain a copy of the license at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the license is distributed on an "as is" basis,
|
|
15
|
-
* without warranties or conditions of any kind, either express or implied.
|
|
16
|
-
* see the license for the specific language governing permissions and
|
|
17
|
-
* limitations under the license.
|
|
18
|
-
*/
|
|
19
|
-
(function () {
|
|
20
|
-
var root = this
|
|
21
|
-
, previous_spell = root.spell
|
|
22
|
-
;
|
|
23
|
-
|
|
24
|
-
/*
|
|
25
|
-
* dictionary
|
|
26
|
-
*
|
|
27
|
-
* creates a dictionary given a place to store
|
|
28
|
-
*
|
|
29
|
-
* @param {dict_store:object:required}
|
|
30
|
-
* object that implements two functions
|
|
31
|
-
* `get` to retrieve a stored dictionary from disk/memory
|
|
32
|
-
* `store` to store a dictionary from disk/memory
|
|
33
|
-
*
|
|
34
|
-
* @return {object} a dictionary module
|
|
35
|
-
*/
|
|
36
|
-
var spell = function dictionary(dict_store) {
|
|
37
|
-
var dict =
|
|
38
|
-
dict_store && typeof dict_store.get === 'function' ? dict_store.get() : {}
|
|
39
|
-
, noop = function(){}
|
|
40
|
-
, alphabet = "abcdefghijklmnopqrstuvwxyz".split("")
|
|
41
|
-
;
|
|
42
|
-
|
|
43
|
-
function spell_store(cb) {
|
|
44
|
-
if (dict_store && typeof dict_store.store === 'function') {
|
|
45
|
-
dict_store.store(dict, cb);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function spell_train(corpus,regex) {
|
|
50
|
-
var match, word;
|
|
51
|
-
regex = regex || /[a-z]+/g;
|
|
52
|
-
corpus = corpus.toLowerCase();
|
|
53
|
-
while ((match = regex.exec(corpus))) {
|
|
54
|
-
word = match[0];
|
|
55
|
-
spell_add_word(word, 1);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function spell_edits(word, alphabetOverride) {
|
|
60
|
-
var edits = []
|
|
61
|
-
, thisAlphabet = alphabetOverride ? alphabetOverride : alphabet
|
|
62
|
-
, i
|
|
63
|
-
, j
|
|
64
|
-
;
|
|
65
|
-
for (i=0; i < word.length; i++) { // deletion
|
|
66
|
-
edits.push(word.slice(0, i) + word.slice(i+1));
|
|
67
|
-
}
|
|
68
|
-
for (i=0; i < word.length-1; i++) { // transposition
|
|
69
|
-
edits.push( word.slice(0, i) + word.slice(i+1, i+2) +
|
|
70
|
-
word.slice(i, i+1) + word.slice(i+2));
|
|
71
|
-
}
|
|
72
|
-
for (i=0; i < word.length; i++) { // alteration
|
|
73
|
-
for(j in thisAlphabet) {
|
|
74
|
-
edits.push(word.slice(0, i) + thisAlphabet[j] + word.slice(i+1));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
for (i=0; i <= word.length; i++) { // insertion
|
|
78
|
-
for(j in thisAlphabet) {
|
|
79
|
-
edits.push(word.slice(0, i) + thisAlphabet[j] + word.slice(i));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return edits;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function is_empty(obj) {
|
|
86
|
-
for (var key in obj) { if (obj.hasOwnProperty(key)) return false; }
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function spell_order(candidates, min, max) {
|
|
91
|
-
var ordered_candidates = []
|
|
92
|
-
, current
|
|
93
|
-
, i
|
|
94
|
-
, w
|
|
95
|
-
;
|
|
96
|
-
for(i=max; i>=min; i--) {
|
|
97
|
-
if(candidates.hasOwnProperty(i)) {
|
|
98
|
-
current = candidates[i];
|
|
99
|
-
for (w in current) {
|
|
100
|
-
if(current.hasOwnProperty(w)) {
|
|
101
|
-
ordered_candidates.push({"word": w, "score": i});
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return ordered_candidates;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/*
|
|
110
|
-
* reset
|
|
111
|
-
*
|
|
112
|
-
* resets the dictionary.
|
|
113
|
-
*
|
|
114
|
-
* e.g.
|
|
115
|
-
* spell.reset();
|
|
116
|
-
*
|
|
117
|
-
* @return void
|
|
118
|
-
*/
|
|
119
|
-
function spell_reset() { return spell_load({reset: true}); }
|
|
120
|
-
|
|
121
|
-
/*
|
|
122
|
-
* load
|
|
123
|
-
*
|
|
124
|
-
* loads a free form corpus dictionary.
|
|
125
|
-
*
|
|
126
|
-
* e.g.
|
|
127
|
-
* spell.load({'dog': 1, 'cat': 2});
|
|
128
|
-
* spell.load('dog cat cat');
|
|
129
|
-
*
|
|
130
|
-
* @param {opts.corpus:string|object:optional}
|
|
131
|
-
* corpus string to initialize to
|
|
132
|
-
* @param {opts.reset:boolean:optional}
|
|
133
|
-
* whether you want to reset the existing dictionary or just append
|
|
134
|
-
* to what already exists
|
|
135
|
-
* @param {opts.store:boolean:optional}
|
|
136
|
-
* decide if you want to use storage
|
|
137
|
-
* @param {opts.after_store:function:optional}
|
|
138
|
-
* function to call back when store is done
|
|
139
|
-
*
|
|
140
|
-
* @return void
|
|
141
|
-
*/
|
|
142
|
-
function spell_load(corpus, opts) {
|
|
143
|
-
if ('object' === typeof corpus) { opts = corpus; }
|
|
144
|
-
if ('string' === typeof corpus) {
|
|
145
|
-
if('object' === typeof opts) {
|
|
146
|
-
opts.corpus = corpus;
|
|
147
|
-
} else {
|
|
148
|
-
opts = {corpus: corpus };
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if ('string' === typeof opts) { opts = {corpus: opts }; }
|
|
152
|
-
opts = 'object' === typeof opts ? opts : {};
|
|
153
|
-
opts.reset = (opts.reset !== false);
|
|
154
|
-
opts.store = (opts.score !== false);
|
|
155
|
-
opts.after_store = opts.after_store || noop;
|
|
156
|
-
opts.corpus = opts.corpus || '';
|
|
157
|
-
if(opts.reset) { dict = {}; }
|
|
158
|
-
if('object' === typeof opts.corpus) {
|
|
159
|
-
for(var key in opts.corpus) {
|
|
160
|
-
spell_add_word(key, {score: opts.corpus[key]});
|
|
161
|
-
}
|
|
162
|
-
} else { spell_train(opts.corpus); }
|
|
163
|
-
if(opts.store) { spell_store(opts.after_store); }
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/*
|
|
167
|
-
* add word
|
|
168
|
-
*
|
|
169
|
-
* loads a word into the dictionary
|
|
170
|
-
*
|
|
171
|
-
* e.g.
|
|
172
|
-
* spell.insert_word('dog', 5);
|
|
173
|
-
*
|
|
174
|
-
* @param {word:string:required}
|
|
175
|
-
* the word you want to add
|
|
176
|
-
* @param {opts.count:int:optional}
|
|
177
|
-
* the number of times the word appears in a text, defaults to one
|
|
178
|
-
* @param {opts.store:boolean:optional}
|
|
179
|
-
* decide if you want to use storage
|
|
180
|
-
* @param {opts.done:function:optional}
|
|
181
|
-
* function to call back when store is done
|
|
182
|
-
*
|
|
183
|
-
* @return void
|
|
184
|
-
*/
|
|
185
|
-
function spell_add_word(word, opts) {
|
|
186
|
-
if ('string' === typeof opts || 'number' === typeof opts) {
|
|
187
|
-
opts = { score: parseInt(opts, 10) };
|
|
188
|
-
}
|
|
189
|
-
opts = 'object' === typeof opts ? opts : {};
|
|
190
|
-
opts.score = opts.score || 1;
|
|
191
|
-
opts.store = opts.store || true;
|
|
192
|
-
opts.done = opts.done || noop;
|
|
193
|
-
word = word.toLowerCase();
|
|
194
|
-
dict[word] =
|
|
195
|
-
dict.hasOwnProperty(word) ? dict[word] + opts.score : opts.score;
|
|
196
|
-
if(opts.store) { spell_store(opts.done); }
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/*
|
|
200
|
-
* remove word
|
|
201
|
-
*
|
|
202
|
-
* removes word from the dictionary
|
|
203
|
-
*
|
|
204
|
-
* e.g.
|
|
205
|
-
* spell.remove_word('dog');
|
|
206
|
-
*
|
|
207
|
-
* @param {word:string:required}
|
|
208
|
-
* the word you want to add
|
|
209
|
-
* @param {opts.store:boolean:optional}
|
|
210
|
-
* decide if you want to use storage
|
|
211
|
-
* @param {opts.done:function:optional}
|
|
212
|
-
* function to call back when store is done
|
|
213
|
-
*
|
|
214
|
-
* @return void
|
|
215
|
-
*/
|
|
216
|
-
function spell_remove_word(word,opts) {
|
|
217
|
-
opts = 'object' === typeof opts ? opts : {};
|
|
218
|
-
opts.store = (opts.score !== false);
|
|
219
|
-
opts.done = opts.done || noop;
|
|
220
|
-
if (dict.hasOwnProperty(word)) { delete dict[word]; }
|
|
221
|
-
if(opts.store) { spell_store(opts.done); }
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/*
|
|
225
|
-
* suggest
|
|
226
|
-
*
|
|
227
|
-
* returns spelling sugestions for a given word
|
|
228
|
-
*
|
|
229
|
-
* e.g.
|
|
230
|
-
* spell.suggest('speling');
|
|
231
|
-
*
|
|
232
|
-
* @param {word:string:required}
|
|
233
|
-
* the word you want to spell check
|
|
234
|
-
* @param {alphabet:array:optional}
|
|
235
|
-
* if you need to override checking for just words you can set this
|
|
236
|
-
* and it will enable you to make suggestions that include punctiation
|
|
237
|
-
* etc
|
|
238
|
-
*
|
|
239
|
-
* @return {array} ordered array containing json objects such as
|
|
240
|
-
* [{"word": "spelling", "score": 10}]
|
|
241
|
-
*/
|
|
242
|
-
function spell_suggest(word, alphabet) {
|
|
243
|
-
if (dict.hasOwnProperty(word)) {
|
|
244
|
-
return [{"word":word, "score": dict[word]}];
|
|
245
|
-
}
|
|
246
|
-
var edits1 = spell_edits(word, alphabet)
|
|
247
|
-
, candidates = {}
|
|
248
|
-
, min
|
|
249
|
-
, max
|
|
250
|
-
, current_count
|
|
251
|
-
;
|
|
252
|
-
function get_candidates(word) {
|
|
253
|
-
if(dict.hasOwnProperty(word)) {
|
|
254
|
-
current_count = dict[word];
|
|
255
|
-
if (candidates.hasOwnProperty(current_count)) {
|
|
256
|
-
candidates[current_count][word] = true;
|
|
257
|
-
} else {
|
|
258
|
-
candidates[current_count] = {};
|
|
259
|
-
candidates[current_count][word] = true;
|
|
260
|
-
}
|
|
261
|
-
max = max ? (max < current_count ? current_count : max) : current_count;
|
|
262
|
-
min = min ? (min > current_count ? current_count : min) : current_count;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
edits1.forEach(get_candidates);
|
|
266
|
-
if(!is_empty(candidates)) { return spell_order(candidates,min,max); }
|
|
267
|
-
edits1.forEach(function(edit1){
|
|
268
|
-
spell_edits(edit1, alphabet).forEach(get_candidates);
|
|
269
|
-
});
|
|
270
|
-
if(!is_empty(candidates)) { return spell_order(candidates,min,max); }
|
|
271
|
-
return []; // no suggestions
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/*
|
|
275
|
-
* feeling lucky
|
|
276
|
-
*
|
|
277
|
-
* returns the first spelling correction for a word
|
|
278
|
-
*
|
|
279
|
-
* e.g.
|
|
280
|
-
* spell.lucky('speling');
|
|
281
|
-
*
|
|
282
|
-
* @param {word:string:required}
|
|
283
|
-
* the word you want to spell check
|
|
284
|
-
* @param {alphabet:array:optional}
|
|
285
|
-
* if you need to override checking for just words you can set this
|
|
286
|
-
* and it will enable you to make suggestions that include punctiation
|
|
287
|
-
* etc
|
|
288
|
-
*
|
|
289
|
-
* @return {string} the most likely match
|
|
290
|
-
*/
|
|
291
|
-
function spell_lucky(word, alphabet) {
|
|
292
|
-
var suggest = spell_suggest(word, alphabet)[0];
|
|
293
|
-
if(suggest && suggest.hasOwnProperty("word")) {
|
|
294
|
-
return suggest.word;
|
|
295
|
-
}
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/*
|
|
300
|
-
* export
|
|
301
|
-
*
|
|
302
|
-
* exports the dictionary
|
|
303
|
-
*
|
|
304
|
-
* e.g.
|
|
305
|
-
* spell.export();
|
|
306
|
-
*
|
|
307
|
-
* @return {json} dictionary
|
|
308
|
-
*/
|
|
309
|
-
function spell_export(word) {
|
|
310
|
-
return {corpus: dict};
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
return { reset : spell_reset
|
|
314
|
-
, load : spell_load
|
|
315
|
-
, "export" : spell_export
|
|
316
|
-
, save : spell_export // alias
|
|
317
|
-
, add_word : spell_add_word
|
|
318
|
-
, addWord : spell_add_word // alias
|
|
319
|
-
, remove_word : spell_remove_word
|
|
320
|
-
, removeWord : spell_remove_word // alias
|
|
321
|
-
, suggest : spell_suggest
|
|
322
|
-
, lucky : spell_lucky
|
|
323
|
-
};
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
spell._previous = previous_spell;
|
|
327
|
-
if (typeof exports !== 'undefined') { // nodejs
|
|
328
|
-
spell.platform = { name: "node.js", version: process.version };
|
|
329
|
-
spell.version = JSON.parse(
|
|
330
|
-
require('fs').readFileSync(__dirname + "/package.json")).version;
|
|
331
|
-
spell.path = __dirname;
|
|
332
|
-
if (typeof module !== 'undefined' && module.exports) {
|
|
333
|
-
exports = module.exports = spell;
|
|
334
|
-
}
|
|
335
|
-
exports.spell = spell;
|
|
336
|
-
} else { // browser
|
|
337
|
-
// browser detection is possible in the future
|
|
338
|
-
spell.platform = { name: "browser" };
|
|
339
|
-
spell.version = "0.0.3";
|
|
340
|
-
if (typeof define === 'function' && define.amd) {
|
|
341
|
-
define('spell', function() { return spell; });
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
root.spell = spell;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
})();
|