de-compromise 0.0.9 → 0.1.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.
Files changed (85) hide show
  1. package/README.md +1 -1
  2. package/builds/de-compromise.cjs +2357 -1687
  3. package/builds/de-compromise.min.js +1 -1
  4. package/builds/de-compromise.mjs +1 -1
  5. package/package.json +21 -14
  6. package/src/01-one/lexicon/_data.js +32 -0
  7. package/src/01-one/lexicon/compute/root.js +55 -0
  8. package/src/01-one/lexicon/lexicon.js +95 -0
  9. package/src/01-one/lexicon/methods/_data.js +193 -0
  10. package/src/01-one/lexicon/methods/adjectives/_test-back.js +29 -0
  11. package/src/01-one/lexicon/methods/adjectives/_test.js +49 -0
  12. package/src/01-one/lexicon/methods/adjectives/inflect.js +27 -0
  13. package/src/01-one/lexicon/methods/adjectives/model.js +126 -0
  14. package/src/01-one/lexicon/methods/adjectives/new/adjective.js +8 -0
  15. package/src/01-one/lexicon/methods/adjectives/new/comparative.js +7 -0
  16. package/src/01-one/lexicon/methods/adjectives/new/superlative.js +159 -0
  17. package/src/01-one/lexicon/methods/adjectives/toRoot.js +47 -0
  18. package/src/01-one/lexicon/methods/index.js +56 -0
  19. package/src/01-one/lexicon/methods/models.js +10 -0
  20. package/src/01-one/lexicon/methods/nouns/_test-back.js +45 -0
  21. package/src/01-one/lexicon/methods/nouns/new.js +56 -0
  22. package/src/01-one/lexicon/methods/nouns/toPlural.js +49 -0
  23. package/src/01-one/lexicon/methods/nouns/toSingular.js +46 -0
  24. package/src/01-one/lexicon/methods/verbs/conjugate.js +87 -0
  25. package/src/01-one/lexicon/methods/verbs/custom/_test-from.js +53 -0
  26. package/src/01-one/lexicon/methods/verbs/custom/_test-to.js +50 -0
  27. package/src/01-one/lexicon/methods/verbs/custom/fromPastParticiple.js +37 -0
  28. package/src/01-one/lexicon/methods/verbs/custom/rules.js +182 -0
  29. package/src/01-one/lexicon/methods/verbs/custom/toPastParticiple.js +54 -0
  30. package/src/01-one/lexicon/methods/verbs/irregulars.js +202 -0
  31. package/src/01-one/lexicon/methods/verbs/toRoot.js +55 -0
  32. package/src/01-one/lexicon/misc.js +78 -0
  33. package/src/01-one/lexicon/plugin.js +14 -0
  34. package/src/01-one/splitter/build.js +20 -0
  35. package/src/01-one/splitter/compute/splitter.js +54 -0
  36. package/src/01-one/splitter/model/index.js +20 -0
  37. package/src/01-one/splitter/plugin.js +10 -0
  38. package/src/01-one/splitter/trie.js +40 -0
  39. package/src/01-one/tagset/plugin.js +11 -0
  40. package/src/01-one/tagset/tags/dates.js +45 -0
  41. package/src/01-one/tagset/tags/misc.js +70 -0
  42. package/src/01-one/tagset/tags/nouns.js +116 -0
  43. package/src/01-one/tagset/tags/values.js +47 -0
  44. package/src/01-one/tagset/tags/verbs.js +64 -0
  45. package/src/01-one/tokenizer/contractions.js +40 -0
  46. package/src/01-one/tokenizer/is-sentence.js +37 -0
  47. package/src/01-one/tokenizer/plugin.js +11 -0
  48. package/src/01-one/tokenizer/unicode.js +55 -0
  49. package/src/02-two/postTagger/matches.js +47 -0
  50. package/src/02-two/postTagger/plugin.js +8 -0
  51. package/src/02-two/preTagger/compute/1st-pass/regex.js +43 -0
  52. package/src/02-two/preTagger/compute/1st-pass/titlecase.js +21 -0
  53. package/src/02-two/preTagger/compute/1st-pass/year.js +54 -0
  54. package/src/02-two/preTagger/compute/2nd-pass/acronym.js +82 -0
  55. package/src/02-two/preTagger/compute/2nd-pass/fallback.js +27 -0
  56. package/src/02-two/preTagger/compute/2nd-pass/prefix-lookup.js +33 -0
  57. package/src/02-two/preTagger/compute/2nd-pass/suffix-lookup.js +44 -0
  58. package/src/02-two/preTagger/compute/index.js +42 -0
  59. package/src/02-two/preTagger/model/index.js +14 -0
  60. package/src/02-two/preTagger/model/prefixes.js +46 -0
  61. package/src/02-two/preTagger/model/regex/regex-normal.js +25 -0
  62. package/src/02-two/preTagger/model/regex/regex-numbers.js +62 -0
  63. package/src/02-two/preTagger/model/regex/regex-text.js +17 -0
  64. package/src/02-two/preTagger/model/suffixes.js +171 -0
  65. package/src/02-two/preTagger/plugin.js +12 -0
  66. package/src/03-three/adjectives/api.js +34 -0
  67. package/src/03-three/adjectives/plugin.js +5 -0
  68. package/src/03-three/nouns/api.js +74 -0
  69. package/src/03-three/nouns/plugin.js +5 -0
  70. package/src/03-three/numbers/api.js +259 -0
  71. package/src/03-three/numbers/data.js +77 -0
  72. package/src/03-three/numbers/format/index.js +27 -0
  73. package/src/03-three/numbers/format/toText.js +118 -0
  74. package/src/03-three/numbers/parse/fromText.js +52 -0
  75. package/src/03-three/numbers/parse/index.js +63 -0
  76. package/src/03-three/numbers/plugin.js +5 -0
  77. package/src/03-three/verbs/api.js +41 -0
  78. package/src/03-three/verbs/plugin.js +5 -0
  79. package/src/_lib.js +4 -0
  80. package/src/_version.js +1 -0
  81. package/src/index.js +53 -0
  82. package/types/index.d.ts +46 -0
  83. package/types/misc.ts +92 -0
  84. package/types/view/de.ts +61 -0
  85. package/types/view/one.ts +235 -0
package/src/index.js ADDED
@@ -0,0 +1,53 @@
1
+ import nlp from './_lib.js'
2
+ import lexicon from './01-one/lexicon/plugin.js'
3
+ import tagset from './01-one/tagset/plugin.js'
4
+ import tokenizer from './01-one/tokenizer/plugin.js'
5
+ import splitter from './01-one/splitter/plugin.js'
6
+ import tagger from './02-two/preTagger/plugin.js'
7
+ import postTagger from './02-two/postTagger/plugin.js'
8
+ import numbers from './03-three/numbers/plugin.js'
9
+ import verbs from './03-three/verbs/plugin.js'
10
+ import adjectives from './03-three/adjectives/plugin.js'
11
+ import nouns from './03-three/nouns/plugin.js'
12
+ import version from './_version.js'
13
+
14
+ nlp.plugin(tokenizer)
15
+ nlp.plugin(tagset)
16
+ nlp.plugin(lexicon)
17
+ nlp.plugin(tagger)
18
+ nlp.plugin(postTagger)
19
+ nlp.plugin(splitter)
20
+ nlp.plugin(numbers)
21
+ nlp.plugin(verbs)
22
+ nlp.plugin(adjectives)
23
+ nlp.plugin(nouns)
24
+
25
+ const de = function (txt, lex) {
26
+ let dok = nlp(txt, lex)
27
+ return dok
28
+ }
29
+
30
+ // copy constructor methods over
31
+ Object.keys(nlp).forEach(k => {
32
+ if (nlp.hasOwnProperty(k)) {
33
+ de[k] = nlp[k]
34
+ }
35
+ })
36
+
37
+ // this one is hidden
38
+ Object.defineProperty(de, '_world', {
39
+ value: nlp._world,
40
+ writable: true,
41
+ })
42
+ /** log the decision-making to console */
43
+ de.verbose = function (set) {
44
+ let env = typeof process === 'undefined' ? self.env || {} : process.env //use window, in browser
45
+ env.DEBUG_TAGS = set === 'tagger' || set === true ? true : ''
46
+ env.DEBUG_MATCH = set === 'match' || set === true ? true : ''
47
+ env.DEBUG_CHUNKS = set === 'chunker' || set === true ? true : ''
48
+ return this
49
+ }
50
+
51
+ de.version = version
52
+
53
+ export default de
@@ -0,0 +1,46 @@
1
+ import { Lexicon, Plugin, matchOptions, Match, Net } from './misc'
2
+ import View from './view/de'
3
+
4
+ /** parse a given text */
5
+ declare function nlp(text: string, lexicon?: Lexicon): View
6
+
7
+ // Constructor
8
+ declare module nlp {
9
+ /** interpret text without tagging */
10
+ export function tokenize(text: string, lexicon?: Lexicon): View
11
+ /** scan through text with minimal analysis */
12
+ export function lazy(text: string, match?: string): View
13
+ /** mix-in a compromise plugin */
14
+ export function plugin(plugin: Plugin): any
15
+ /** mix-in a compromise plugin */
16
+ export function extend(plugin: Plugin): any
17
+ /** turn a match-string into json */
18
+ export function parseMatch(match: string, opts?: matchOptions): object[]
19
+ /** grab library internals */
20
+ export function world(): object
21
+ /** grab library metadata */
22
+ export function model(): object
23
+ /** grab exposed library methods */
24
+ export function methods(): object
25
+ /** which compute functions run automatically */
26
+ export function hooks(): string[]
27
+ /** log our decision-making for debugging */
28
+ export function verbose(toLog?: boolean | string): any
29
+ /** current semver version of the library */
30
+ export const version: string
31
+ /** connect new tags to tagset graph */
32
+ export function addTags(tags: object): any
33
+ /** add new words to internal lexicon */
34
+ export function addWords(words: Lexicon): any
35
+ /** turn a list of words into a searchable graph */
36
+ export function buildTrie(words: string[]): object
37
+ /** compile a set of match objects to a more optimized form */
38
+ export function buildNet(matches: Match[]): Net
39
+ /** add words to the autoFill dictionary */
40
+ export function typeahead(words: Lexicon): any
41
+ /** export internal methods for plugins */
42
+ export interface TypedPlugin<Methods extends object> extends Plugin { methods: Methods }
43
+ }
44
+
45
+ export default nlp
46
+
package/types/misc.ts ADDED
@@ -0,0 +1,92 @@
1
+ export type Document = Term[][]
2
+
3
+ export type Pointer = [n?: number, start?: number, end?: number, startId?: string, endId?: string]
4
+
5
+ export type outMethods = 'text' | 'normal' | 'offset' | 'terms' | 'topk' | 'json' | 'tags' | 'array' | 'debug'
6
+
7
+ export type Groups = object
8
+
9
+ export interface Term {
10
+ text: string,
11
+ pre: string,
12
+ post: string,
13
+ normal: string,
14
+
15
+ // in /two
16
+ tags?: Set<string>,
17
+ index?: [n?: number, start?: number],
18
+ id?: string,
19
+ chunk?: string,
20
+ dirty?: boolean
21
+
22
+ // other things you may find...
23
+ syllables?: string[],
24
+ }
25
+
26
+ // possible values to .json()
27
+ export interface JsonProps {
28
+ /** a perfect copy of the input text */
29
+ text?: boolean
30
+ /** normalized whitespace, case, unicode, punctuation */
31
+ normal?: boolean
32
+ /** lowercase, trimmed, contractions expanded. */
33
+ reduced?: boolean
34
+ /** cleanup whitespace */
35
+ trim?: boolean
36
+ /** character-position where this begins */
37
+ offset?: boolean
38
+ /** frequency of this match in the document */
39
+ count?: boolean
40
+ /** remove duplicate results*/
41
+ unique?: boolean
42
+ /** starting term # in document */
43
+ index?: boolean
44
+ /** options for each term */
45
+ terms?: {
46
+ text?: boolean
47
+ normal?: boolean
48
+ clean?: boolean
49
+ implicit?: boolean
50
+ tags?: boolean
51
+ whitespace?: boolean
52
+ id?: boolean
53
+ offset?: boolean
54
+ bestTag?: boolean
55
+ }
56
+ }
57
+
58
+ // a key-value object of words, terms
59
+ export interface Lexicon {
60
+ [key: string]: string
61
+ }
62
+
63
+ export interface Plugin {
64
+ methods?: object,
65
+ model?: object,
66
+ compute?: object,
67
+ hooks?: string[],
68
+ tags?: object,
69
+ words?: object,
70
+ lib?: () => object,
71
+ api?: (fn: (view: any) => {}) => void, //should be View
72
+ mutate?: (fn: (world: object) => {}) => void,
73
+ }
74
+
75
+ export interface matchOptions {
76
+ fuzzy?: number,
77
+ caseSensitive?: boolean,
78
+ }
79
+
80
+ export interface Match {
81
+ match: string,
82
+ tag?: string | string[],
83
+ unTag?: string | string[],
84
+ group?: string | number,
85
+ reason?: string,
86
+ }
87
+
88
+ export interface Net {
89
+ hooks: object,
90
+ always?: any,
91
+ isNet: boolean
92
+ }
@@ -0,0 +1,61 @@
1
+ import View from './one'
2
+
3
+
4
+ interface Numbers extends View {
5
+ /** grab the parsed number */
6
+ parse: (n?: number) => object[]
7
+ /** grab the parsed number */
8
+ get: (n?: number) => number | number[]
9
+ /** grab 'kilos' from `25 kilos' */
10
+ // units: () => View
11
+ /** return only ordinal numbers */
12
+ isOrdinal: () => View
13
+ /** return only cardinal numbers */
14
+ isCardinal: () => View
15
+ /** convert number to `5` or `5th` */
16
+ toNumber: () => View
17
+ /** add commas, or nicer formatting for numbers */
18
+ toLocaleString: () => View
19
+ /** convert number to `five` or `fifth` */
20
+ toText: () => View
21
+ /** convert number to `five` or `5` */
22
+ toCardinal: () => View
23
+ /** convert number to `fifth` or `5th` */
24
+ toOrdinal: () => View
25
+ /** return numbers with this value */
26
+ isEqual: () => View
27
+ /** return numbers bigger than n */
28
+ greaterThan: (min: number) => View
29
+ /** return numbers smaller than n */
30
+ lessThan: (max: number) => View
31
+ /** return numbers between min and max */
32
+ between: (min: number, max: number) => View
33
+ /** set number to n */
34
+ set: (n: number) => View
35
+ /** increase number by n */
36
+ add: (n: number) => View
37
+ /** decrease number by n*/
38
+ subtract: (n: number) => View
39
+ /** increase number by 1 */
40
+ increment: () => View
41
+ /** decrease number by 1*/
42
+ decrement: () => View
43
+ }
44
+
45
+ interface Contractions extends View {
46
+ /** */
47
+ expand(): View
48
+ }
49
+
50
+
51
+
52
+ interface FrView extends View {
53
+ /** return any multi-word terms, like "didn't" */
54
+ contractions: (n?: number) => Contractions
55
+ /** */
56
+ numbers(): Numbers
57
+ /** */
58
+ topics(): View
59
+ }
60
+
61
+ export default FrView
@@ -0,0 +1,235 @@
1
+ import { Document, Pointer, Groups, JsonProps, outMethods, matchOptions, Term, Net } from '../misc'
2
+
3
+ class View {
4
+ // Utils
5
+ /** is this document empty? */
6
+ found: boolean
7
+ /** get a list of term objects for this view */
8
+ docs: Document
9
+ /** get the full parsed text */
10
+ document: Document
11
+ /** the indexes for the current view */
12
+ pointer: Pointer[] | null
13
+ /** explicit indexes for the current view */
14
+ fullPointer: Pointer[]
15
+ /** access internal library methods */
16
+ methods: object
17
+ /** access internal library data */
18
+ model: object
19
+ /** which compute methods run by default */
20
+ hooks: string[]
21
+ /** helper for detecting a compromise object */
22
+ isView: boolean
23
+ /** count the # of characters of each match */
24
+ length: number
25
+
26
+ /** create a new view, from this document */
27
+ update: (pointer: Pointer | null) => View
28
+ /** turn a Verb or Noun view into a normal one */
29
+ toView: (pointer: Pointer | null) => View
30
+ /** create a new document */
31
+ fromText: (text: string) => View
32
+ /** .docs [alias] */
33
+ termList: () => Term[]
34
+
35
+ /** run a named operation on the document */
36
+ compute: (method: string | string[]) => View
37
+ /** deep-copy the document, so that no references remain */
38
+ clone: (shallow?: boolean) => View
39
+
40
+ // Loops
41
+ /** run a function on each phrase, as an individual document */
42
+ forEach: (fn: (m: View) => void) => View
43
+ /** run each phrase through a function, and create a new document */
44
+ map: (fn: (m: View) => any, emptyResult?: any) => View | []
45
+ /** return only the phrases that return true */
46
+ filter: (fn: (m: View) => boolean) => View
47
+ /** return a document with only the first phrase that matches */
48
+ find: (fn: (m: View) => boolean) => View | undefined
49
+ /** return true or false if there is one matching phrase */
50
+ some: (fn: (m: View) => boolean) => View
51
+ /** sample a subset of the results */
52
+ random: (n?: number) => View
53
+
54
+ // Accessors
55
+ /** split-up results by each individual term */
56
+ terms: (n?: number) => View
57
+ /** grab a specific named capture group */
58
+ groups: (name?: string) => View | Groups
59
+ /** use only the nth result */
60
+ eq: (n: number) => View
61
+ /** use only the first result(s) */
62
+ first: (n?: number) => View
63
+ /** use only the last result(s) */
64
+ last: (n?: number) => View
65
+ /** get the first word in each match */
66
+ firstTerms: () => View
67
+ /** get the end word in each match */
68
+ lastTerms: () => View
69
+ /** grab a subset of the results */
70
+ slice: (start: number, end?: number) => View
71
+ /** return the whole original document ('zoom out') */
72
+ all: () => View
73
+ /** return the full original sentence for each match */
74
+ fullSentences: () => View
75
+ /** return an empty view */
76
+ none: () => View
77
+ /** are these two views of the same document? */
78
+ isDoc: (view?: View) => boolean
79
+ /** count the # of terms in each match */
80
+ wordCount: () => number
81
+
82
+ // Match
83
+ /** return matching patterns in this doc */
84
+ match: (match: string | View | Net, group?: string | number, options?: matchOptions) => View
85
+ /** return only the first match */
86
+ matchOne: (match: string | View | Net, group?: string | number, options?: matchOptions) => View
87
+ /** Return a boolean if this match exists */
88
+ has: (match: string | View | Net, group?: string | number, options?: matchOptions) => boolean
89
+ /** return each current phrase, only if it contains this match */
90
+ if: (match: string | View | Net, group?: string | number, options?: matchOptions) => View
91
+ /** Filter-out any current phrases that have this match */
92
+ ifNo: (match: string | View | Net, group?: string | number, options?: matchOptions) => View
93
+
94
+ /** return the terms before each match */
95
+ before: (match: string | View, group?: string | number, options?: matchOptions) => View
96
+ /** return the terms after each match */
97
+ after: (match: string | View, group?: string | number, options?: matchOptions) => View
98
+ /** add any immediately-preceding matches to the view*/
99
+ growLeft: (match: string | View, group?: string | number, options?: matchOptions) => View
100
+ /** add any immediately-following matches to the view*/
101
+ growRight: (match: string | View, group?: string | number, options?: matchOptions) => View
102
+ /** expand the view with any left-or-right matches*/
103
+ grow: (match: string | View, group?: string | number, options?: matchOptions) => View
104
+ /** apply a sequence of match objects to the document */
105
+ sweep: (match: Net, opts?: object) => { view: View, found: object[] }
106
+
107
+ /** .split() [alias] */
108
+ splitOn: (match?: string, group?: string | number) => View
109
+ /** separate everything after the match as a new phrase */
110
+ splitBefore: (match?: string, group?: string | number) => View
111
+ /** separate everything before the word, as a new phrase */
112
+ splitAfter: (match?: string, group?: string | number) => View
113
+ /** splitAfter() alias */
114
+ split: (match?: string, group?: string | number) => View
115
+
116
+ // Case
117
+ /** turn every letter of every term to lower-cse */
118
+ toLowerCase: () => View
119
+ /** turn every letter of every term to upper case */
120
+ toUpperCase: () => View
121
+ /** upper-case the first letter of each term */
122
+ toTitleCase: () => View
123
+ /** remove whitespace and title-case each term */
124
+ toCamelCase: () => View
125
+
126
+ // Insert
127
+ /** add these new things to the end */
128
+ concat: (input: string | View) => View
129
+ /** add these words before each match */
130
+ insertBefore: (input: string | View) => View
131
+ /** insertBefore() alias */
132
+ prepend: (input: string | View) => View
133
+ /** add these words after each match */
134
+ insertAfter: (text: string | View) => View
135
+ /** insertAfter() alias */
136
+ append: (text: string | View) => View
137
+ /** insertAfter() alias */
138
+ insert: (text: string | View) => View
139
+ /** fully remove these terms from the document */
140
+ remove: (match: string | View) => View
141
+ /** alias for .remove() */
142
+ delete: (match: string | View) => View
143
+ /** search and replace match with new content */
144
+ replace: (from: string | View, to?: string | Function, keep?: object) => View
145
+ /** substitute-in new content */
146
+ replaceWith: (to: string | Function, keep?: object) => View
147
+
148
+ /** remove any duplicate matches */
149
+ unique: () => View
150
+ /**reverse the order of the matches, but not the words */
151
+ reverse: () => View
152
+ /**re-arrange the order of the matches (in place) */
153
+ sort: (method?: string | Function) => View
154
+
155
+ // Whitespace
156
+ /** add this punctuation or whitespace before each match */
157
+ pre: (str?: string, concat?: boolean) => View
158
+ /** add this punctuation or whitespace after each match */
159
+ post: (str?: string, concat?: boolean) => View
160
+ /** remove start and end whitespace */
161
+ trim: () => View
162
+ /** connect words with hyphen, and remove whitespace */
163
+ hyphenate: () => View
164
+ /** remove hyphens between words, and set whitespace */
165
+ dehyphenate: () => View
166
+ /** alias for .dehyphenate() */
167
+ deHyphenate: () => View
168
+ /** add quotation marks around selections */
169
+ toQuotations: (start?: string, end?: string) => View
170
+ /** alias for toQuotations() */
171
+ toQuotation: (start?: string, end?: string) => View
172
+ /** add parentheses around selections */
173
+ toParentheses: (start?: string, end?: string) => View
174
+
175
+
176
+ // Output
177
+ /** return the document as text */
178
+ text: (options?: string | object) => string
179
+ /** pull out desired metadata from the document */
180
+ json: (options?: JsonProps | string) => any
181
+ /** pretty-print the current document and its tags */
182
+ debug: () => View
183
+ /** some named output formats */
184
+ out: (format?: outMethods) => any
185
+ /** produce an html string */
186
+ html: (toHighlight: object) => string
187
+ /** produce an html string */
188
+ wrap: (matches: object) => string
189
+
190
+
191
+ // ### Pointers
192
+ /** return all matches without duplicates */
193
+ union: (match: string | View) => View
194
+ /** .union() alias */
195
+ and: (match: string | View) => View
196
+ /** return only duplicate matches */
197
+ intersection: (match: string | View) => View
198
+ /** return all results except for this */
199
+ not: (match: string | View, options?: any) => View
200
+ /** .not() alias */
201
+ difference: (match: string | View, options?: any) => View
202
+ /** get everything that is not a match */
203
+ complement: (match: string | View) => View
204
+ /** remove overlaps in matches */
205
+ settle: (match: string | View) => View
206
+
207
+
208
+ // Tag
209
+ /** Give all terms the given tag */
210
+ tag: (tag: string, reason?: string) => View
211
+ /** Only apply tag to terms if it is consistent with current tags */
212
+ tagSafe: (tag: string, reason?: string) => View
213
+ /** Remove this term from the given terms */
214
+ unTag: (tag: string, reason?: string) => View
215
+ /** return only the terms that can be this tag */
216
+ canBe: (tag: string) => View
217
+
218
+ // Cache
219
+ /** freeze the current state of the document, for speed-purposes */
220
+ cache: (options?: object) => View
221
+ /** un-freezes the current state of the document, so it may be transformed */
222
+ uncache: (options?: object) => View
223
+ /*
224
+ harden: () => View
225
+ soften: () => View
226
+ */
227
+
228
+ /** quick find for an array of string matches */
229
+ lookup: (trie: object | string[], opts?: object) => View
230
+ /** assume any type-ahead prefixes */
231
+ autoFill: () => View
232
+
233
+ }
234
+
235
+ export default View