CV工程师
2022-07-25 09:22:24 阅读:674
众所周知,在人工智能领域大放光彩的语言就是 python,然而python很多时候并不适合所有人,所有环境,因此,像一些人工智能框架在其它语言下也可以使用,今天就在nodejs下使用 natural 进行自然语言处理。
警告:亲测不支持中文!!!
首先新建一个项目:
mkdir nodejsAI
cd nodejsAI
npm init
// 一路回车
touch index.js
npm install natural
现在项目环境就算是完成了,接下来在 index.js
中写入示例代码:
import natural from 'natural';
const { BayesClassifier } = natural;
const classifier = new BayesClassifier();
classifier.addDocument('my unit-tests failed.', 'software');
classifier.addDocument('tried the program, but it was buggy.', 'software');
classifier.addDocument('the drive has a 2TB capacity.', 'hardware');
classifier.addDocument('i need a new power supply.', 'hardware');
classifier.train();
console.log(classifier.classify('did the tests pass?')); // software
console.log(classifier.classify('did you buy a new drive?')); // hardware
之后使用 node index.js
命令运行,不出意外的话会报错:
import natural from 'natural';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
小问题,不慌,在package.json中添加一行:"type": "module",
,就可以完美解决。重新运行就已经输出了我们想要的答案。
这是一个利用人工智能进行分类的官方示例,不过对我而言并没有实用价值,因为不支持中文,因此玩玩就好。
评论
扫描二维码获取文章详情
更多精彩内容尽在:WWW.ZNGG.NET