当前位置:网站首页>#夏日挑战赛# 【FFH】从零开始的鸿蒙机器学习之旅-NLP情感分析
#夏日挑战赛# 【FFH】从零开始的鸿蒙机器学习之旅-NLP情感分析
2022-06-22 18:34:00 【51CTO】
什么!竟然有人想用鸿蒙做机器学习!
那又有何不可?最近一直在python进行RNN神经网络的训练,做一些情感分析,突发奇想,在鸿蒙上是否也有操作的空间?答案是肯定的,
本次我们将使用Stanford CoreNLP(斯坦福自然语言工具包)开发一个英文语句情感分析的简单应用。
0.效果展示


1.Stanford CoreNLP
Standford CoreNLP是斯坦福大学的自然语言处理工具包,提供了一套人类语言处理的工具,例如分词、词性标注、命名实体识别、句法树、依存句法分析等。Standford CoreNLP的源代码是利用JAVA编写的,也有封装了一整套的python工具包。
1.1 数据结构
- Annotations
CoreNLP里大多数工具都是利用Annotations这种数据结构进行输入输出,简单的通过传递字符串即可。 - Annotators
Annotators代表了具体的功能,分词,词性判断,断句等都分别对应一种Annotators。Annotators接收一个Annotation(我们传递字符串进去),就可以输出一个Annotation。
Stanford CoreNLP有非常多强大的自然语言处理功能,并且由于是纯JAVA库的原因,我们能非常容易的引入HarmonyOS开发中。
有兴趣的伙伴,可以细细研究: Standford - StanfordCoreNLP
StanfordCoreNLP是该工具包的接口类,大多数功能传参至该接口即可实现功能。
1.2 导入Standford CoreNLP库
1.2.1我们可以在官网下载工具包 StandfordCoreNLP

1.2.2解压,并引入lib中

右键文件夹,点击add as library
至此,准备工作都完成了,下面简单实现一个句子情感分析的功能
2.情感分析
2.1 新建JAVA类,NLP_EMOTION
前文提到,StanfordCoreNLP是实现功能的入口,我们需要实例化一个Properties对象,确定需要的语言处理功能,同时传入一个annotation对象即可实现功能。
这里使用的一些具体函数的构成关系在这里不进行赘述,感兴趣的读者可以进入官网细细研究,未来有机会我也将进一步从零分析和搭建RNN网络。
在sentiment_emotion中我们进行了情感分析,通常可以获得以下的一些信息:
- 情感评分:
emotion = RNNCoreAnnotations.getPredictedClass(tree)
返回int类型数据,1,2,3,4等,数字越大表示越积极,反之消极。 - 情感状态:
emotion_state = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
返回字符串,Positive(3),Very Positive(4),Negative(1),Neutral(2) - 断句后的分句。
- 具体效果:

至此,核心功能用法大概就这么多了,下面是分享一下测试demo的编写过程
3.测试DEMO
这里采用JS,JAVA混合开发模式,Ability类型的JS调用JAVA-PA,社区很多伙伴已经分享了相关的使用方法,这里不再赘述。
3.1 JAVA侧
- NLPServiceAbility
3.2 JS侧
- index.js
- index.hml
4.结语
关于机器学习内容还有非常多有意思的事情,这样的模式显然不是最佳的开发模式,5G大的工程文件(哈哈),最好能部署在云端,只能说实现一些功能,但非好用的功能,却也是一次尝试。在这个包下能够开发出很多有意思的功能,也支持中文等多种语言工具,还可以结合华为鸿蒙目前支持的AI功能,欢迎读者尝试和积极沟通。
或许,我们应该做一些更大胆的尝试?在HarmonyOS,OpenHarmony上从零搭建机器学习模型,再结合分布式能力,穷尽N多台设备的算力?也不知道手上的麒麟990能到何种程度。(嘻)
边栏推荐
猜你喜欢

510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming

图的定义及术语

Focal and global knowledge distillation for detectors

Nrf51822 peripheral learning

数字经济加速落地,能为中小企业带来什么?

0.0 - how can SolidWorks be uninstalled cleanly?

Fault analysis | from data_ Free exception

Some problem records of openpnp using process

Tree, forest and transformation of binary tree

商业智能BI数据仓库中的指标、维度和模型到底是什么?
随机推荐
Storage structure of graph (adjacency matrix)
Concordia University | volume product cycle network for reward generation in reinforcement learning
【深入理解TcaplusDB技术】TcaplusDB常规单据
佐治亚理工学院|具有服务质量保证的多无人机野火协同覆盖和跟踪规划
Calendar control programming
0.1----- process of drawing PCB with AD
自定义控件AutoScaleMode为Font造成宽度增加的问题
希尔排序
The custom control autoscalemode causes the problem of increasing the width of font
数字经济加速落地,能为中小企业带来什么?
Some problem records of openpnp using process
再谈SQL profile : 到底能不能固定执行计划?
Latest download address of libcef - compiled as MD dynamic link under vs2015
Array objects can be compared one by one (the original data with the same index and ID will be retained, and the data not in the original array will be added from the default list)
1.3-----Simplify 3D切片软件简单设置
Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘
【深入理解TcaplusDB技术】入门Tcaplus-JDBC开发
[petty bourgeoisie database] break down the concept: data, database, database system, database management system, database technology
[compréhension approfondie de la technologie tcaplusdb] exploitation et entretien de tcaplusdb - inspection quotidienne des patrouilles
delegate