title: ML.Net n-gram word similarity
body: <p>Please tell me how you can implement the task of determining the similarity of words using n-grams on ML.Net? </p>
  
  <p>Example: there is the word "adidas". The user enters the word "adibas". I need to train the neural network in such a way that it understands that the user wanted to enter the word "adidas". How can this be implemented on ML.Net or Tensorflow? </p>
  
  <p>Here is an example that I implemented, but errors occur.</p>
  
  <p>`</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using Microsoft.ML;
  using Microsoft.ML.Data;
  using Microsoft.ML.Trainers;
  using Microsoft.ML.Transforms.Text;
  
  namespace NGram
  {
      class Program
      {
          static void Main(string[] args)
          {
              var mlContext = new MLContext();
  
              var samples = new List&lt;TextData&gt;()
              {
                  new TextData() {Text = "adidas"},
              };
  
              var dataview = mlContext.Data.LoadFromEnumerable(samples);
  
              var textPipeline = mlContext.Transforms.Text.TokenizeIntoCharactersAsKeys(
                      "Tokens",
                      "Text"
                  )
  
                  .Append(mlContext.Transforms.Conversion.MapValueToKey("Tokens"))
                  .AppendCacheCheckpoint(mlContext)
                  .Append(mlContext.Transforms.Text.ProduceNgrams("NgramFeatures",
                      "Tokens",
                      ngramLength: 2,
                      useAllLengths: false,
                      weighting: NgramExtractingEstimator.WeightingCriteria.Tf)
                  )
                  .Append(mlContext.Regression.Trainers.Sdca(labelColumnName: null, featureColumnName: "NgramFeatures"));
  
  
              // Fit to data.
              var textTransformer = textPipeline.Fit(dataview);
              var transformedDataView = textTransformer.Transform(dataview);
  
              var predictionEngine = mlContext.Model.CreatePredictionEngine&lt;TextData,
                  TransformedTextData&gt;(textTransformer);
  
              var prediction = predictionEngine.Predict(new TextData {Text = "fadida"});
  
              Console.WriteLine("Number of Features: " + prediction.NgramFeatures.Length);
  
              VBuffer&lt;ReadOnlyMemory&lt;char&gt;&gt; slotNames = default;
              transformedDataView.Schema["NgramFeatures"].GetSlotNames(ref slotNames);
  
              var NgramFeaturesColumn = transformedDataView.GetColumn&lt;VBuffer&lt;
                  float&gt;&gt;(transformedDataView.Schema["NgramFeatures"]);
              var slots = slotNames.GetValues();
              Console.Write("N-grams: ");
              foreach (var featureRow in NgramFeaturesColumn)
              {
                  foreach (var item in featureRow.Items())
                      Console.Write($"{slots[item.Key]}  ");
                  Console.WriteLine();
              }
  
              // Print the first 10 feature values.
              Console.Write("Features: ");
              for (int i = 0; i &lt; prediction.NgramFeatures.Length; i++)
                  Console.Write($"{prediction.NgramFeatures[i]:F4}  ");
  
              //  Expected output:
              //   Number of Features: 52
              //   N-grams:   This|is|an  is|an|example  an|example|to  example|to|compute  to|compute|n-grams.  N-gram|is|a  is|a|sequence  a|sequence|of  sequence|of|'N'  of|'N'|consecutive  ...
              //   Features:     1.0000      1.0000          1.0000           1.0000             1.0000            0.0000      0.0000          0.0000          0.0000          0.0000          ...
  
              // var trainer = mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label",
              //     featureColumnName: "NgramFeatures");
          }
  
          private class TextData
          {
              [LoadColumn(0)] 
              public bool Label { get; set; }
  
              [LoadColumn(1)]
              public string Text { get; set; }
          }
  
          private class TransformedTextData : TextData
          {
              public float[] NgramFeatures { get; set; }
          }
      }
  }
  </code></pre>
  
  <p>Thanks.</p>