-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfer StringFormat.cs
More file actions
executable file
·81 lines (64 loc) · 3.21 KB
/
Infer StringFormat.cs
File metadata and controls
executable file
·81 lines (64 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.ML.Probabilistic.Tutorials
{
using System;
using Microsoft.ML.Probabilistic.Models;
using Microsoft.ML.Probabilistic.Factors.Attributes;
[Example("String tutorials", "Using StringFormat operation to reason about strings", Prefix = "2.")]
public class StringFormat
{
public void Run()
{
InferArgument();
InferTemplate();
}
private static void InferArgument()
{
var engine = new InferenceEngine();
engine.Compiler.RecommendedQuality = QualityBand.Experimental;
if (engine.Algorithm is Algorithms.ExpectationPropagation)
{
Variable<string> name = Variable.StringCapitalized().Named("name");
Variable<string> text = Variable.StringFormat("My name is {0}.", name).Named("text");
text.ObservedValue = "My name is John.";
Console.WriteLine("name is '{0}'", engine.Infer(name));
}
else
{
Console.WriteLine("This example only runs with Expectation Propagation");
}
}
private static void InferTemplate()
{
var engine = new InferenceEngine();
engine.Compiler.RecommendedQuality = QualityBand.Experimental;
if (engine.Algorithm is Algorithms.ExpectationPropagation)
{
Variable<string> name = Variable.StringCapitalized().Named("name");
Variable<string> template =
(Variable.StringUniform() + Variable.CharNonWord() + "{0}" + Variable.CharNonWord() + Variable.StringUniform()).Named("template");
Variable<string> text = Variable.StringFormat(template, name).Named("text");
text.ObservedValue = "Hello, mate! I'm Dave.";
Console.WriteLine("name is '{0}'", engine.Infer(name));
Console.WriteLine("template is '{0}'", engine.Infer(template));
text.ObservedValue = "Hi! My name is John.";
Console.WriteLine("name is '{0}'", engine.Infer(name));
Console.WriteLine("template is '{0}'", engine.Infer(template));
Variable<string> name2 = Variable.StringCapitalized().Named("name2");
Variable<string> text2 = Variable.StringFormat(template, name2).Named("text2");
text2.ObservedValue = "Hi! My name is Tom.";
Console.WriteLine("name is '{0}'", engine.Infer(name));
Console.WriteLine("name2 is '{0}'", engine.Infer(name2));
Console.WriteLine("template is '{0}'", engine.Infer(template));
Variable<string> text3 = Variable.StringFormat(template, "Boris").Named("text3");
Console.WriteLine("text3 is '{0}'", engine.Infer(text3));
}
else
{
Console.WriteLine("This example only runs with Expectation Propagation");
}
}
}
}