theprogrammablemind_4wp 7.6.0-beta.6 → 7.6.0-beta.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/client.js +35 -28
  2. package/package.json +1 -1
  3. package/src/generators.js +112 -122
package/client.js CHANGED
@@ -173,22 +173,32 @@ const setupArgs = (args, config, logs, hierarchy) => {
173
173
  args.theDebugger = {
174
174
  breakOnSemantics: (value) => args.breakOnSemantics = value
175
175
  }
176
- args.s = (c) => config.getSemantics(logs).apply(args, c)
177
- args.g = (c) => config.getGenerators(logs).apply(args, c)
178
- args.gp = (c) => config.getGenerators(logs).apply(args, { ...c, paraphrase: true, isResponse: false, response: false})
179
- args.gr = (c) => config.getGenerators(logs).apply(args, { ...c, paraphrase: false, isResponse: true })
180
176
  if (!logs) {
181
177
  debugger
182
178
  }
183
- args.e = (c) => config.getEvaluator(args.s, args.calls, logs, c)
184
179
  args.log = (message) => logs.push(message)
185
- args.gs = gs(args.g)
186
- args.gsp = gs(args.gp)
187
- args.gsr = gs(args.gr)
180
+
181
+ args.addAssumedScoped = (args, assumed) => {
182
+ args.s = (c) => config.getSemantics(logs).apply(args, c)
183
+ args.g = (c) => config.getGenerators(logs).apply(args, c)
184
+ args.gp = (c) => config.getGenerators(logs).apply({...args, assumed: {paraphrase: true, isResponse: false, response: false}}, c, {paraphrase: true, isResponse: false, response: false})
185
+ args.gr = (c) => config.getGenerators(logs).apply({...args, assumed: {paraphrase: false, isResponse: true}}, { ...c, paraphrase: false, isResponse: true })
186
+ args.e = (c) => config.getEvaluator(args.s, args.calls, logs, c)
187
+ args.gs = gs(args.g)
188
+ args.gsp = gs(args.gp)
189
+ args.gsr = gs(args.gr)
190
+ }
191
+ // for semantics
192
+ args.addAssumedScoped(args, {})
188
193
  config.getAddedArgs(args)
189
194
  }
190
195
 
191
196
  const gs = (g) => (contexts, separator, lastSeparator) => {
197
+ if (!Array.isArray(contexts)) {
198
+ debugger
199
+ throw new Error("Expected a list")
200
+ }
201
+
192
202
  let s = ''
193
203
  if (!separator) {
194
204
  separator = ' '
@@ -276,8 +286,9 @@ const processContext = (context, { objects = {}, config, logs = [] }) => {
276
286
  setupArgs(args, config, logs, hierarchy)
277
287
 
278
288
  context = semantics.apply(args, context)
279
- const generated = generators.apply(args, context)[0]
280
- const paraphrases = generators.apply(args, context, { paraphrase: true, response: false, isResponse: false })[0]
289
+ const generated = generators.apply(args, context)
290
+ const assumed = { paraphrase: true, response: false, isResponse: false }
291
+ const paraphrases = generators.apply({...args, assumed}, context, { paraphrase: true, response: false, isResponse: false })
281
292
  let responses = []
282
293
  if (context.isResponse) {
283
294
  responses = generated
@@ -447,37 +458,29 @@ const processContextsB = ({ config, hierarchy, semantics, generators, json, isTe
447
458
  continue
448
459
  }
449
460
  let assumed = { isResponse: true };
450
- const generated = contextPrime.isResponse ? config.getGenerators(json.logs).apply(args, contextPrime, assumed)[0] : ''
461
+ const generated = contextPrime.isResponse ? config.getGenerators(json.logs).apply({...args, assumed}, contextPrime, assumed) : ''
451
462
  let generatedParenthesized = []
452
463
  if (generateParenthesized) {
453
464
  config.parenthesized = true
454
- generatedParenthesized = contextPrime.isResponse ? config.getGenerators(json.logs).apply(args, contextPrime, assumed)[0] : ''
465
+ generatedParenthesized = contextPrime.isResponse ? config.getGenerators(json.logs).apply({...args, assumed}, contextPrime, assumed) : ''
455
466
  config.parenthesized = false
456
467
  }
457
468
  // assumed = { paraphrase: true, response: false };
458
- assumed = { paraphrase: true };
459
- args.g = (c) => config.getGenerators(json.logs).apply(args, c, assumed)
460
- args.gp = (c) => config.getGenerators(json.logs).apply(args, {...c, paraphrase: true, isResponse: false, response: false }, assumed)
461
- args.gr = (c) => config.getGenerators(json.logs).apply(args, {...c, paraphrase: false }, assumed)
462
- args.gs = gs(args.g)
463
- args.gsp = gs(args.gsp)
464
- args.gsr = gs(args.gr)
469
+ assumed = { paraphrase: true, isResponse: false, response: false };
470
+ // args.g = (c) => config.getGenerators(json.logs).apply({...args, assumed}, c, assumed)
471
+ // args.gs = gs(args.g)
465
472
  if (generateParenthesized) {
466
473
  config.parenthesized = false
467
474
  }
468
- const paraphrases = config.getGenerators(json.logs).apply(args, contextPrime, assumed)[0]
475
+ const paraphrases = config.getGenerators(json.logs).apply({...args, assumed}, contextPrime, assumed)
469
476
  let paraphrasesParenthesized = []
470
477
  if (generateParenthesized) {
471
478
  config.parenthesized = true
472
- paraphrasesParenthesized = config.getGenerators(json.logs).apply(args, contextPrime, assumed)[0]
479
+ paraphrasesParenthesized = config.getGenerators(json.logs).apply({...args, assumed}, contextPrime, assumed)
473
480
  config.parenthesized = false
474
481
  }
475
- args.g = (c) => config.getGenerators(json.logs).apply(args, c)
476
- args.gp = (c) => config.getGenerators(json.logs).apply(args, {...c, paraphrase: true, isResponse: false, response: false })
477
- args.gr = (c) => config.getGenerators(json.logs).apply(args, {...c, paraphrase: false })
478
- args.gs = gs(args.g)
479
- args.gsp = gs(args.gp)
480
- args.gsr = gs(args.gr)
482
+ // args.g = (c) => config.getGenerators(json.logs).apply(args, c)
483
+ // args.gs = gs(args.g)
481
484
  contextsPrime.push(contextPrime)
482
485
  generatedPrime.push(generated)
483
486
  paraphrasesPrime.push(paraphrases)
@@ -1794,12 +1797,16 @@ const knowledgeModule = async ({
1794
1797
  show('paraphrases', result.expected.paraphrases, result.actual.paraphrases)
1795
1798
  if (!args.testNoParenthesized) {
1796
1799
  show('paraphrases parenthesized', result.expected.paraphrasesParenthesized, result.actual.paraphrasesParenthesized)
1800
+ if (args.vimdiff) {
1801
+ vimdiff(result.actual.paraphrasesParenthesized, result.expected.paraphrasesParenthesized)
1802
+ }
1803
+ }
1804
+ /*
1797
1805
  }
1798
1806
  show('responses', result.expected.responses, result.actual.responses)
1799
1807
  if (!args.testNoParenthesized) {
1800
1808
  show('responses parenthesized', result.expected.generatedParenthesized, result.actual.generatedParenthesized)
1801
1809
  }
1802
- /*
1803
1810
  if (JSON.stringify(result.expected.paraphrases) !== JSON.stringify(result.actual.paraphrases)) {
1804
1811
  if (!headerShown) {
1805
1812
  console.log(' Failure')
package/package.json CHANGED
@@ -64,6 +64,6 @@
64
64
  "json-stable-stringify": "^1.0.1",
65
65
  "node-fetch": "^2.6.1"
66
66
  },
67
- "version": "7.6.0-beta.6",
67
+ "version": "7.6.0-beta.8",
68
68
  "license": "ISC"
69
69
  }
package/src/generators.js CHANGED
@@ -73,7 +73,8 @@ class Generator {
73
73
  return matches
74
74
  }
75
75
 
76
- apply (baseArgs, objects, g, gs, context, hierarchy, config, response, log, options = {}) {
76
+ // apply (baseArgs, objects, g, gs, context, hierarchy, config, response, log, options = {}) {
77
+ apply (baseArgs, objects, context, hierarchy, config, response, log, options = {}) {
77
78
  if (!log) {
78
79
  throw new Error('generators.apply argument log is required')
79
80
  }
@@ -103,12 +104,12 @@ class Generator {
103
104
  log,
104
105
  global:
105
106
  objects,
106
- g,
107
+ // g,
107
108
  n,
108
109
  hierarchy,
109
110
  context,
110
111
  uuid: this.uuid,
111
- gs,
112
+ // gs,
112
113
  config,
113
114
  response,
114
115
  api: this.getAPI(config),
@@ -165,136 +166,125 @@ class Generators {
165
166
  this.logs = logs
166
167
  };
167
168
 
168
- // assumed - properties added to contexts before the generators are called. For setting paraphrase property
169
- apply (args, contexts, assumed = {}, options = {}) {
169
+ // assumed - properties added to context before the generators are called. For setting paraphrase property
170
+ apply (args, context, assumed = {}, options = {}) {
171
+ if (Array.isArray(context)) {
172
+ return new Error("Expected a context not an array")
173
+ }
174
+ if (typeof context !== 'object') {
175
+ return String(context)
176
+ }
177
+
170
178
  const config = args.config
171
179
  const objects = args.objects
172
180
  const hierarchy = args.hierarchy
173
181
  const response = args.response
174
- if (Array.isArray(contexts)) {
175
- // no-op
176
- } else if (typeof contexts === 'object') {
177
- contexts = [contexts]
178
- } else {
179
- return String(contexts)
180
- }
181
182
 
182
- const contextsPrime = []
183
- for (const icontext in contexts) {
184
- let context = contexts[icontext]
185
- context = Object.assign({}, context, assumed)
186
- // let context_prime = JSON.stringify(context);
187
- let generated = context
188
- let applied = false
189
- const stack = args.calls.push()
190
- for (let igenerator = 0; igenerator < this.generators.length; ++igenerator) {
191
- const generator = this.generators[igenerator]
192
- if (generator.matches(args, objects, context, hierarchy, config, options)) {
193
- const g = (context, options) => {
194
- const r = this.apply(args, context, Object.assign({}, ((options||{}).assumed || {}), assumed), options)
195
- if (Array.isArray(r)) {
196
- return r.join(' ')
197
- } else {
198
- return r
199
- }
200
- }
201
- const log = (message) => { this.logs.push(message) }
202
- // this.logs.push(`Generators: applied ${generator.toString()}\n to\n ${JSON.stringify(context)}`)
203
- let errorMessage = 'The apply function did not return a value'
204
- try {
205
- generated= generator.apply(args, objects, g, args.gs, context, hierarchy, config, response, log)
206
- } catch( e ) {
207
- // the next if handle this
208
- generated = null
209
- e.retryCall = () => generator.apply(args, objects, g, args.gs, context, hierarchy, config, response, log)
210
- const help = 'The error has a retryCall property that will recall the function that failed.'
211
- if (e.stack && e.message) {
212
- const info = `${generator.notes ? generator.notes : ''}${generator.where ? generator.where : ''}`
213
- errorMessage = `Error applying generator '${info}. Error is ${e.toString()} stack is ${e.stack}. Generator is ${generator.toString()}. ${help}`
214
- } else if (e.error) {
215
- const info = `${generator.notes ? generator.notes : ''}${generator.where ? generator.where : ''}`
216
- errorMessage = `Error applying generator '${info}. Error is ${e.error.join()}. Generator is ${generator.toString()}. ${help}`
217
- } else {
218
- errorMessage = e.toString()
219
- }
183
+
184
+ // args = { ...args, ...args.getAssumedScoped(assumed) }
185
+ args.addAssumedScoped(args, assumed)
186
+ context = Object.assign({}, context, args.assumed)
187
+ let generated = ''
188
+ let applied = false
189
+ const stack = args.calls.push()
190
+ for (let igenerator = 0; igenerator < this.generators.length; ++igenerator) {
191
+ const generator = this.generators[igenerator]
192
+ if (generator.matches(args, objects, context, hierarchy, config, options)) {
193
+ const log = (message) => { this.logs.push(message) }
194
+ // this.logs.push(`Generators: applied ${generator.toString()}\n to\n ${JSON.stringify(context)}`)
195
+ let errorMessage = 'The apply function did not return a value'
196
+ try {
197
+ generated= generator.apply(args, objects, context, hierarchy, config, response, log)
198
+ } catch( e ) {
199
+ // the next if handle this
200
+ generated = null
201
+ e.retryCall = () => generator.apply(args, objects, context, hierarchy, config, response, log)
202
+ const help = 'The error has a retryCall property that will recall the function that failed.'
203
+ if (e.stack && e.message) {
204
+ const info = `${generator.notes ? generator.notes : ''}${generator.where ? generator.where : ''}`
205
+ errorMessage = `Error applying generator '${info}. Error is ${e.toString()} stack is ${e.stack}. Generator is ${generator.toString()}. ${help}`
206
+ } else if (e.error) {
207
+ const info = `${generator.notes ? generator.notes : ''}${generator.where ? generator.where : ''}`
208
+ errorMessage = `Error applying generator '${info}. Error is ${e.error.join()}. Generator is ${generator.toString()}. ${help}`
209
+ } else {
210
+ errorMessage = e.toString()
220
211
  }
221
- if (!generated && generated !== '') {
222
- const widths = [10, 10, 90]
223
- const lines = new Lines(widths)
224
- lines.setElement(0, 0, 'Generator')
225
- const source = `${generator.km}/#${generator.index}`
226
- lines.setElement(0, 2, `ERROR while applying (${source}) ${generator.toLabel()}`)
227
- lines.newRow()
228
- lines.setElement(0, 2, generator.toString())
229
- lines.newRow()
230
- lines.setElement(0, 1, 'TO')
231
- lines.setElement(0, 2, `(HASHCODE ${helpers.hashCode(JSON.stringify(helpers.sortJson(context, { depth: 25 })))})`)
232
- lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
233
- lines.newRow()
234
- lines.setElement(0, 1, 'STACK')
235
- lines.setElement(0, 2, stack)
236
- lines.newRow()
237
- lines.setElement(0, 1, 'DEBUG')
238
- lines.setElement(0, 2, `To debug this use args.callId == '${args.calls.current()}'`)
239
- lines.newRow()
240
- lines.setElement(0, 1, 'ERROR')
241
- lines.setElement(0, 2, errorMessage)
242
- this.logs.push(lines.toString())
243
- const message = `ERROR while applying (${source}) ${generator.toLabel()}\n to\n ${JSON.stringify(context, null, 2)}.\n${errorMessage}'`
244
- // this.logs.push(message)
245
- // return [message]
246
- args.calls.pop()
247
- throw { error: [message], logs: this.logs }
248
- }
249
- if (((config || {}).config || {}).debug) {
250
- const widths = [10, 10, 90]
251
- const lines = new Lines(widths)
252
- lines.setElement(0, 0, 'Generator')
253
- if (generator.index > -1 && generator.km) {
254
- // lines.setElement(0, 2, `KM '${generator.km}' ordinal: ${generator.index}`)
255
- lines.setElement(0, 2, generator.toLabel())
256
- }
257
- lines.newRow()
258
- lines.setElement(0, 1, 'APPLIED')
212
+ }
213
+ if (!generated && generated !== '') {
214
+ const widths = [10, 10, 90]
215
+ const lines = new Lines(widths)
216
+ lines.setElement(0, 0, 'Generator')
217
+ const source = `${generator.km}/#${generator.index}`
218
+ lines.setElement(0, 2, `ERROR while applying (${source}) ${generator.toLabel()}`)
219
+ lines.newRow()
220
+ lines.setElement(0, 2, generator.toString())
221
+ lines.newRow()
222
+ lines.setElement(0, 1, 'TO')
223
+ lines.setElement(0, 2, `(HASHCODE ${helpers.hashCode(JSON.stringify(helpers.sortJson(context, { depth: 25 })))})`)
224
+ lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
225
+ lines.newRow()
226
+ lines.setElement(0, 1, 'STACK')
227
+ lines.setElement(0, 2, stack)
228
+ lines.newRow()
229
+ lines.setElement(0, 1, 'DEBUG')
230
+ lines.setElement(0, 2, `To debug this use args.callId == '${args.calls.current()}'`)
231
+ lines.newRow()
232
+ lines.setElement(0, 1, 'ERROR')
233
+ lines.setElement(0, 2, errorMessage)
234
+ this.logs.push(lines.toString())
235
+ const message = `ERROR while applying (${source}) ${generator.toLabel()}\n to\n ${JSON.stringify(context, null, 2)}.\n${errorMessage}'`
236
+ // this.logs.push(message)
237
+ // return [message]
238
+ args.calls.pop()
239
+ throw { error: [message], logs: this.logs }
240
+ }
241
+ if (((config || {}).config || {}).debug) {
242
+ const widths = [10, 10, 90]
243
+ const lines = new Lines(widths)
244
+ lines.setElement(0, 0, 'Generator')
245
+ if (generator.index > -1 && generator.km) {
246
+ // lines.setElement(0, 2, `KM '${generator.km}' ordinal: ${generator.index}`)
259
247
  lines.setElement(0, 2, generator.toLabel())
260
- lines.newRow()
261
- lines.setElement(0, 2, generator.toString())
262
- lines.newRow()
263
- lines.setElement(0, 1, 'RESULT')
264
- lines.setElement(0, 2, generated)
265
- lines.newRow()
266
- lines.setElement(0, 1, 'STACK')
267
- lines.setElement(0, 2, stack)
268
- lines.newRow()
269
- lines.setElement(0, 1, 'DEBUG')
270
- lines.setElement(0, 2, `To debug this use args.callId == '${args.calls.current()}'`)
271
- lines.newRow()
272
- lines.setElement(0, 1, 'TO')
273
- lines.setElement(0, 2, `(HASHCODE ${helpers.hashCode(JSON.stringify(helpers.sortJson(context, { depth: 25 })))})`)
274
- lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
275
- this.logs.push(lines.toString())
276
248
  }
277
- applied = true
278
- break
249
+ lines.newRow()
250
+ lines.setElement(0, 1, 'APPLIED')
251
+ lines.setElement(0, 2, generator.toLabel())
252
+ lines.newRow()
253
+ lines.setElement(0, 2, generator.toString())
254
+ lines.newRow()
255
+ lines.setElement(0, 1, 'RESULT')
256
+ lines.setElement(0, 2, generated)
257
+ lines.newRow()
258
+ lines.setElement(0, 1, 'STACK')
259
+ lines.setElement(0, 2, stack)
260
+ lines.newRow()
261
+ lines.setElement(0, 1, 'DEBUG')
262
+ lines.setElement(0, 2, `To debug this use args.callId == '${args.calls.current()}'`)
263
+ lines.newRow()
264
+ lines.setElement(0, 1, 'TO')
265
+ lines.setElement(0, 2, `(HASHCODE ${helpers.hashCode(JSON.stringify(helpers.sortJson(context, { depth: 25 })))})`)
266
+ lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
267
+ this.logs.push(lines.toString())
279
268
  }
269
+ applied = true
270
+ break
280
271
  }
281
- args.calls.pop()
282
- if (!applied && ((config || {}).config || {}).debug) {
283
- const widths = [10, 10, 90]
284
- const lines = new Lines(widths)
285
- lines.setElement(0, 0, 'Generator')
286
- lines.setElement(0, 2, 'No generator applied')
287
- lines.newRow()
288
- lines.setElement(0, 1, 'STACK')
289
- lines.setElement(0, 2, stack)
290
- lines.newRow()
291
- lines.setElement(0, 1, 'TO')
292
- lines.setElement(0, 2, JSON.stringify(context, null, 2))
293
- this.logs.push(lines.toString())
294
- }
295
- contextsPrime.push((config || {}).parenthesized ? "(" + generated + ")" : generated)
296
272
  }
297
- return contextsPrime
273
+ args.calls.pop()
274
+ if (!applied && ((config || {}).config || {}).debug) {
275
+ const widths = [10, 10, 90]
276
+ const lines = new Lines(widths)
277
+ lines.setElement(0, 0, 'Generator')
278
+ lines.setElement(0, 2, 'No generator applied')
279
+ lines.newRow()
280
+ lines.setElement(0, 1, 'STACK')
281
+ lines.setElement(0, 2, stack)
282
+ lines.newRow()
283
+ lines.setElement(0, 1, 'TO')
284
+ lines.setElement(0, 2, JSON.stringify(context, null, 2))
285
+ this.logs.push(lines.toString())
286
+ }
287
+ return ((config || {}).parenthesized ? "(" + generated + ")" : generated)
298
288
  }
299
289
  }
300
290