在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#只是将命令传递给它,因为每次需要处理单个消息时重新初始化脚本都要花费太长时间。