百度的SDK只需注册开发者后即可使用,python安装SDK的话只需先执行
pip install baidu-aip
即可
然后编写程序:
from aip import AipNlp
import sys
APP_ID = '11341452'
API_KEY = 'NLOckCSQfgHDgsvuOky4YYWZ'
SECRET_KEY = 'L2qyZbPHPWMvtibzr4z8F70PGMGiuRHv'
client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
# 命令行输入读取
# print('input first sentence')
# text1 = input()
# print('input second sentence')
# text2 = input()
#命令行参数读取
#python tt.py "I have a pen" "I have an apple"
text1 = sys.argv[1]
text2 = sys.argv[2]
#print(text1)
#print(text2)
ans = client.simnet(text1, text2);
print(ans["score"])
测试结果如图:
然后是C#脚本调用py文件的一个Demo:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;
using System.IO;
using System.Text;
using System;
public class testpy : MonoBehaviour {
//支持中文
// Use this for initialization
void Start () {
ProcessRun ();
}
// Update is called once per frame
void Update () {
}
public static void ProcessRun()
{
string args = "C://Users/Administrator/Desktop/tt.py \"I have a pen\" \"I have an apple\"" ;
RunPythonCmd(args);
}
private static void RunPythonCmd(string args)
{
ProcessStartInfo start = new ProcessStartInfo ();
start.FileName = "python";
start.Arguments = args;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using ( Process process = Process.Start(start))
{
using ( StreamReader reader = process.StandardOutput)
{
string result = "";
while (result != null)
{
result = reader.ReadLine();
print (result);
UnityEngine. Debug.Log(result);
}
}
}
}
}