Skip to content

Commit

Permalink
support the bestEffort flag
Browse files Browse the repository at this point in the history
  • Loading branch information
slackhappy committed Aug 27, 2024
1 parent 1a13570 commit 9b7bf2d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ Pass top level domain as a hint

Pass an HTTP "Content-Encoding" value as a hint

#### bestEffort

Set to true to give best-effort answer, instead of UNKNOWN_LANGUAGE. May be useful for
short text if the caller prefers an approximate answer over none.

## Warning
Once the module has been installed, the underlying C sources will remain in the ```deps/cld``` folder and continue to occupy considerable space. This is because they will be required if you ever need to run `npm rebuild`. If you are under severe constraints you can delete this folder and reclam >100M

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface Options {
readonly encodingHint?: string;
readonly tldHint?: string;
readonly httpHint?: string;
readonly bestEffort?: string;
}
interface DetectLanguage {
readonly reliable: boolean;
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ module.exports = {
languageHint : '',
encodingHint : '',
tldHint : '',
httpHint : ''
httpHint : '',
bestEffort : false
};
options = _.defaults({}, options, defaults);

if (!_.isBoolean(options.isHTML)) {
throw new Error('Invalid isHTML value');
}
if (!_.isBoolean(options.bestEffort)) {
throw new Error('Invalid bestEffort value');
}
if (!_.isString(options.languageHint)) {
throw new Error('Invalid languageHint');
}
Expand Down Expand Up @@ -64,7 +68,8 @@ module.exports = {
options.languageHint,
options.encodingHint,
options.tldHint,
options.httpHint
options.httpHint,
options.bestEffort
);

if (result.languages.length < 1) {
Expand Down
8 changes: 7 additions & 1 deletion src/cld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace NodeCld {
httpHint;
int numBytes;
bool isPlainText;
bool bestEffort;
};

struct CLDOutput {
Expand Down Expand Up @@ -52,6 +53,7 @@ namespace NodeCld {
if (info[5].IsString()) {
input->httpHint = info[5].ToString().Utf8Value();
}
input->bestEffort = info[6].ToBoolean();

return input;
}
Expand Down Expand Up @@ -79,13 +81,17 @@ namespace NodeCld {
if (input->httpHint.length() > 0) {
hints.content_language_hint = input->httpHint.c_str();
}
int flags = 0;
if (input->bestEffort) {
flags |= CLD2::kCLDFlagBestEffort;
}

CLD2::ExtDetectLanguageSummary(
input->bytes.c_str(),
input->numBytes,
input->isPlainText,
&hints,
0,
flags,
output->language3,
output->percent3,
output->normalized_score3,
Expand Down
17 changes: 16 additions & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function runCoreTests(detected) {
return;
}

const result = await cld.detct(val.sample);
const result = await cld.detect(val.sample);
assert.equal(_.isArray(result.languages), true);
assert.equal(result.languages.length > 0, true);
assert.equal(val.name, result.languages[0].name);
Expand All @@ -20,6 +20,20 @@ async function runCoreTests(detected) {
}
}

async function runBestEffortTests() {
for (const val of data.all) {
if (!val.testOnWindows) {
return;
}

const result = await cld.detect(val.sample, {bestEffort: true});
assert.equal(_.isArray(result.languages), true);
assert.equal(result.languages.length > 0, true);
assert.equal(val.name, result.languages[0].name);
}
}


async function runChunkTests() {
for (const val of data.all) {
if (!val.testOnWindows) {
Expand Down Expand Up @@ -165,6 +179,7 @@ function runCrossCheckTests(detected) {
let detected = {};

await runCoreTests(detected);
await runBestEffortTests();
await runChunkTests();
await runEncodingHintTests();
await runLanguageHintTests();
Expand Down

0 comments on commit 9b7bf2d

Please sign in to comment.