-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaip.js
134 lines (116 loc) · 3.68 KB
/
aip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const conf = require("./conf.js")
var AipNlpClient = require("baidu-aip-sdk").nlp;
var AipOcrClient = require("baidu-aip-sdk").ocr;
var AipSpeechClient = require("baidu-aip-sdk").speech;
// 设置APPID/AK/SK
var APP_ID = conf.APP_ID;
var API_KEY = conf.API_KEY;
var SECRET_KEY = conf.SECRET_KEY;
// 设置语音识别 APPID/AK/SK
var SPEECH_APP_ID = conf.APP_ID;
var SPEECH_API_KEY = conf.API_KEY;
var SPEECH_SECRET_KEY = conf.SECRET_KEY;
// 新建一个对象,建议只保存一个对象调用服务接口
var nlpClient = new AipNlpClient(APP_ID, API_KEY, SECRET_KEY);
var ocrClient = new AipOcrClient(APP_ID, API_KEY, SECRET_KEY);
var speechClient = new AipSpeechClient(SPEECH_APP_ID, SPEECH_API_KEY, SPEECH_SECRET_KEY);
function w2v(word) {
// 调用词向量表示
nlpClient.wordembedding(word).then(function(result) {
console.log(JSON.stringify(result));
}).catch(function(err) {
// 如果发生网络错误
console.log(err);
});
}
function word_segment(text) {
// 调用词法分析
return nlpClient.lexer(text).then(function(result) {
const items = result["items"]
const words = []
items.forEach(item => words.push(item["item"]));
return words;
}).catch(function(err) {
// 如果发生网络错误
console.log(err);
});
}
function word_frequency(words, sort="ascending") {
let wordCounts = {};
for (let i = 0; i < words.length; i++) {
var word = words[i];
if (!wordCounts[word]) {
wordCounts[word] = 1;
} else {
wordCounts[word]++;
}
}
let sditc;
if (sort === "ascending") {
sdict = Object.keys(wordCounts).sort(function(a,b) {return wordCounts[a]-wordCounts[b]});
} else {
sdict = Object.keys(wordCounts).sort(function(a,b) {return wordCounts[b]-wordCounts[a]});
}
let result = []
for (ki in sdict) {
result.push(sdict[ki] + ": " + wordCounts[sdict[ki]] + ",");
}
return result;
};
function word_similarity(word1, word2) {
// 调用词义相似度
return nlpClient.wordSimEmbedding(word1, word2).then(function(result) {
return result['score'];
}).catch(function(err) {
// 如果发生网络错误
console.log(err);
});
}
function text_similarity(text1, text2) {
// 如果有可选参数
var options = {};
// model 默认为"BOW",可选"BOW"、"CNN"与"GRNN"
options["model"] = "GRNN";
// 带参数调用短文本相似度
return nlpClient.simnet(text1, text2, options).then(function(result) {
return result['score'];
}).catch(function(err) {
// 如果发生网络错误
console.log(err);
});;
}
function ocr(imagePath) {
var fs = require('fs');
var image = fs.readFileSync(imagePath).toString("base64");
// 调用通用文字识别, 图片参数为本地图片
return ocrClient.generalBasic(image).then(function(result) {
let compactString = "";
for (let i = 0; i < result['words_result_num']; i++) {
compactString += result['words_result'][i]['words'];
}
return compactString;
}).catch(function(err) {
// 如果发生网络错误
console.log(err);
});
};
function stt(audioPath) {
let fs = require('fs');
let voice = fs.readFileSync(audioPath);
let voiceBuffer = new Buffer(voice);
// 识别本地文件
return speechClient.recognize(voiceBuffer, 'wav', 16000).then(function (result) {
let compactString = "";
result['result'].forEach(res => compactString = compactString + res);
return compactString;
}, function(err) {
console.log(err);
});
};
exports.w2v = w2v;
exports.word_segment = word_segment;
exports.word_frequency = word_frequency;
exports.word_similarity = word_similarity;
exports.text_similarity = text_similarity;
exports.ocr = ocr;
exports.stt = stt;