c#和python_C#与Python之间的通信?

在C#和Python之间的通信中,我遇到了一点小问题。

我使用以下恶意代码从C#将一些参数传递给Python:string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Substring(6);

string pyUnintelligibilityPath = "\\unintelligibility.py";

string pyNeuralPredictorPath = "\\predict.py";

string clf = "\\clf.pkl";

public double unintelligibleProbability(string pyLocation, string msg)

{

FileStream tempMessage = new FileStream(path + "\\tempMessage.txt", FileMode.Create);

StreamWriter writer = new StreamWriter(tempMessage);

writer.WriteLine(msg);

writer.Close();

string args = path + pyUnintelligibilityPath + " " + path + clf + " " + path + "\\tempMessage.txt" + " " + path + "\\tempCoefficient.txt";

ProcessStartInfo start = new ProcessStartInfo();

start.FileName = pyLocation;

start.Arguments = args;

start.UseShellExecute = false;

start.RedirectStandardOutput = false;

start.RedirectStandardError = false;

Process process = Process.Start(start);

Thread.Sleep(5000);

double unintelligibility = Convert.ToDouble(File.ReadAllText(path + "\\tempCoefficient.txt").Replace('.', ','));

return unintelligibility;

}from sklearn.feature_extraction.text import TfidfTransformer, CountVectorizer, TfidfVectorizer

from sklearn.linear_model import LogisticRegression, SGDClassifier

from sklearn.svm import LinearSVC

from sklearn.cross_validation import cross_val_score

from sklearn.pipeline import Pipeline

from sklearn.decomposition import NMF, TruncatedSVD

from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier

from sklearn.pipeline import FeatureUnion

from sklearn.externals import joblib

import numpy as np

import pandas as pd

import codecs

import sys

clf = joblib.load(sys.argv[1])

data = codecs.open(sys.argv[2], encoding='utf-8', mode='r')

text = data.readlines()

data.close()

text = [x.strip() for x in text]

f = open(sys.argv[3], mode='w')

proba = clf.predict_proba(text)

for i in range(0, len(text)):

meme = proba[i,:]

memeNum = meme[1]/(meme[0]+meme[1])

f.write(str(memeNum.round(4)) + "\n")

f.close()

我的问题是,是否有可能重新编写代码,使我能够在后台运行Python脚本,而C#只是将命令传递给它,因为每次需要处理单个消息时重新初始化脚本都要花费太长时间。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值