Skip to content

Commit ec7d4b8

Browse files
committed
I simplified the test cases. The test is now making sure that the language is detected correctly for each file and instead of checking the language ratio we're now checking the confidence score
1 parent 380a478 commit ec7d4b8

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

src/components/processContent.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ module.exports = (data, fileInfo) => {
2020

2121
const calculations = calculateConfidenceScore(data, fileInfo);
2222

23-
if (data.testFile) {
24-
return calculations;
25-
}
26-
2723
if (fileInfo.confidence.encoding) {
2824
fileInfo.confidence.language = calculations;
2925
} else {

src/components/processing-content/calculateConfidenceScore.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = (data, fileInfo) => {
66
const totalCharacters = data.content.replace(charRegex, "").length;
77
const langArr = data.languageArr;
88
const pos = data.pos;
9-
const testFile = data.testFile;
109

1110
const secondLanguage = langArr.reduce((acc, val) => {
1211
if (acc.name === fileInfo.language) return val;
@@ -57,24 +56,5 @@ module.exports = (data, fileInfo) => {
5756
);
5857
}
5958

60-
// If the test script is running
61-
if (testFile) {
62-
return {
63-
name: testFile.substr(testFile.lastIndexOf("/") + 1),
64-
path: testFile,
65-
encoding: fileInfo.encoding,
66-
language: fileInfo.language,
67-
languageConfidence: confidenceScore,
68-
ratio: Number(languageRatio.toFixed(2)),
69-
count: langArr[pos].count,
70-
totalCharacters: totalCharacters,
71-
characterWordRatio: characterWordRatio.toFixed(6),
72-
secondLanguage: {
73-
name: secondLanguage.name,
74-
count: secondLanguage.count,
75-
},
76-
};
77-
}
78-
7959
return confidenceScore;
8060
};

src/index-node.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const checkUTF = require("./components/checkUTF.js");
33
const processContent = require("./components/processContent.js");
44
const checkByteOrderMark = require("./components/checkByteOrderMark.js");
55

6-
module.exports = (file, test) => {
6+
module.exports = (file) => {
77
return new Promise((resolve, reject) => {
88
const fileInfo = {
99
encoding: null,
@@ -14,7 +14,6 @@ module.exports = (file, test) => {
1414
},
1515
};
1616
const data = {};
17-
data.testFile = test ? file : null;
1817

1918
// Reading the first four bytes and checking if they coincide with one of the predefined byte order marks.
2019
const readStream = fs.createReadStream(file, { start: 0, end: 3 });

testing/language-encoding.test.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,44 @@ const folderPath = "/home/gignu/Documents/Subtitle Database/Language Folders/";
1212
const testFiles = getFiles(folderPath);
1313

1414
testFiles.forEach((file) => {
15-
languageEncoding(file, true)
15+
languageEncoding(file)
1616
.then((fileInfo) => {
17-
if (fileInfo.ratio <= 0.85) {
17+
// language = language
18+
const testFileArray = file.split("/");
19+
const expectedLanguage = testFileArray[testFileArray.length - 2]
20+
.toLowerCase()
21+
.replace(" ", "-");
22+
23+
if (
24+
fileInfo.language !== expectedLanguage &&
25+
expectedLanguage !== "japanese"
26+
)
27+
testFailed("language");
28+
29+
// confidence >= 0.95
30+
if (
31+
fileInfo.confidence.encoding < 0.95 &&
32+
expectedLanguage !== "japanese"
33+
)
34+
testFailed("confidence");
35+
36+
function testFailed(issue) {
1837
console.log("Test case failed:");
38+
39+
switch (issue) {
40+
case "language":
41+
console.log("Expected language:", expectedLanguage);
42+
console.log("Detected language:", fileInfo.language);
43+
case "confidence":
44+
console.log("Confidence score too low!");
45+
console.log(
46+
"fileInfo.confidence.encoding:",
47+
fileInfo.confidence.encoding
48+
);
49+
}
50+
1951
console.log(fileInfo);
52+
console.log("file:", file);
2053
process.exit(1);
2154
}
2255
})

0 commit comments

Comments
 (0)