language-monitor
Advanced tools
Comparing version 1.0.1 to 1.0.2
65
index.js
@@ -1,5 +0,13 @@ | ||
var LanguageDetect = require('languagedetect'); | ||
var lngDetector = new LanguageDetect(); | ||
/* | ||
* @Author: zyc | ||
* @Date: 2015-12-06 03:17:09 | ||
* @Last Modified by: zyc | ||
* @Last Modified time: 2016-01-26 14:15:23 | ||
*/ | ||
'use strict' | ||
var unicode = { | ||
const LanguageDetect = require('languagedetect') | ||
const lngDetector = new LanguageDetect() | ||
const unicode = { | ||
chinese: [ | ||
@@ -19,22 +27,39 @@ /[\u4E00-\u9FBF]/g, // CJK 统一表意符号 (CJK Unified Ideographs) | ||
], | ||
}; | ||
} | ||
module.exports = function (sample, limit) { | ||
if (!sample) return []; | ||
var languages = lngDetector.detect(sample, limit); | ||
for (var langcode in unicode) { | ||
var count = 0; | ||
for (var regex of unicode[langcode]) { | ||
var matcher = sample.match(regex); | ||
if (matcher) count += matcher.length; | ||
module.exports = (sample, limit) => { | ||
if (!sample) return [] | ||
const languages = detectLanguage(sample, limit) | ||
for (let langcode in unicode) { | ||
let count = 0 | ||
for (let regex of unicode[langcode]) { | ||
const matcher = sample.match(regex) | ||
if (matcher) count += matcher.length | ||
} | ||
if (count) languages.push([langcode, count/sample.length]); | ||
if (count) languages.push([langcode, count / sample.length]) | ||
} | ||
languages.sort((a, b) => { return b[1] - a[1] }); | ||
var result = []; | ||
for (var language of languages) { | ||
if (result.length == limit) break; | ||
result.push({ code: language[0], rate: language[1] }); | ||
languages.sort((a, b) => { return b[1] - a[1] }) | ||
const result = [] | ||
for (let language of languages) { | ||
if (result.length == limit) break | ||
result.push({ code: language[0], rate: language[1] }) | ||
} | ||
return result; | ||
}; | ||
return result | ||
} | ||
const detectLanguage = (sample, limit) => { | ||
let languages = []//lngDetector.detect(sample, limit) | ||
let length = sample.length, count = 1 | ||
while (!languages.length) { | ||
count *= 2 | ||
const size = length / count | ||
if (size < 1) break | ||
let start = 0 | ||
while (start < length) { | ||
const text = sample.substring(start, start += size) | ||
languages = lngDetector.detect(text, limit) | ||
if (languages.length) break | ||
} | ||
} | ||
return languages | ||
} |
{ | ||
"name": "language-monitor", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Nodejs language monitor library using languagedetect and unicode", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "test.js" | ||
"test": "node test" | ||
}, | ||
@@ -9,0 +9,0 @@ "author": { |
@@ -15,3 +15,3 @@ # language-monitor | ||
```js | ||
var languageMonitor = require('./index'); | ||
const languageMonitor = require('language-monitor'); | ||
@@ -18,0 +18,0 @@ console.log(languageMonitor('为languagedetect添加中にほん한语言检测')); |
12
test.js
@@ -1,3 +0,11 @@ | ||
var languageMonitor = require('./index'); | ||
/* | ||
* @Author: zyc | ||
* @Date: 2016-01-26 13:13:34 | ||
* @Last Modified by: zyc | ||
* @Last Modified time: 2016-01-26 14:13:49 | ||
*/ | ||
'use strict' | ||
console.log(languageMonitor('为languagedetect添加中にほん한语言检测')); | ||
const languageMonitor = require('./index') | ||
console.log(languageMonitor('为languagedetect添加中にほん한语言检测')) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6786
67