Skip to content

Commit

Permalink
Merge pull request #8 from translationCoreApps/feature-mcleanb-5051.2
Browse files Browse the repository at this point in the history
Feature 5051/Fix edge cases for merge and unmerge
  • Loading branch information
richmahn authored Oct 16, 2018
2 parents 6dca089 + f091394 commit 4b5acab
Show file tree
Hide file tree
Showing 19 changed files with 948 additions and 1,433 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# IntelliJ config
.idea

# compiler output
lib
144 changes: 103 additions & 41 deletions __tests__/AlignmentHelpers.test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
/* eslint-disable no-use-before-define */
/* eslint-env jest */
import fs from 'fs-extra';
import path from 'path';
import usfmjs from 'usfm-js';
jest.unmock('fs-extra');
import wordaligner, {VerseObjectUtils} from '../src/';
const RESOURCES = path.join('__tests__', 'fixtures', 'pivotAlignmentVerseObjects');
/**
* Reads a usfm file from the resources dir
* @param {string} filename relative path to usfm file
* @return {Object} - The read JSON object
*/
const readJSON = filename => {
const fullPath = path.join(RESOURCES, filename);
if (fs.existsSync(fullPath)) {
const json = fs.readJsonSync(fullPath);
return json;
}
console.log('File not found.');
return false;
};

/**
* Generator for testing merging of alignment into verseObjects
* @param {string} name - the name of the test files to use. e.g. `valid` will test `valid.usfm` to `valid.json`
*/
const mergeTest = (name = {}) => {
const json = readJSON(`${name}.json`);
expect(json).toBeTruthy();
const {alignment, verseObjects, verseString, wordBank} = json;
const output = wordaligner.merge(alignment, wordBank, verseString);
expect(output).toEqual(verseObjects);
};
/**
* Generator for testing unmerging of alignment from verseObjects
* @param {string} name - the name of the test files to use. e.g. `valid` will test `valid.usfm` to `valid.json`
*/
const unmergeTest = (name = {}) => {
const json = readJSON(`${name}.json`);
expect(json).toBeTruthy();
const {verseObjects, alignment, wordBank, alignedVerseString} = json;
const output = wordaligner.unmerge(verseObjects, alignedVerseString);
expect(output).toEqual({alignment, wordBank});
};

describe("Merge Alignment into Verse Objects", () => {
it('handles one to one', () => {
Expand Down Expand Up @@ -102,6 +67,9 @@ describe("Merge Alignment into Verse Objects", () => {
}
expect(fail).toBeTruthy();
});
it('handles titus 1-12', () => {
mergeTest('tit1-12');
});
});

describe("UnMerge Alignment from Verse Objects", () => {
Expand Down Expand Up @@ -153,6 +121,18 @@ describe("UnMerge Alignment from Verse Objects", () => {
it('handles acts 1-4', () => {
unmergeTest('acts-1-4');
});
it('handles titus 1-12', () => {
unmergeTest('tit1-12');
});
});

describe("export USFM3 from Verse Objects", () => {
it('handles acts-1-11', () => {
exportTest('acts-1-11');
});
it('handles acts 1-4', () => {
exportTest('acts-1-4');
});
});

describe('wordaligner.generateBlankAlignments', () => {
Expand Down Expand Up @@ -242,8 +222,90 @@ describe('wordaligner.generateWordBank', () => {
// then
expect(results).toEqual(wordBank);
});

//
// helpers
//
});

//
// helpers
//

/**
* Reads a json file from the resources dir
* @param {string} filename relative path to usfm file
* @return {Object} - The read JSON object
*/
const readJSON = filename => {
const fullPath = path.join(RESOURCES, filename);
if (fs.existsSync(fullPath)) {
const json = fs.readJsonSync(fullPath);
return json;
}
console.log('File not found.');
return false;
};

/**
* Reads a usfm file from the resources dir
* @param {string} filename relative path to usfm file
* @return {Object} - The read JSON object
*/
const readUSFM = filename => {
const fullPath = path.join(RESOURCES, filename);
if (fs.existsSync(fullPath)) {
const usfm = fs.readFileSync(fullPath, 'UTF-8').toString();
return usfm;
}
console.log('File not found.');
return false;
};

/**
* Generator for testing merging of alignment into verseObjects
* @param {string} name - the name of the test files to use. e.g. `valid` will test `valid.usfm` to `valid.json`
*/
const mergeTest = (name = {}) => {
const json = readJSON(`${name}.json`);
expect(json).toBeTruthy();
const {alignment, verseObjects, verseString, wordBank} = json;
const output = wordaligner.merge(alignment, wordBank, verseString);
expect(output).toEqual(verseObjects);
};

/**
* Generator for testing unmerging of alignment from verseObjects
* @param {string} name - the name of the test files to use. e.g. `valid` will test `valid.usfm` to `valid.json`
*/
const unmergeTest = (name = {}) => {
const json = readJSON(`${name}.json`);
expect(json).toBeTruthy();
const {verseObjects, alignment, wordBank, alignedVerseString} = json;
const output = wordaligner.unmerge(verseObjects, alignedVerseString);
expect(output).toEqual({alignment, wordBank});
};

/**
* Generator for testing merging of alignment into verseObjects
* @param {string} name - the name of the test files to use. e.g. `valid` will test `valid.usfm` to `valid.json`
*/
const exportTest = (name = {}) => {
const json = readJSON(`${name}.json`);
expect(json).toBeTruthy();
const expectedUsfm = readUSFM(`${name}.usfm`);
expect(expectedUsfm).toBeTruthy();
const {alignment, verseString, wordBank} = json;
const output = wordaligner.merge(alignment, wordBank, verseString);
const outputData = {
chapters: {},
headers: [],
verses: {
1: output
}
};
let usfm = usfmjs.toUSFM(outputData, {chunk: true});
const split = usfm.split("\\v 1");
usfm = split.length > 1 ? split[1] : "";
if (usfm.substr(0, 1) === ' ') {
usfm = usfm.substr(1);
}
expect(usfm).toEqual(expectedUsfm);
};

10 changes: 5 additions & 5 deletions __tests__/VerseObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('VerseObjectUtils.getWordsFromVerseObjects', () => {
occurrence: 1,
occurrences: 1
},
{type: 'text', text: ','},
{type: 'text', text: ', '},
{
tag: 'w',
type: 'word',
Expand Down Expand Up @@ -147,7 +147,7 @@ describe("getOrderedVerseObjectsFromString", () => {
},
{
type: "text",
text: ","
text: ", "
},
{
tag: "w",
Expand Down Expand Up @@ -193,7 +193,7 @@ describe("getOrderedVerseObjectsFromString", () => {
},
{
type: "text",
text: ","
text: ", "
},
{
tag: "w",
Expand Down Expand Up @@ -253,7 +253,7 @@ describe("getOrderedVerseObjectsFromString", () => {
},
{
type: "text",
text: ","
text: ", "
},
{
tag: "w",
Expand All @@ -278,7 +278,7 @@ describe("getOrderedVerseObjectsFromString", () => {
},
{
type: "text",
text: "."
text: ". "
},
{
tag: "f",
Expand Down
10 changes: 5 additions & 5 deletions __tests__/fixtures/pivotAlignmentVerseObjects/acts-1-11.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"alignedVerseString": "οἳ καὶ εἶπαν, “ ἄνδρες, Γαλιλαῖοι, τί ἑστήκατε βλέποντες εἰς τὸν οὐρανόν? οὗτος ὁ Ἰησοῦς ὁ ἀναλημφθεὶς ἀφ’ ὑμῶν εἰς τὸν οὐρανὸν, οὕτως ἐλεύσεται ὃν τρόπον ἐθεάσασθε αὐτὸν πορευόμενον εἰς τὸν οὐρανόν.”",
"verseString": "And they said,\"Men of Galilee, why do you stand looking into heaven? This Jesus who has been taken up from you into heaven, will likewise return in the same manner as you saw him going into heaven.\"\\p",
"verseString": "And they said,\"Men of Galilee, why do you stand looking into heaven? This Jesus who has been taken up from you into heaven, will likewise return in the same manner as you saw him going into heaven.\"\n\\p",
"alignment": [
{
"topWords": [
Expand Down Expand Up @@ -709,7 +709,7 @@
"type": "milestone"
},
{
"text": ",",
"text": ", ",
"type": "text"
},
{
Expand Down Expand Up @@ -834,7 +834,7 @@
"type": "milestone"
},
{
"text": "?",
"text": "? ",
"type": "text"
},
{
Expand Down Expand Up @@ -1035,7 +1035,7 @@
"type": "milestone"
},
{
"text": ",",
"text": ", ",
"type": "text"
},
{
Expand Down Expand Up @@ -1273,7 +1273,7 @@
"type": "text"
},
{
"text": "\"",
"text": "\"\n",
"type": "text"
},
{
Expand Down
104 changes: 104 additions & 0 deletions __tests__/fixtures/pivotAlignmentVerseObjects/acts-1-11.usfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

\zaln-s | x-strong="G25320" x-lemma="καί" x-morph="Gr,D,,,,,,,,," x-occurrence="1" x-occurrences="1" x-content="καὶ"
\w And|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G37390" x-lemma="ὅς" x-morph="Gr,RR,,,,NMP," x-occurrence="1" x-occurrences="1" x-content="οἳ"
\w they|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G30040" x-lemma="λέγω" x-morph="Gr,V,IAA3,,P," x-occurrence="1" x-occurrences="1" x-content="εἶπαν"
\w said|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*,"
\zaln-s | x-strong="G04350" x-lemma="ἀνήρ" x-morph="Gr,N,,,,,VMP," x-occurrence="1" x-occurrences="1" x-content="ἄνδρες"
\w Men|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G10570" x-lemma="Γαλιλαῖος" x-morph="Gr,NS,,,,VMP," x-occurrence="1" x-occurrences="1" x-content="Γαλιλαῖοι"
\w of|x-occurrence="1" x-occurrences="1"\w*
\w Galilee|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*,
\zaln-s | x-strong="G51010" x-lemma="τίς" x-morph="Gr,RT,,,,ANS," x-occurrence="1" x-occurrences="1" x-content="τί"
\w why|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G24760" x-lemma="ἵστημι" x-morph="Gr,V,IEA2,,P," x-occurrence="1" x-occurrences="1" x-content="ἑστήκατε"
\w do|x-occurrence="1" x-occurrences="1"\w*
\w you|x-occurrence="1" x-occurrences="3"\w*
\w stand|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G09910" x-lemma="βλέπω" x-morph="Gr,V,PPA,NMP," x-occurrence="1" x-occurrences="1" x-content="βλέποντες"
\w looking|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G15190" x-lemma="εἰς" x-morph="Gr,P,,,,,A,,," x-occurrence="1" x-occurrences="3" x-content="εἰς"
\w into|x-occurrence="1" x-occurrences="3"\w*
\zaln-e\*
\zaln-s | x-strong="G35880" x-lemma="ὁ" x-morph="Gr,EA,,,,AMS," x-occurrence="1" x-occurrences="3" x-content="τὸν"
\zaln-s | x-strong="G37720" x-lemma="οὐρανός" x-morph="Gr,N,,,,,AMS," x-occurrence="1" x-occurrences="2" x-content="οὐρανόν"
\w heaven|x-occurrence="1" x-occurrences="3"\w*
\zaln-e\*
\zaln-e\*?
\zaln-s | x-strong="G37780" x-lemma="οὗτος" x-morph="Gr,ED,,,,NMS," x-occurrence="1" x-occurrences="1" x-content="οὗτος"
\w This|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G35880" x-lemma="ὁ" x-morph="Gr,EA,,,,NMS," x-occurrence="1" x-occurrences="2" x-content="ὁ"
\zaln-s | x-strong="G24240" x-lemma="Ἰησοῦς" x-morph="Gr,N,,,,,NMS," x-occurrence="1" x-occurrences="1" x-content="Ἰησοῦς"
\w Jesus|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-e\*
\zaln-s | x-strong="G35880" x-lemma="ὁ" x-morph="Gr,RD,,,,NMS," x-occurrence="2" x-occurrences="2" x-content="ὁ"
\zaln-s | x-strong="G03530" x-lemma="ἀναλαμβάνω" x-morph="Gr,V,PAP,NMS," x-occurrence="1" x-occurrences="1" x-content="ἀναλημφθεὶς"
\w who|x-occurrence="1" x-occurrences="1"\w*
\w has|x-occurrence="1" x-occurrences="1"\w*
\w been|x-occurrence="1" x-occurrences="1"\w*
\w taken|x-occurrence="1" x-occurrences="1"\w*
\w up|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-e\*
\zaln-s | x-strong="G05750" x-lemma="ἀπό" x-morph="Gr,P,,,,,G,,," x-occurrence="1" x-occurrences="1" x-content="ἀφ’"
\w from|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G47710" x-lemma="σύ" x-morph="Gr,RP,,,2G,P," x-occurrence="1" x-occurrences="1" x-content="ὑμῶν"
\w you|x-occurrence="2" x-occurrences="3"\w*
\zaln-e\*
\zaln-s | x-strong="G15190" x-lemma="εἰς" x-morph="Gr,P,,,,,A,,," x-occurrence="2" x-occurrences="3" x-content="εἰς"
\w into|x-occurrence="2" x-occurrences="3"\w*
\zaln-e\*
\zaln-s | x-strong="G35880" x-lemma="ὁ" x-morph="Gr,EA,,,,AMS," x-occurrence="2" x-occurrences="3" x-content="τὸν"
\zaln-s | x-strong="G37720" x-lemma="οὐρανός" x-morph="Gr,N,,,,,AMS," x-occurrence="1" x-occurrences="1" x-content="οὐρανὸν"
\w heaven|x-occurrence="2" x-occurrences="3"\w*
\zaln-e\*
\zaln-e\*,
\zaln-s | x-strong="G20640" x-lemma="ἔρχομαι" x-morph="Gr,V,IFM3,,S," x-occurrence="1" x-occurrences="1" x-content="ἐλεύσεται"
\w will|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G37790" x-lemma="οὕτως" x-morph="Gr,D,,,,,,,,," x-occurrence="1" x-occurrences="1" x-content="οὕτως"
\w likewise|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G20640" x-lemma="ἔρχομαι" x-morph="Gr,V,IFM3,,S," x-occurrence="1" x-occurrences="1" x-content="ἐλεύσεται"
\w return|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G37390" x-lemma="ὅς" x-morph="Gr,ER,,,,AMS," x-occurrence="1" x-occurrences="1" x-content="ὃν"
\w in|x-occurrence="1" x-occurrences="1"\w*
\w the|x-occurrence="1" x-occurrences="1"\w*
\w same|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G51580" x-lemma="τρόπος" x-morph="Gr,N,,,,,AMS," x-occurrence="1" x-occurrences="1" x-content="τρόπον"
\w manner|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G23000" x-lemma="θεάομαι" x-morph="Gr,V,IAM2,,P," x-occurrence="1" x-occurrences="1" x-content="ἐθεάσασθε"
\w as|x-occurrence="1" x-occurrences="1"\w*
\w you|x-occurrence="3" x-occurrences="3"\w*
\w saw|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G08460" x-lemma="αὐτός" x-morph="Gr,RP,,,3AMS," x-occurrence="1" x-occurrences="1" x-content="αὐτὸν"
\w him|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G41980" x-lemma="πορεύω" x-morph="Gr,V,PPM,AMS," x-occurrence="1" x-occurrences="1" x-content="πορευόμενον"
\w going|x-occurrence="1" x-occurrences="1"\w*
\zaln-e\*
\zaln-s | x-strong="G15190" x-lemma="εἰς" x-morph="Gr,P,,,,,A,,," x-occurrence="3" x-occurrences="3" x-content="εἰς"
\w into|x-occurrence="3" x-occurrences="3"\w*
\zaln-e\*
\zaln-s | x-strong="G35880" x-lemma="ὁ" x-morph="Gr,EA,,,,AMS," x-occurrence="3" x-occurrences="3" x-content="τὸν"
\zaln-s | x-strong="G37720" x-lemma="οὐρανός" x-morph="Gr,N,,,,,AMS," x-occurrence="2" x-occurrences="2" x-content="οὐρανόν"
\w heaven|x-occurrence="3" x-occurrences="3"\w*
\zaln-e\*
\zaln-e\*."
\p
Loading

0 comments on commit 4b5acab

Please sign in to comment.