UNPKG

alignmentapi

Version:

An API to align words from a source language sentence to other words from a target language sentence. It generates a JSON.

299 lines (286 loc) 12.5 kB
var $a4wmA$lodash = require("lodash"); var $a4wmA$axios = require("axios"); var $a4wmA$proskommacore = require("proskomma-core"); function $parcel$defineInteropFlag(a) { Object.defineProperty(a, '__esModule', {value: true, configurable: true}); } function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } function $parcel$interopDefault(a) { return a && a.__esModule ? a.default : a; } $parcel$defineInteropFlag(module.exports); $parcel$export(module.exports, "default", () => $df16d17eb59f28f2$export$2e2bcd8739ae039); // const path = require('path'); // const fse = require('fs-extra'); class $f8f2767196dd0ff5$var$ProskommaInterface { constructor(verbose = false){ let proskomma = new (0, $a4wmA$proskommacore.Proskomma)(); const query = `{ id }`; const content = proskomma.gqlQuerySync(query) || {}; if (!content || !content.data.id) throw new Error("Failed to instantiate Proskomma"); this.proskomma = proskomma; this.usfms = {}; this.verbose = verbose; } getInstance() { return this.proskomma; } getUsfms() { return this.usfms; } getIdsUsfms() { return Object.keys(this.usfms); } getUsfm(docSetId) { return this.usfms[docSetId]; } testFirstUsfm() { let arrayIds = this.getIds(); let getSpecificId = this.queryPk(`{ documents(ids:"${arrayIds[0]}") { docSetId } }`); // let getSpecificId = await this.queryPk(`{ documents { id docSetId headers { key value } } }`); // let getSpecificId = await this.queryPk(`{ documents { docSetId } }`); let myKeyUsfm = getSpecificId.data.documents[0].docSetId; let theUsfm = this.usfms[myKeyUsfm]; console.log(theUsfm); } getIds() { let listIds = this.queryPk("{ documents { id } }"); let arrayIds = []; listIds.data.documents.forEach((element)=>{ arrayIds.push(element.id); }); return arrayIds; } getSourceText() { let arrayIds = this.getIds(); const resQuery = this.queryPk(`{ documents(ids:"${arrayIds[0]}") { mainSequence { blocks { text } } } }`); return resQuery.data.documents[0].mainSequence.blocks[0].text; } getTargetText() { let arrayIds = this.getIds(); const resQuery = this.queryPk(`{ documents(ids:"${arrayIds[1]}") { mainSequence { blocks { text } } } }`); return resQuery.data.documents[0].mainSequence.blocks[0].text; } /** * Add a document to the Proskomma instance * @param {string} rpath the relative path to the document * @param {string} codeLang the code of the language ["eng", "fra", "grk"] */ // async addDocument(rpath, codeLang="fra", abbr="ust") { // let content = fse.readFileSync(path.resolve(__dirname, rpath)).toString(); // const mutation = `mutation { addDocument(` + // `selectors: [{key: "lang", value: "${codeLang}"}, {key: "abbr", value: "${abbr}"}], ` + // `contentType: "usfm", ` + // `content: """${content}""") }`; // const docSetId = codeLang + "_" + abbr; // let res = await this.queryPk(mutation); // // does the mutation worked ? // if(res.data.addDocument) { // this.usfms[docSetId] = content; // } // } addRawDocument(fullText, codeLang = "fra", abbr = "ust") { const mutation = `mutation { addDocument(` + `selectors: [{key: "lang", value: "${codeLang}"}, {key: "abbr", value: "${abbr}"}], ` + `contentType: "usfm", ` + `content: """${fullText}""") }`; const docSetId = codeLang + "_" + abbr; let res = this.queryPk(mutation); // does the mutation worked ? if (res.data.addDocument) this.usfms[docSetId] = fullText; } /** * Fetch the document via a http address and add it to the Proskomma instance * @param {string} addr the http address where to find the document */ async addDocumentHttp(addr, codeLang = "fra", abbr = "ust") { try { this.verbose && console.log(`Fetching HTTP content for Source lsg_tit.usfm`); const response = await (0, ($parcel$interopDefault($a4wmA$axios))).get(addr); if (response.status !== 200) console.log(`Status code ${response.status} when fetching content by HTTP(S) for Source : ${addr}`); else await this.addRawDocument(response.data, codeLang, abbr); } catch (err) { console.log(`Exception when fetching content by HTTP(S) for Source lsg_tit.usfm: \n${err}`); } } queryPk(query) { const result = this.proskomma.gqlQuerySync(query); return result; } testQueryPk(query) { const result = this.proskomma.gqlQuerySync(query); console.log(JSON.stringify(result.data.documents[0].mainBlocksText[0], null, 2)); } getBookCode() { const res = this.queryPk('{ documents { bookCode: header(id:"bookCode") } }'); return res.data.documents[0].bookCode; } } var /** * Proskomma instance * @typedef Proskomma * @see {@link https://github.com/abelpz/proskomma-testing-environment/tree/main/libs/pk-core} */ $f8f2767196dd0ff5$export$2e2bcd8739ae039 = $f8f2767196dd0ff5$var$ProskommaInterface; // import Epitelete from 'epitelete'; // import { PipelineHandler } from 'proskomma-json-tools'; class $d704464ee270ec67$var$Aligner { /** * * @param {String[]} sourceUsfm - source raw usfm, code lang and abbr * @param {String[]} targetUsfm - target raw usfm, code lang and abbr * @param {boolean} verbose */ constructor({ sourceUsfm: sourceUsfm = [] , targetUsfm: targetUsfm = [] , verbose: verbose = false }){ this.proskommaInterface = new (0, $f8f2767196dd0ff5$export$2e2bcd8739ae039)(); if (!(0, $a4wmA$lodash.isNull)(sourceUsfm[0])) { this.proskommaInterface.addRawDocument(sourceUsfm[0], sourceUsfm[1], sourceUsfm[2]); this.sourceUsfm = sourceUsfm[0]; } else this.sourceUsfm = ""; if (!(0, $a4wmA$lodash.isNull)(targetUsfm[0])) { this.proskommaInterface.addRawDocument(targetUsfm[0], targetUsfm[1], targetUsfm[2]); this.targetUsfm = targetUsfm[0]; } else this.targetUsfm = ""; this.currentChapter = 0; this.currentVerse = 0; this.currentSourceSentence = []; this.currentSourceSentenceStr = ""; this.currentTargetSentence = []; this.currentTargetSentenceStr = ""; this.verbose = verbose; this.AlignementJSON = JSON.parse("{}"); this.AlignementJSON.sourceText = this.currentSourceSentenceStr; this.AlignementJSON.targetText = this.currentTargetSentenceStr; this.AlignementJSON.alignments = {}; } /** * * @returns {string[]} */ getCurrentTargetSentence() { return this.currentTargetSentence; } /** * * @returns {string[]} */ getCurrentSourceSentence() { return this.currentSourceSentence; } getCurrentChapter() { return this.currentChapter; } getCurrentVerse() { return this.currentVerse; } setCurrentChapter(chapterInt) { this.currentChapter = chapterInt; } setCurrentVerse(verseInt) { this.currentVerse = verseInt; } /** * * @param {string[]|string} sentence the sentence for alignment */ setCurrentSourceSentence(sentence) { if (typeof sentence === "object" && !(0, $a4wmA$lodash.isNull)(sentence[0]) && typeof sentence[0] === "string") this.currentSourceSentence = sentence; else if (typeof sentence === "string") { this.currentSourceSentence = sentence.trim().split(/[ ,-]/g).filter((element)=>{ return element.trim() !== ""; }); this.currentSourceSentenceStr = sentence; } this.AlignementJSON["sourceText"] = this.currentSourceSentenceStr; } /** * * @param {string[]|string} sentence the sentence for alignment */ setCurrentTargetSentence(sentence) { if (typeof sentence === "object" && !(0, $a4wmA$lodash.isNull)(sentence[0]) && typeof sentence[0] === "string") this.currentTargetSentence = sentence; else if (typeof sentence === "string") { this.currentTargetSentence = sentence.trim().split(/\W+/g).filter((element)=>{ return element.trim() !== ""; }); this.currentTargetSentenceStr = sentence; } this.AlignementJSON["targetText"] = this.currentTargetSentenceStr; } /** * Get a well formated JSON word alignment informations * @returns {JSON} */ getAlignmentJSON() { return this.AlignementJSON; } /** * Align two words and add the information to the final JSON * @param {int} sourceIndex the index of the SOURCE language to align * @param {int} targetIndex the index of the TARGET language to align */ addAlignment(sourceIndex, targetIndex) { // let ref = this.generateReference(); let sWord = this.currentSourceSentence[sourceIndex]; let tWord = this.currentTargetSentence[targetIndex]; if (!this.AlignementJSON["alignments"][sWord]) this.AlignementJSON["alignments"][sWord] = this.generateTemplateAlign(sWord, tWord, sourceIndex, targetIndex); else { this.AlignementJSON["alignments"][sWord]["targetWords"].push(tWord); this.AlignementJSON["alignments"][sWord]["targetIndexes"].push(targetIndex); } } /** * * @param {int} sourceIndex the index of the SOURCE language to UNalign * @param {int} targetIndex the index of the TARGET language to UNalign * @returns nothing */ removeAlignment(sourceIndex, targetIndex) { let sWord = this.currentSourceSentence[sourceIndex]; let tWord = this.currentTargetSentence[targetIndex]; // removing the word from 'targetWords' let index = this.AlignementJSON["alignments"][sWord]["targetWords"].indexOf(tWord); if (index !== -1) { this.AlignementJSON["alignments"][sWord]["targetWords"].splice(index, 1); // if the array of the current alignement is empty, we completely // remove the entry "sWord" if ((0, $a4wmA$lodash.isNull)(this.AlignementJSON["alignments"][sWord]["targetWords"][0])) { delete this.AlignementJSON["alignments"][sWord]; return; } this.AlignementJSON["alignments"][sWord]["targetIndexes"].splice(index, 1); } } generateReference() { let cc = this.currentChapter.toString(); let cv = this.currentVerse.toString(); if (this.currentChapter > 0 && this.currentChapter < 10) cc = "00" + cc; else if (this.currentChapter > 10 && this.currentChapter < 100) cc = "0" + cc; if (this.currentVerse > 0 && this.currentVerse < 10) cv = "00" + cv; else if (this.currentVerse > 10 && this.currentVerse < 100) cv = "0" + cv; return cc + cv; } /** * * @param {string} reference string in format chapterVerse. Ex : '001001' (chapter 1, verse 1) * @param {string} sourceWord the source word of the alignment * @param {string} targetWord the target word of the alignment * @param {int} sourceIndex * @param {int} targetIndex */ generateTemplateJson() { this.AlignementJSON = { "sourceText": this.currentSourceSentenceStr, "targetText": this.currentTargetSentenceStr, "alignments": {} }; } /** * * @param {string} sourceWord * @param {string} targetWord * @param {int} sourceIndex * @param {int} targetIndex * @returns */ generateTemplateAlign(sourceWord, targetWord, sourceIndex, targetIndex) { return { "sourceWord": sourceWord, "targetWords": [ targetWord ], "sourceIndex": sourceIndex, "targetIndexes": [ targetIndex ] }; } } var $d704464ee270ec67$export$2e2bcd8739ae039 = $d704464ee270ec67$var$Aligner; var $df16d17eb59f28f2$export$2e2bcd8739ae039 = (0, $d704464ee270ec67$export$2e2bcd8739ae039);