Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformatting misc. samples to width 85 #3949

Merged
merged 1 commit into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 61 additions & 23 deletions docs/samples/Microsoft.ML.Samples/Dynamic/NgramExtraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ public static partial class TransformSamples
{
public static void Example()
{
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
// as well as the source of randomness.
// Create a new ML context, for ML.NET operations. It can be used for
// exception tracking and logging, as well as the source of randomness.
var ml = new MLContext();

// Get a small dataset as an IEnumerable and convert to IDataView.
var data = new List<SampleSentimentData>() {
new SampleSentimentData { Sentiment = true, SentimentText = "Best game I've ever played." },
new SampleSentimentData { Sentiment = false, SentimentText = "==RUDE== Dude, 2" },
new SampleSentimentData { Sentiment = true, SentimentText = "Until the next game, this is the best Xbox game!" } };
new SampleSentimentData { Sentiment = true,
SentimentText = "Best game I've ever played." },

new SampleSentimentData { Sentiment = false,
SentimentText = "==RUDE== Dude, 2" },

new SampleSentimentData { Sentiment = true,
SentimentText = "Until the next game," +
"this is the best Xbox game!" } };

// Convert IEnumerable to IDataView.
var trainData = ml.Data.LoadFromEnumerable(data);
Expand All @@ -29,23 +35,42 @@ public static void Example()
// false ==RUDE== Dude, 2.
// true Until the next game, this is the best Xbox game!

// A pipeline to tokenize text as characters and then combine them together into n-grams
// The pipeline uses the default settings to featurize.
// A pipeline to tokenize text as characters and then combine them
// together into n-grams. The pipeline uses the default settings to
// featurize.

var charsPipeline = ml.Transforms.Text
.TokenizeIntoCharactersAsKeys("Chars", "SentimentText",
useMarkerCharacters: false);

var ngramOnePipeline = ml.Transforms.Text
.ProduceNgrams("CharsUnigrams", "Chars", ngramLength: 1);

var charsPipeline = ml.Transforms.Text.TokenizeIntoCharactersAsKeys("Chars", "SentimentText", useMarkerCharacters: false);
var ngramOnePipeline = ml.Transforms.Text.ProduceNgrams("CharsUnigrams", "Chars", ngramLength: 1);
var ngramTwpPipeline = ml.Transforms.Text.ProduceNgrams("CharsTwograms", "Chars");
var oneCharsPipeline = charsPipeline.Append(ngramOnePipeline);
var twoCharsPipeline = charsPipeline.Append(ngramTwpPipeline);
var ngramTwpPipeline = ml.Transforms.Text
.ProduceNgrams("CharsTwograms", "Chars");

var oneCharsPipeline = charsPipeline
.Append(ngramOnePipeline);

var twoCharsPipeline = charsPipeline
.Append(ngramTwpPipeline);

// The transformed data for pipelines.
var transformedData_onechars = oneCharsPipeline.Fit(trainData).Transform(trainData);
var transformedData_twochars = twoCharsPipeline.Fit(trainData).Transform(trainData);
var transformedData_onechars = oneCharsPipeline.Fit(trainData)
.Transform(trainData);

var transformedData_twochars = twoCharsPipeline.Fit(trainData)
.Transform(trainData);

// Small helper to print the text inside the columns, in the console.
Action<string, IEnumerable<VBuffer<float>>, VBuffer<ReadOnlyMemory<char>>> printHelper = (columnName, column, names) =>
Action<string, IEnumerable<VBuffer<float>>,
VBuffer<ReadOnlyMemory<char>>>
printHelper = (columnName, column, names) =>

{
Console.WriteLine($"{columnName} column obtained post-transformation.");
Console.WriteLine(
$"{columnName} column obtained post-transformation.");

var slots = names.GetValues();
foreach (var featureRow in column)
{
Expand All @@ -54,21 +79,33 @@ public static void Example()
Console.WriteLine("");
}

Console.WriteLine("===================================================");
Console.WriteLine(
"===================================================");
};
// Preview of the CharsUnigrams column obtained after processing the input.
// Preview of the CharsUnigrams column obtained after processing the
// input.
VBuffer<ReadOnlyMemory<char>> slotNames = default;
transformedData_onechars.Schema["CharsUnigrams"].GetSlotNames(ref slotNames);
var charsOneGramColumn = transformedData_onechars.GetColumn<VBuffer<float>>(transformedData_onechars.Schema["CharsUnigrams"]);
transformedData_onechars.Schema["CharsUnigrams"]
.GetSlotNames(ref slotNames);

var charsOneGramColumn = transformedData_onechars
.GetColumn<VBuffer<float>>(transformedData_onechars
.Schema["CharsUnigrams"]);

printHelper("CharsUnigrams", charsOneGramColumn, slotNames);

// CharsUnigrams column obtained post-transformation.
// 'B' - 1 'e' - 6 's' - 1 't' - 1 '<?>' - 4 'g' - 1 'a' - 2 'm' - 1 'I' - 1 ''' - 1 'v' - 2 ...
// 'e' - 1 '<?>' - 2 'd' - 1 '=' - 4 'R' - 1 'U' - 1 'D' - 2 'E' - 1 'u' - 1 ',' - 1 '2' - 1
// 'B' - 0 'e' - 6 's' - 3 't' - 6 '<?>' - 9 'g' - 2 'a' - 2 'm' - 2 'I' - 0 ''' - 0 'v' - 0 ...
// Preview of the CharsTwoGrams column obtained after processing the input.
var charsTwoGramColumn = transformedData_twochars.GetColumn<VBuffer<float>>(transformedData_twochars.Schema["CharsTwograms"]);
transformedData_twochars.Schema["CharsTwograms"].GetSlotNames(ref slotNames);
var charsTwoGramColumn = transformedData_twochars
.GetColumn<VBuffer<float>>(transformedData_twochars
.Schema["CharsTwograms"]);

transformedData_twochars.Schema["CharsTwograms"]
.GetSlotNames(ref slotNames);

printHelper("CharsTwograms", charsTwoGramColumn, slotNames);

// CharsTwograms column obtained post-transformation.
Expand All @@ -78,7 +115,8 @@ public static void Example()
}

/// <summary>
/// A dataset that contains a tweet and the sentiment assigned to that tweet: 0 - negative and 1 - positive sentiment.
/// A dataset that contains a tweet and the sentiment assigned to that
/// tweet: 0 - negative and 1 - positive sentiment.
/// </summary>
public class SampleSentimentData
{
Expand Down
Loading