Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

articulate-nlg

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

articulate-nlg - npm Package Compare versions

Comparing version 1.1.8 to 2.0.1

dist/index_old.d.ts

41

dist/index.d.ts

@@ -1,27 +0,28 @@

export default class Persona {
vocab: Object;
core: Object;
constructor(vocab?: Object, core?: Object);
say: (template: string, params?: {}) => string;
}
interface WeightedVocab {
v: string;
t: string;
w: number;
}
interface ParamTextPair {
interface ParamValuePair {
p: string;
t: string;
t: any;
}
export declare class VocabHelpers {
static capitalize: (text: string) => string;
static choose: (texts: (string | WeightedVocab)[]) => string;
static maybe: (text: string) => string;
static say: (vocabKey: string) => string;
static capSay: (text: string) => string;
static param: (paramKey: string) => string;
static ifThen: (paramKey: string, thenText: string) => string;
static ifNot: (paramKey: string, thenText: string) => string;
static ifElse: (paramKey: string, thenText: string, elseText: string) => string;
static doFirst: (paramTextPairs: ParamTextPair[], defaultText?: string) => string;
export default class Persona {
vocab: any;
private params;
private cycledTextsGroups;
constructor(vocab?: any, params?: any, cycledTextsGroups?: any);
say: (vocabKey: string, params?: any) => string;
capitalize: (text: string) => string;
capSay: (vocabKey: string, params?: any) => string;
render: (val: any) => string;
choose: (...texts: (string | WeightedVocab)[]) => string;
private getCycledTextsFor;
cycle: (...texts: (string | WeightedVocab)[]) => string;
maybe: (text: string) => string;
param: (paramKey: string) => string;
ifThen: (paramKey: string, then: any) => string;
ifNot: (paramKey: string, then: any) => string;
ifElse: (paramKey: string, then: any, otherwise: any) => string;
doFirst: (paramTextPairs: ParamValuePair[], defaultText?: string) => string;
}
export {};
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -17,117 +6,183 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
var mustache_1 = __importDefault(require("mustache"));
var random_seed_weighted_chooser_1 = __importDefault(require("random-seed-weighted-chooser"));
var defaultCore = {
capitalize: function () {
return function (text, render) {
var renderedText = render(text);
return renderedText.charAt(0).toUpperCase() + renderedText.slice(1);
};
},
choose: function () {
return function (text, render) {
var segments = text.split("|");
var segmentsWithWeights = [];
var regex = /(.*)[=]([0-9]*[.]?[0-9]+)/;
segments.forEach(function (segment) {
var match = segment.match(regex);
if (match !== null && match.length >= 2) {
segmentsWithWeights.push({
value: match[1],
weight: parseFloat(match[2])
});
}
else {
segmentsWithWeights.push({ value: segment, weight: 1 });
}
});
var chosen = random_seed_weighted_chooser_1.default.chooseWeightedObject(segmentsWithWeights);
var renderedText = render(chosen.value);
return renderedText;
};
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
var hashCode = function (text) {
var hash = 0, i, chr;
if (text.length === 0)
return hash;
for (i = 0; i < text.length; i++) {
chr = text.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
var toWeightedVocabs = function (texts) {
return texts.map(function (val) {
if (typeof val === "string") {
return { t: val, w: 1 };
}
return val;
});
};
var Persona = /** @class */ (function () {
function Persona(vocab, core) {
function Persona(vocab, params, cycledTextsGroups) {
var _this = this;
if (vocab === void 0) { vocab = {}; }
if (core === void 0) { core = defaultCore; }
if (params === void 0) { params = {}; }
if (cycledTextsGroups === void 0) { cycledTextsGroups = {}; }
this.vocab = vocab;
this.core = core;
this.say = function (template, params) {
if (params === void 0) { params = {}; }
return mustache_1.default.render("{{>" + template + "}}", __assign({}, _this.core, { params: params }), _this.vocab);
this.params = params;
this.cycledTextsGroups = cycledTextsGroups;
this.say = function (vocabKey, params) {
if (params === void 0) { params = _this.params; }
_this.params = params;
var val = _this.vocab[vocabKey];
return _this.render(val);
};
}
return Persona;
}());
exports.default = Persona;
var preventNesting = function (stringToCheck, tag, returnIfValid, returnIfInvalid) {
if (stringToCheck.indexOf(tag) >= 0) {
console.warn("Can't nest " + tag + ". Make into another partial and reference it instead.", "Defaulting to " + returnIfInvalid, "For:", stringToCheck);
return returnIfInvalid;
}
return returnIfValid;
};
var VocabHelpers = /** @class */ (function () {
function VocabHelpers() {
}
VocabHelpers.capitalize = function (text) {
return preventNesting(text, "{{#capitalize}}", "{{#capitalize}}" + text + "{{/capitalize}}", text);
};
VocabHelpers.choose = function (texts) {
var firstValue = "";
var parts = texts.map(function (val) {
if (typeof val === "string") {
firstValue = !!firstValue ? firstValue : val;
this.capitalize = function (text) {
return text.charAt(0).toUpperCase() + text.slice(1);
};
this.capSay = function (vocabKey, params) {
if (params === void 0) { params = _this.params; }
return _this.capitalize(_this.say(vocabKey, params));
};
this.render = function (val) {
if (typeof val === "function") {
// Call it and render the value, which could be anything.
return _this.render(val());
}
else if (typeof val === "string") {
return val;
}
else if (!!val) {
return val + "";
}
else {
firstValue = !!firstValue ? firstValue : val.v;
return val.v + "=" + val.w;
return "";
}
});
var joinedParts = parts.join("|");
return preventNesting(joinedParts, "{{#choose}}", "{{#choose}}" + joinedParts + "{{/choose}}", firstValue);
};
this.choose = function () {
var texts = [];
for (var _i = 0; _i < arguments.length; _i++) {
texts[_i] = arguments[_i];
}
var weightedVocabs = toWeightedVocabs(texts);
var choice = random_seed_weighted_chooser_1.default.chooseWeightedObject(weightedVocabs, "w");
return _this.render(choice["t"]);
};
this.cycle = function () {
var texts = [];
for (var _i = 0; _i < arguments.length; _i++) {
texts[_i] = arguments[_i];
}
var weightedVocabs = toWeightedVocabs(texts);
// Create a hash that's used to group the items provided.
// This prevents global cycling and increases search performance.
var textsHash = hashCode(weightedVocabs.map(function (val) { return val.t; }).join("")) + "";
var cycledTexts = _this.getCycledTextsFor(textsHash);
// console.log(textsHash, cycledTexts);
var filtered = weightedVocabs.filter(function (val) {
return !cycledTexts.includes(val.t);
});
// If they've all been used...
if (filtered.length === 0) {
// Choose from any of them
filtered = weightedVocabs;
// And remove all items from the cycled texts array
weightedVocabs.forEach(function (val) {
var index = cycledTexts.indexOf(val.t);
if (index >= 0) {
cycledTexts.splice(index, 1);
}
});
}
var chosen = _this.choose.apply(_this, filtered);
cycledTexts.push(chosen);
return chosen;
};
this.maybe = function (text) {
return _this.choose("", text);
};
this.param = function (paramKey) {
var val = _this.params[paramKey];
return _this.render(val);
};
this.ifThen = function (paramKey, then) {
return _this.ifElse(paramKey, then, "");
};
this.ifNot = function (paramKey, then) {
return _this.ifElse(paramKey, "", then);
};
this.ifElse = function (paramKey, then, otherwise) {
if (!!_this.params[paramKey]) {
return _this.render(then);
}
else {
return _this.render(otherwise);
}
};
this.doFirst = function (paramTextPairs, defaultText) {
if (defaultText === void 0) { defaultText = ""; }
for (var i = 0; i < paramTextPairs.length; i++) {
var pair = paramTextPairs[i];
var paramKey = pair.p;
var value = pair.t;
if (!!_this.params[paramKey]) {
return _this.render(value);
}
}
return defaultText;
};
}
Persona.prototype.getCycledTextsFor = function (hash) {
var cycledTexts = this.cycledTextsGroups[hash];
if (!!cycledTexts) {
return cycledTexts;
}
else {
this.cycledTextsGroups[hash] = [];
return this.cycledTextsGroups[hash];
}
};
VocabHelpers.maybe = function (text) {
return VocabHelpers.choose([text, ""]);
return Persona;
}());
exports.default = Persona;
/*
class Justin extends Persona {
createVocab = () => {
let say = this.say;
let capSay = this.capSay;
let choose = this.choose;
let maybe = this.maybe;
let cycle = this.cycle;
let param = this.param;
let doFirst = this.doFirst;
return {
greet: (): string =>
capSay("hi") +
"-" +
cycle("1", "2", "3", "4", "5") +
"-" +
cycle("2", "1", "3", "4", "5") +
"-" +
choose("hi", "hey", "hello", "what's up") +
"-" +
maybe(say("hi")) +
say("name") +
doFirst([{ p: "name", t: say("name") }], "not found"),
hi: "hiiii",
num: 6,
name: (): string => param("name")
};
VocabHelpers.say = function (vocabKey) {
return "{{>" + vocabKey + "}}";
};
VocabHelpers.capSay = function (text) {
var articulated = VocabHelpers.say(text);
var capitalized = VocabHelpers.capitalize(articulated);
return capitalized;
};
VocabHelpers.param = function (paramKey) {
return "{{params." + paramKey + "}}";
};
VocabHelpers.ifThen = function (paramKey, thenText) {
return preventNesting(thenText, "{{#params." + paramKey + "}}", "{{#params." + paramKey + "}}" + thenText + "{{/params." + paramKey + "}}", thenText);
};
VocabHelpers.ifNot = function (paramKey, thenText) {
return preventNesting(thenText, "{{^params." + paramKey + "}}", "{{^params." + paramKey + "}}" + thenText + "{{/params." + paramKey + "}}", thenText);
};
VocabHelpers.ifElse = function (paramKey, thenText, elseText) {
return "" + VocabHelpers.ifThen(paramKey, thenText) + VocabHelpers.ifNot(paramKey, elseText);
};
VocabHelpers.doFirst = function (paramTextPairs, defaultText) {
if (defaultText === void 0) { defaultText = ""; }
// Each if/else goes inside the previous.
// So I put my thang down, slice it and reverse it.
// The slice creates a new array since reverse() affects the original.
var template = paramTextPairs
.slice()
.reverse()
.reduce(function (acc, curr) {
var paramKey = curr.p;
var value = curr.t;
return VocabHelpers.ifElse(paramKey, value, acc);
}, defaultText);
return template;
};
return VocabHelpers;
}());
exports.VocabHelpers = VocabHelpers;
};
vocab = this.createVocab();
}
let justin = new Justin();
let count = 100;
new Array(count).fill(0).forEach(() => {
let params = { name: "justin" };
console.log(justin.say("greet", params));
});
*/
{
"name": "articulate-nlg",
"version": "1.1.8",
"version": "2.0.1",
"description": "A natural language generator (NLG) that articulates concepts as words, phrases, and sentences.",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc