赞
踩
目录
关于自己给自己桌宠接GPT后想反推出来文本的情绪状态方案
vader —— 一种基于规则的英文文本情感识别方法_vader算法-CSDN博客
VADER是一种基于词库和语法规则来进行文本情感分析的方法,目前除了基本的情感词语分析外,已能对表情符号(utf-8)等分析进行支持。
创建新Unity项目
创建StreamingAssets文件夹
创建脚本文件夹
#如果有用过Odin插件也可以先导入//方便测试用,没有也不要紧
Python导入包vaderSentiment
Python导入包translate
- pip3 install vaderSentiment
- pip3 install translate
转摘:基于词库和语法规则的情感识别方法——vader_vader 情感分析的情感得分范围是多少-CSDN博客
代码如下(示例):
- from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
- import re
- import sys
- from translate import Translator
-
- url_google = 'http://translate.google.cn'
- reg_text = re.compile(r'(?<=TRANSLATED_TEXT=).*?;')
- user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' \
- r'Chrome/44.0.2403.157 Safari/537.36'
-
- def translate_cn_api(content):
- translator= Translator(to_lang="zh")
- translation = translator.translate(content)
- return translation
-
- def translate_en_api(content):
- translator= Translator(to_lang="en",from_lang='zh')
- translation = translator.translate(content)
- return translation
-
- def print_sentiment_scores(tweets):
- analyser = SentimentIntensityAnalyzer()
- vadersenti = analyser.polarity_scores(tweets)
- return vadersenti['compound']
-
- def main(question):
- #英文翻译
- questionEn = translate_en_api(question)
- print("问题英文版:", questionEn)
- #情感评分
- result = print_sentiment_scores(questionEn)
- #将情感评分-1至1的区间转换到0-1的区间。
- X_scale = (1 + result) / 2.0
- print("情感评分:", X_scale)
-
- if __name__ == '__main__':
- question=input("请输入你的问题:")
- print("问题中文版:", question)
- main(question)
在PyCharm里面右键运行脚本测试,如下图测试成功
转摘:
Unity直接调用Python脚本_unity 导出工程后 执行py脚本-CSDN博客
代码如下(示例):
- using System.Collections;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Diagnostics; //需要添加这个名词空间,调用DataReceivedEventArg
- using System.IO;
- using UnityEditor.Scripting.Python;
- using UnityEditor;
- using Sirenix.OdinInspector;
-
- public class LoadPython : MonoBehaviour
- {
- string sArguments = @"UnityLoad.py";//这里是python的文件名字
-
-
- //这个是基于Odin插件的,如果没用过就用UI的InputField的函数调用也可以
- [Button]
- public void Test(string str)
- {
-
-
- string[] strs = new string[] { str };
- UnityEngine.Debug.Log("检测文本"+ str);
- UnityEngine.Debug.Log("调用PY脚本");//e.Data);
- RunPython(Path.Combine(Application.streamingAssetsPath, sArguments), strs);
- }
-
- public Process process;
- /// <summary>
- ///
- /// </summary>
- /// <param name="path">python代码文件路径</param>
- /// <param name="argvs">需要传递的参数数组</param>
- private void RunPython(string path, string[] argvs)
- {
-
- if (process != null)
- {
- process.Close();
- //process.Kill();
- process = null;
- }
-
-
-
- string arguments = path;
- if (argvs != null)
- {
- foreach (string item in argvs)
- {
- arguments += (" " + item);
- }
- }
- else
- {
- //return;
- }
- UnityEngine.Debug.Log(arguments);
-
- if (process == null)
- process = new Process();
- process.StartInfo.FileName = @"F:\OTK\Anaconda\python.exe";//Python代码解释器路径,写自己的路径
- process.StartInfo.UseShellExecute = false;//在python中使用sys.argv[]设置为true
- process.StartInfo.Arguments = arguments;//python脚本文件路径+参数(参数用空格隔开)
- process.StartInfo.RedirectStandardError = true;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.CreateNoWindow = true;//是否创建窗口
-
-
- process.Start();
- process.BeginOutputReadLine();
- process.OutputDataReceived += new DataReceivedEventHandler(GetData);
- process.WaitForExit();
- }
-
- /// <summary>
- /// 用于接收python返回的数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void GetData(object sender, DataReceivedEventArgs e)
- {
-
- if (string.IsNullOrEmpty(e.Data) == false)
- {
- UnityEngine.Debug.Log("情感评分:" + e.Data);
- }
- }
-
- private void OnDestroy()
- {
- if (process != null)
- process.Close();
-
- }
-
- }
修改PY的脚本,让传入的参数为sys.argv[1],我们从Unity传入用C#传入参数
- import sys
- question = sys.argv[1]
- from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
- import re
- from translate import Translator
-
- url_google = 'http://translate.google.cn'
- reg_text = re.compile(r'(?<=TRANSLATED_TEXT=).*?;')
- user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' \
- r'Chrome/44.0.2403.157 Safari/537.36'
-
-
- def translate_cn_api(content):
- translator = Translator(to_lang="zh")
- translation = translator.translate(content)
- return translation
-
-
- def translate_en_api(content):
- translator = Translator(to_lang="en", from_lang='zh')
- translation = translator.translate(content)
- return translation
-
-
- def print_sentiment_scores(tweets):
- analyser = SentimentIntensityAnalyzer()
- vadersenti = analyser.polarity_scores(tweets)
- return vadersenti['compound']
-
-
- def main(question):
- #print("startMain")
- # 英文翻译
- questionEn = translate_en_api(question)
- #print("问题英文版:", questionEn)
- # 情感评分
- result = print_sentiment_scores(questionEn)
- # 将情感评分-1至1的区间转换到0-1的区间。
- X_scale = (1 + result) / 2.0
- print(X_scale)
-
-
- # if __name__ == '__main__':
- # question = input("请输入你的问题:")
- # print("问题中文版:", question)
- main(question)
Unity中创建新GameObject挂上脚本,测试函数
VADER会对文本分析,得到的结果是一个字典信息,包含
pos,文本中正面信息得分
neg,文本中负面信息得分
neu,文本中中性信息得分
compound,文本综合情感得分
所以我们可以利用返回的参数做一些简单的情绪状态机
PS:现脚本是用C#调用Python解析器跑Py脚本的,所以需要Python解析器的环境,个人没怎么学过PY,好像要完全支持本地需要把PY打包成EXE,然后EXE传参数。(好像PY打包挺麻烦的,就不在这里多解释了)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。