Skip to content

Instantly share code, notes, and snippets.

@titomus
Created January 17, 2023 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titomus/932ee4a20f255c1c4fe60c643be5abf2 to your computer and use it in GitHub Desktop.
Save titomus/932ee4a20f255c1c4fe60c643be5abf2 to your computer and use it in GitHub Desktop.
Utils NodeJS GPT-3
const fs = require("fs");
module.exports = {
getFileContent: function (file) {
return fs.readFileSync(file, 'utf8');
},
logThis: function (file,datas) {
fs.appendFileSync(file, `${datas}\n===\n`);
},
stringToFilename: function (string, ext = 'txt') {
return string.trim().toLowerCase()
.replace(/[éêèë]/g, 'e')
.replace(/[àâä]/g, 'a')
.replace(/[îï]/g, 'i')
.replace(/[ôö]/g, 'o')
.replace(/[ùûü]/g, 'u')
.replace(/[ç]/g, 'c')
//remove punctuation ! ? . , ; : ' " ( ) [ ] { } / \ | & @ # $ % ^ * + = - _ ~ ` < > °
.replace(/[!?.;:'"()\[\]{}\/\\|&@#$%^*+=\-_~`<>°]/g, '')
.replace(/[^a-z0-9\-]/g, '-') + '.' + ext;
},
formatHtml: function (html) {
var toReplace = [`&lt;`, `&gt;`, `&amp;`, `&quot;`, `&apos;`, `&nbsp;`, `&copy;`, `&reg;`, `&trade;`, `&cent;`, `&pound;`, `&yen;`, `&euro;`];
var replaceWith = [`<`, `>`, `&`, `"`, `'`, ` `, `©`, `®`, `™`, `¢`, `£`, `¥`, `€`];
for (let i = 0; i < toReplace.length; i++) {
html = html.replace(new RegExp(toReplace[i], "g"), replaceWith[i]);
}
let formatted = html.replace(/(\.\s)?(En conclusion|De plus|En résumé|Ensuite|Enfin|Tout d\'abord),\s([a-z])/g, (match, p1, p2, p3) => {
return `${p1 ? p1 : ''}${p3.toUpperCase()}`;
});
return formatted;
},
toHtml: function (text) {
let tag = 'p';
let html = text.split(/(?:\r?\n)+/).map((paragraph) => {
if(paragraph.startsWith('-')){
return `<li>${paragraph.replace('-','')}</li>`;
}
return `<${tag}>${paragraph.trim()}</${tag}>`;
}).join(`\n`);
html = html.replace(/<\/p>(?:\r?\n)+<li>/g, '</p>\n<ul>\n<li>')
.replace(/<\/li>(?:\r?\n)+<p>/g, '</li>\n</ul>\n<p>');
return html;
},
cleanLine: function (line) {
// whatever
return line.replace(/[0-9]{0,2}\.\s?/gi,'').trim();
},
wordCount: function (text) {
return text.split(/\s+/).length;
},
removeIaPatterns: function (text) {
let formatted = text.replace(/(\.\s)?(En conclusion|De plus|Ensuite|Enfin|Tout d\'abord),\s([a-z])/g, (match, p1, p2, p3) => {
return `${p1 ? p1 : ''}${p3.toUpperCase()}`;
});
return formatted;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment