使用元组将IENUMERABLE从C#转换为VB

时间:2019-04-12 19:58:06

标签: c# vb.net tuples ienumerable

我希望我将问题表述为好,请原谅我对如果措辞不够准确的知识,如果您对如何更好地提出该问题有任何建议,请告诉我,我将其重述。

我正在按照Microsoft的新Microsoft.ML软件包上的指南进行操作:https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/sentiment-analysis

该指南基于C#构建,我正尝试转换为VB.NET。本指南的完整C#代码位于:https://github.com/dotnet/samples/blob/master/machine-learning/tutorials/SentimentAnalysis/Program.cs

除了几行内容外,我已经对所有内容进行了转换,而我只是缺乏有关如何完成此转换的知识:

第220行:

IEnumerable<(SentimentData sentiment, SentimentPrediction prediction)> sentimentsAndPredictions = sentiments.Zip(predictedResults, (sentiment, prediction) => (sentiment, prediction));

接着是#224至#228行:

foreach ((SentimentData sentiment, SentimentPrediction prediction) item in sentimentsAndPredictions)
            {
                Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(Convert.ToBoolean(item.prediction.Prediction) ? "Positive" : "Negative")} | Probability: {item.prediction.Probability} ");

            }

我以前从未使用过它,有人知道如何转换此代码吗?

作为最后的手段,我还尝试在converter.telerik.com上使用Telerik的转换工具,并收到以下错误消息:

''' Cannot convert ForEachStatementSyntax, CONVERSION ERROR: Conversion for TupleType not implemented, please report this issue in '(SentimentData sentiment, S...'

1 个答案:

答案 0 :(得分:0)

我终于能够弄清楚了,不是最简单的转换,但是它可以工作。如果有人对完整代码感兴趣,请参见下文。

    Dim sentimentsAndPredictions = sentiments.Zip(predictedResults, Function(sentiment As SentimentData, prediction As SentimentPrediction) (sentiment, prediction))

    For Each item In sentimentsAndPredictions
        Dim result = item.ToTuple
        Console.WriteLine("Sentiment: " & result.Item1.SentimentText & " | Prediction: " & If(Convert.ToBoolean(result.Item2.Prediction), "Positive", "Negative"))
    Next

请注意,在上面的代码中,我必须通过

Function(sentiment As SentimentData, prediction As SentimentPrediction) (sentiment, prediction)

进入Zip函数,然后在For语句中使用.ToTuple函数将该项转换为Tuple。

完整代码:

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports Microsoft.Data.DataView
Imports Microsoft.ML
Imports Microsoft.ML.Data
Imports Microsoft.ML.Trainers
Imports Microsoft.ML.Transforms.Text

Module Module1

    Public _dataPath As String = Path.Combine(Environment.CurrentDirectory, "Data", "yelp_labelled.txt")
    Public _modelPath As String = Path.Combine(Environment.CurrentDirectory, "Data", "Model.zip")

    Sub Main()

        Dim mlcontext As MLContext = New MLContext()
        Dim splitDataView As TrainCatalogBase.TrainTestData = LoadData(mlcontext)
        Dim model As ITransformer = BuildAndTrainModel(mlcontext, splitDataView.TrainSet)
        Evaluate(mlcontext, model, splitDataView.TestSet)
        UseModelWithSingleItem(mlcontext, model)

        UseLoadedModelWithBatchItems(mlcontext)

        Console.WriteLine()
        Console.WriteLine("=============== End of process ===============")

        Console.ReadLine()

    End Sub

    Public Function LoadData(ByVal mlContext As MLContext) As TrainCatalogBase.TrainTestData
        Dim dataView As IDataView = mlContext.Data.LoadFromTextFile(Of SentimentData)(_dataPath, hasHeader:=False)
        Dim splitDataView As TrainCatalogBase.TrainTestData = mlContext.BinaryClassification.TrainTestSplit(dataView, testFraction:=0.2)
        Return splitDataView
    End Function

    Public Function BuildAndTrainModel(ByVal mlContext As MLContext, ByVal splitTrainSet As IDataView) As ITransformer
        Dim pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName:=DefaultColumnNames.Features, inputColumnName:=NameOf(SentimentData.SentimentText)).Append(mlContext.BinaryClassification.Trainers.FastTree(numLeaves:=50, numTrees:=50, minDatapointsInLeaves:=20))
        Console.WriteLine("=============== Create and Train the Model ===============")
        Dim model = pipeline.Fit(splitTrainSet)
        Console.WriteLine("=============== End of training ===============")
        Console.WriteLine()
        Return model
    End Function

    Public Sub Evaluate(ByVal mlContext As MLContext, ByVal model As ITransformer, ByVal splitTestSet As IDataView)
        Console.WriteLine("=============== Evaluating Model accuracy with Test data===============")
        Dim predictions As IDataView = model.Transform(splitTestSet)
        Dim metrics As CalibratedBinaryClassificationMetrics = mlContext.BinaryClassification.Evaluate(predictions, "Label")
        Console.WriteLine()
        Console.WriteLine("Model quality metrics evaluation")
        Console.WriteLine("--------------------------------")
        Console.WriteLine($"Accuracy: {metrics.Accuracy}")
        Console.WriteLine($"Auc: {metrics.Auc}")
        Console.WriteLine($"F1Score: {metrics.F1Score}")
        Console.WriteLine("=============== End of model evaluation ===============")
        SaveModelAsFile(mlContext, model)
    End Sub

    Private Sub UseModelWithSingleItem(ByVal mlContext As MLContext, ByVal model As ITransformer)
        Dim predictionFunction As PredictionEngine(Of SentimentData, SentimentPrediction) = model.CreatePredictionEngine(Of SentimentData, SentimentPrediction)(mlContext)
        Dim sampleStatement As SentimentData = New SentimentData With {
            .SentimentText = "This was a very bad steak"
        }
        Dim resultprediction = predictionFunction.Predict(sampleStatement)
        Console.WriteLine()
        Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============")
        Console.WriteLine()
        Console.WriteLine($"Sentiment: {sampleStatement.SentimentText} | Prediction: {(If(Convert.ToBoolean(resultprediction.Prediction), "Positive", "Negative"))} | Probability: {resultprediction.Probability} ")
        Console.WriteLine("=============== End of Predictions ===============")
        Console.WriteLine()
    End Sub

    Public Sub UseLoadedModelWithBatchItems(ByVal mlContext As MLContext)
        Dim sentiments As IEnumerable(Of SentimentData) = {New SentimentData With {
            .SentimentText = "This was a horrible meal"
        }, New SentimentData With {
            .SentimentText = "I love this spaghetti."
        }}

        Dim loadedModel As ITransformer
        Using s1 As IO.FileStream = New FileStream(_modelPath, FileMode.Open, FileAccess.Read, FileShare.Read)
            loadedModel = mlContext.Model.Load(s1)
        End Using

        Dim sentimentStreamingDataView As IDataView = mlContext.Data.LoadFromEnumerable(sentiments)
        Dim predictions As IDataView = loadedModel.Transform(sentimentStreamingDataView)
        Dim predictedResults As IEnumerable(Of SentimentPrediction) = mlContext.Data.CreateEnumerable(Of SentimentPrediction)(predictions, reuseRowObject:=False)
        Console.WriteLine()

        Console.WriteLine("=============== Prediction Test of loaded model with a multiple samples ===============")

        Console.WriteLine()

        Dim sentimentsAndPredictions = sentiments.Zip(predictedResults, Function(sentiment As SentimentData, prediction As SentimentPrediction) (sentiment, prediction))

        For Each item In sentimentsAndPredictions
            Dim result = item.ToTuple
            Console.WriteLine("Sentiment: " & result.Item1.SentimentText & " | Prediction: " & If(Convert.ToBoolean(result.Item2.Prediction), "Positive", "Negative"))
        Next

    End Sub

    Private Sub SaveModelAsFile(ByVal mlContext As MLContext, ByVal model As ITransformer)
        Using fs = New FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write)
            mlContext.Model.Save(model, fs)
        End Using

        Console.WriteLine("The model is saved to {0}", _modelPath)
    End Sub

End Module