创新实训 调用百度SDK实现的句子相似度比较,以及C#运行py文件

百度的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);
                }

            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值