1- const bydictionary = require ( './bydictionary.json' ) ;
2- const bypattern = require ( './bypattern.js' ) ;
3-
4- const spellingVariations = function ( word ) {
5- this . data = analyse ( word ) ;
6- } ;
7-
8- // @return {Number } how common this variation in the UK's texts (1-0)
9- spellingVariations . prototype . scoreUK = function ( ) { return this . data . scoreUK ; } ;
10- // @return {Number } how common this variation in the US's texts (1-0)
11- spellingVariations . prototype . scoreUS = function ( ) { return this . data . scoreUS ; } ;
12- // @return {Boolean } the word has variations
13- spellingVariations . prototype . hasVariations = function ( ) { return this . data . hasVariations ; } ;
14- // @return {Array } US variations of the word
15- spellingVariations . prototype . USVariations = function ( ) { return this . data . USVariations ; } ;
16- // @return {Array } UK variations of the word
17- spellingVariations . prototype . UKVariations = function ( ) { return this . data . UKVariations ; } ;
18- // @return {String } UK's preferred variation
19- spellingVariations . prototype . UKPrefered = function ( ) { return this . data . UKPrefered ; } ;
20- // @return {String } US's preferred variation
21- spellingVariations . prototype . USPrefered = function ( ) { return this . data . USPrefered ; } ;
22- // @return {Array } All of the word's variations
23- spellingVariations . prototype . variations = function ( ) { return this . data . variations ; } ;
24- // @return {String } UK and US common variation
25- spellingVariations . prototype . commonVariation = function ( ) { return this . data . commonVariation ; } ;
26- // @return {String } converts the word spelling to it's UK variant
27- spellingVariations . prototype . toUK = function ( ) { return this . data . UKPrefered || this . data . word ; } ;
28- // @return {String } converts the word spelling to it's US variant
29- spellingVariations . prototype . toUS = function ( ) { return this . data . USPrefered || this . data . word ; } ;
30- // @return {Object } all the info above
31- spellingVariations . prototype . analyse = function ( ) { return this . data ; } ;
32- // a us alias for the above function :)
33- spellingVariations . prototype . analyze = function ( ) { return this . data ; } ;
34-
35-
36- /**
37- *
38- * This little guy here is actually the one who does all the heavy
39- * lifting of finding the variations and the class and such..
40- *
41- **/
42- function analyse ( word ) {
43-
44- word = ( word || "" ) . toLowerCase ( ) ;
45-
46- const result = {
47- word,
48- scoreUK :- 1 ,
49- scoreUS :- 1 ,
50- hasVariations :false ,
51- UKPrefered :word ,
52- USPrefered :word ,
53- commonVariation :word ,
54- UKVariations :[ ] ,
55- USVariations :[ ] ,
56- variations :[ ] ,
57- analyse :analyse ,
58- analyze :analyse
59- } ;
60-
61- var resultArr = [ ] ;
62- var dictionaryEntry = bydictionary [ word ] ;
63- var patternEntry = bypattern ( word ) ;
64- if ( dictionaryEntry ) resultArr = dictionaryEntry . split ( "|" ) ;
65- else if ( patternEntry ) resultArr = patternEntry ;
66- else return result ;
67-
68- // resultArr reference:
69- // 0: UK1 4: US1
70- // 1: UK2 5: US2
71- // 2: UK3 6: US3
72- // 3: UK4 7: US4 8:UKUS
73-
74-
75- result . hasVariations = true ;
76- result . variations = filterOut ( resultArr , word ) ;
77- result . UKPrefered = resultArr [ 0 ] ;
78- result . USPrefered = resultArr [ 4 ] ;
79- result . commonVariation = resultArr [ 8 ] || "" ;
80- result . UKVariations = resultArr . filter ( ( e , i ) => e && ( i < 4 || i === 8 ) && e !== word ) ;
81- result . USVariations = resultArr . filter ( ( e , i ) => e && ( i > 3 || i === 8 ) && e !== word ) ;
82-
83- if ( resultArr . indexOf ( word ) === 8 ) {
84- result . scoreUK = 0.87 ;
85- result . scoreUS = 0.87 ;
86- }
87-
88- else {
89- var UKi = resultArr . slice ( 0 , 4 ) . indexOf ( word ) ;
90- var USi = resultArr . slice ( 4 , 8 ) . indexOf ( word ) ;
91-
92- if ( UKi === - 1 ) result . scoreUK = 0 ;
93- else result . scoreUK = ( 4 - UKi ) * 0.25 ;
94-
95- if ( USi === - 1 ) result . scoreUS = 0 ;
96- else result . scoreUS = ( 4 - USi ) * 0.25 ;
97- }
98-
99- return result ;
1+ import bypattern from './bypattern.js' ;
2+ import fs from 'fs' ;
3+
4+ import path from 'path' ;
5+ import { exit } from 'process' ;
6+ import { fileURLToPath } from 'url' ;
7+
8+ const __filename = fileURLToPath ( import . meta. url ) ;
9+
10+ const __dirname = path . dirname ( __filename ) ;
11+
12+ function readJsonFile ( filePath ) {
13+ try {
14+ const data = fs . readFileSync ( filePath , 'utf8' ) ;
15+ return JSON . parse ( data ) ;
16+ } catch ( error ) {
17+ console . error ( `Error reading JSON file: ${ error } ` ) ;
18+ exit ( - 1 ) ;
19+ }
10020}
10121
102- function filterOut ( arr , word ) {
103- return arr . filter ( ( x ) => x && x !== word ) ;
22+ const bydictionary = readJsonFile ( path . join ( __dirname , 'bydictionary.json' ) ) ;
23+
24+ class SpellingVariations {
25+ constructor ( word ) {
26+ this . data = this . analyse ( word ) ;
27+ }
28+
29+ // @return {Number } how common this variation is in the UK's texts (1-0)
30+ scoreUK ( ) {
31+ return this . data . scoreUK ;
32+ }
33+
34+ // @return {Number } how common this variation is in the US's texts (1-0)
35+ scoreUS ( ) {
36+ return this . data . scoreUS ;
37+ }
38+
39+ // @return {Boolean } the word has variations
40+ hasVariations ( ) {
41+ return this . data . hasVariations ;
42+ }
43+
44+ // @return {Array } US variations of the word
45+ USVariations ( ) {
46+ return this . data . USVariations ;
47+ }
48+
49+ // @return {Array } UK variations of the word
50+ UKVariations ( ) {
51+ return this . data . UKVariations ;
52+ }
53+
54+ // @return {String } UK's preferred variation
55+ UKPreferred ( ) {
56+ return this . data . UKPreferred ;
57+ }
58+
59+ // @return {String } US's preferred variation
60+ USPreferred ( ) {
61+ return this . data . USPreferred ;
62+ }
63+
64+ // @return {Array } All of the word's variations
65+ variations ( ) {
66+ return this . data . variations ;
67+ }
68+
69+ // @return {String } UK and US common variation
70+ commonVariation ( ) {
71+ return this . data . commonVariation ;
72+ }
73+
74+ // @return {String } converts the word spelling to its UK variant
75+ toUK ( ) {
76+ return this . data . UKPreferred || this . data . word ;
77+ }
78+
79+ // @return {String } converts the word spelling to its US variant
80+ toUS ( ) {
81+ return this . data . USPreferred || this . data . word ;
82+ }
83+
84+ // @return {Object } all the info above
85+ analyse ( ) {
86+ return this . data ;
87+ }
88+
89+ // a US alias for the above function :)
90+ analyze ( ) {
91+ return this . data ;
92+ }
93+
94+ /**
95+ *
96+ * This little guy here is actually the one who does all the heavy
97+ * lifting of finding the variations and the class and such..
98+ *
99+ **/
100+ analyse ( word ) {
101+ word = ( word || "" ) . toLowerCase ( ) ;
102+
103+ const result = {
104+ word,
105+ scoreUK : - 1 ,
106+ scoreUS : - 1 ,
107+ hasVariations : false ,
108+ UKPreferred : word ,
109+ USPreferred : word ,
110+ commonVariation : word ,
111+ UKVariations : [ ] ,
112+ USVariations : [ ] ,
113+ variations : [ ] ,
114+ analyse : this . analyse ,
115+ analyze : this . analyse
116+ } ;
117+
118+ var resultArr = [ ] ;
119+ var dictionaryEntry = bydictionary [ word ] ;
120+ var patternEntry = bypattern ( word ) ;
121+ if ( dictionaryEntry ) resultArr = dictionaryEntry . split ( "|" ) ;
122+ else if ( patternEntry ) resultArr = patternEntry ;
123+ else return result ;
124+
125+ // resultArr reference:
126+ // 0: UK1 4: US1
127+ // 1: UK2 5: US2
128+ // 2: UK3 6: US3
129+ // 3: UK4 7: US4 8:UKUS
130+
131+ result . hasVariations = true ;
132+ result . variations = this . filterOut ( resultArr , word ) ;
133+ result . UKPreferred = resultArr [ 0 ] ;
134+ result . USPreferred = resultArr [ 4 ] ;
135+ result . commonVariation = resultArr [ 8 ] || "" ;
136+ result . UKVariations = resultArr . filter ( ( e , i ) => e && ( i < 4 || i === 8 ) && e !== word ) ;
137+ result . USVariations = resultArr . filter ( ( e , i ) => e && ( i > 3 || i === 8 ) && e !== word ) ;
138+
139+ if ( resultArr . indexOf ( word ) === 8 ) {
140+ result . scoreUK = 0.87 ;
141+ result . scoreUS = 0.87 ;
142+ } else {
143+ var UKi = resultArr . slice ( 0 , 4 ) . indexOf ( word ) ;
144+ var USi = resultArr . slice ( 4 , 8 ) . indexOf ( word ) ;
145+
146+ if ( UKi === - 1 ) result . scoreUK = 0 ;
147+ else result . scoreUK = ( 4 - UKi ) * 0.25 ;
148+
149+ if ( USi === - 1 ) result . scoreUS = 0 ;
150+ else result . scoreUS = ( 4 - USi ) * 0.25 ;
151+ }
152+
153+ return result ;
154+ }
155+
156+ filterOut ( arr , word ) {
157+ return arr . filter ( ( x ) => x && x !== word ) ;
158+ }
104159}
105160
106- module . exports = spellingVariations ;
161+ export default SpellingVariations ;
0 commit comments