Created
January 17, 2023 08:55
-
-
Save titomus/932ee4a20f255c1c4fe60c643be5abf2 to your computer and use it in GitHub Desktop.
Utils NodeJS GPT-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [`<`, `>`, `&`, `"`, `'`, ` `, `©`, `®`, `™`, `¢`, `£`, `¥`, `€`]; | |
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