Ejemplo n.º 1
0
 public void TestJsonSimpleLabel()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("1 |a foo:1", "{\"_label\":{\"Label\":1},\"a\":{\"foo\":1}}", VowpalWabbitLabelComparator.Simple);
     }
 }
Ejemplo n.º 2
0
        public void TestJsonLabel()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator(""))
            {
                validator.Validate("1 | a:2 ", "{\"a\":2,\"_label_Label\":1}");
            }

            using (var validator = new VowpalWabbitExampleJsonValidator(new VowpalWabbitSettings
                {
                    Arguments = "--cb_adf",
                    PropertyConfiguration = new PropertyConfiguration
                    {
                        MultiProperty = "adf",
                        TextProperty = "someText",
                        FeatureIgnorePrefix = "xxx"
                    }
                }))
            {
                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "0:-1:.3 | w2 w3"
                    },
                    "{\"Age\":25,\"adf\":[{\"someText\":\"w1 w2\", \"a\":{\"x\":1}, \"xxxxIgnoreMe\":2}, {\"someText\":\"w2 w3\"}], \"_labelIndex\":1, \"_label_Cost\":-1, \"_label_Probability\":0.3}",
                    VowpalWabbitLabelComparator.ContextualBandit);
            }
        }
Ejemplo n.º 3
0
        public void TestJsonDict()
        {
            var vec = new float[] { 1, 2, 3 };

            var jsonResolver = new RefResolve();
            var settings     = new JsonSerializerSettings {
                ReferenceResolverProvider = () => jsonResolver
            };

            var ctx1 = new Context(vec, 1, settings);
            var ctx2 = new Context(vec, 2, settings);

            using (var vw = new VowpalWabbit(new VowpalWabbitSettings {
                EnableStringExampleGeneration = true
            }))
                using (var resolver = new VowpalWabbitJsonReferenceResolver(serializer => Assert.Fail()))
                    using (var serializer1 = new VowpalWabbitJsonSerializer(vw, resolver))
                        using (var example1 = serializer1.ParseAndCreate(ctx1.JSON))
                            using (var serializer2 = new VowpalWabbitJsonSerializer(vw, resolver))
                                using (var example2 = serializer2.ParseAndCreate(ctx2.JSON))
                                    using (var validator = new VowpalWabbitExampleJsonValidator())
                                    {
                                        validator.Validate("| Id:1 :1 :2 :3", example1);
                                        validator.Validate(ctx1.VW, example1);
                                        validator.Validate("| Id:2 :1 :2 :3", example2);
                                        validator.Validate(ctx2.VW, example2);
                                    }
        }
Ejemplo n.º 4
0
        public void TestJsonLabel()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator(""))
            {
                validator.Validate("1 | a:2 ", "{\"a\":2,\"_label_Label\":1}");
            }

            using (var validator = new VowpalWabbitExampleJsonValidator(new VowpalWabbitSettings
            {
                Arguments = "--cb_adf",
                PropertyConfiguration = new PropertyConfiguration
                {
                    MultiProperty = "adf",
                    TextProperty = "someText",
                    FeatureIgnorePrefix = "xxx"
                }
            }))
            {
                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "0:-1:.3 | w2 w3"
                },
                                   "{\"Age\":25,\"adf\":[{\"someText\":\"w1 w2\", \"a\":{\"x\":1}, \"xxxxIgnoreMe\":2}, {\"someText\":\"w2 w3\"}], \"_labelIndex\":1, \"_label_Cost\":-1, \"_label_Probability\":0.3}",
                                   VowpalWabbitLabelComparator.ContextualBandit);
            }
        }
Ejemplo n.º 5
0
 public void TestJsonSimpleLabel()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("1 |a foo:1", "{\"_label\":{\"Label\":1},\"a\":{\"foo\":1}}", VowpalWabbitLabelComparator.Simple);
     }
 }
Ejemplo n.º 6
0
 public void TestJsonText()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator(""))
     {
         validator.Validate("| a b c |a d e f", "{\"_text\":\"a b c\",\"a\":{\"_text\":\"d e f\"}}");
     }
 }
Ejemplo n.º 7
0
        public void TestJsonToVWString()
        {
            var jsonContext = new JsonContext()
            {
                Label = new SimpleLabel
                {
                    Label = 25
                },
                Ns1 = new Namespace1
                {
                    Foo          = 1,
                    Age          = "25",
                    DontConsider = "XXX"
                },
                Ns2 = new Namespace2
                {
                    FeatureA = true
                },
                Clicks = 5
            };

            var jsonContextString = JsonConvert.SerializeObject(jsonContext);

            using (var validator = new VowpalWabbitExampleJsonValidator(""))
            {
                validator.Validate("25 |a Bar:1 Age25 |b Marker | Clicks:5 MoreClicks:0",
                                   jsonContextString,
                                   VowpalWabbitLabelComparator.Simple);
            }
        }
Ejemplo n.º 8
0
 public void TestJsonArray()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("| :1 :2.3 :4", "{\"a\":[1,2.3,4]}");
         validator.Validate("|a :1 :2.3 :4", "{\"a\":{\"b\":[1,2.3,4]}}");
     }
 }
Ejemplo n.º 9
0
 public void TestJsonArray()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("| :1 :2.3 :4", "{\"a\":[1,2.3,4]}");
         validator.Validate("|a :1 :2.3 :4", "{\"a\":{\"b\":[1,2.3,4]}}");
     }
 }
Ejemplo n.º 10
0
 public void TestJsonAux()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1},\"_aux\":{\"abc\":{\"def\":3}}}");
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1},\"_aux\":5}");
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1},\"_aux\":[1,2,[3,4],2]}");
     }
 }
Ejemplo n.º 11
0
 public void TestJsonAux()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1},\"_aux\":{\"abc\":{\"def\":3}}}");
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1},\"_aux\":5}");
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1},\"_aux\":[1,2,[3,4],2]}");
     }
 }
Ejemplo n.º 12
0
 public void TestJson()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1}}");
         validator.Validate("|a foo:2.3", "{\"a\":{\"foo\":2.3}}");
         validator.Validate("|a foo:2.3 bar", "{\"a\":{\"foo\":2.3, \"bar\":true}}");
         validator.Validate("|a foo:1 |bcd Age25_old", "{\"a\":{\"foo\":1},\"bcd\":{\"Age\":\"25 old\"}}");
     }
 }
Ejemplo n.º 13
0
 public void TestJsonContextualBanditLabel()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
     {
         //validator.Validate("1:-2:.3 |a foo:1",
         //    "{\"_label\":{\"Action\":1,\"Cost\":-2,\"Probability\":.3},\"a\":{\"foo\":1}}",
         //    VowpalWabbitLabelComparator.ContextualBandit);
         validator.Validate("1:2:.5 |a foo:1", "{\"_label\":\"1:2:.5\",\"a\":{\"foo\":1}}", VowpalWabbitLabelComparator.ContextualBandit);
     }
 }
Ejemplo n.º 14
0
 public void TestJsonContextualBanditLabel()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
     {
         //validator.Validate("1:-2:.3 |a foo:1",
         //    "{\"_label\":{\"Action\":1,\"Cost\":-2,\"Probability\":.3},\"a\":{\"foo\":1}}",
         //    VowpalWabbitLabelComparator.ContextualBandit);
         validator.Validate("1:2:.5 |a foo:1", "{\"_label\":\"1:2:.5\",\"a\":{\"foo\":1}}", VowpalWabbitLabelComparator.ContextualBandit);
     }
 }
Ejemplo n.º 15
0
 public void TestJson()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("|a foo:1", "{\"a\":{\"foo\":1}}");
         validator.Validate("|a foo:2.3", "{\"a\":{\"foo\":2.3}}");
         validator.Validate("|a foo:2.3 bar", "{\"a\":{\"foo\":2.3, \"bar\":true}}");
         validator.Validate("|a foo:1 |bcd Age25_old", "{\"a\":{\"foo\":1},\"bcd\":{\"Age\":\"25 old\"}}");
     }
 }
Ejemplo n.º 16
0
 public void ValidateExample(VowpalWabbitExampleCollection example, Context ctx)
 {
     using (var vw = new VowpalWabbit(new VowpalWabbitSettings {
         EnableStringExampleGeneration = true
     }))
         using (var validator = new VowpalWabbitExampleJsonValidator())
         {
             var singleExample = (VowpalWabbitSingleLineExampleCollection)example;
             validator.Validate(ctx.VW, example);
         }
 }
Ejemplo n.º 17
0
 public void TestJsonSimpleLabelOverride()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("2 |a foo:1", "{\"_label\":{\"Label\":1},\"a\":{\"foo\":1}}", VowpalWabbitLabelComparator.Simple,
                            new SimpleLabel {
             Label = 2
         },
                            enableNativeJsonValidation: false /* vw.Parse(json) doesn't support label overwrite */);
     }
 }
Ejemplo n.º 18
0
        public void TestJsonArray()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator())
            {
                validator.Validate("| :1 :2.3 :4", "{\"a\":[1,2.3,4]}");
                validator.Validate("|a :1 :2.3 :4", "{\"a\":{\"b\":[1,2.3,4]}}");
            }

            using (var vw = new VowpalWabbit("--json"))
            {
                AssertThrow(() => vw.ParseJson("{\"a\":{\"b\":[1,[1,2],4]}}"));
            }
        }
Ejemplo n.º 19
0
        public void TestJsonArray()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator())
            {
                validator.Validate("| :1 :2.3 :4", "{\"a\":[1,2.3,4]}");
                validator.Validate("|a :1 :2.3 :4", "{\"a\":{\"b\":[1,2.3,4]}}");
            }

            using (var vw = new VowpalWabbit("--json"))
            {
                AssertThrow(() => vw.ParseJson("{\"a\":{\"b\":[1,[1,2],4]}}"));
            }
        }
Ejemplo n.º 20
0
 public void TestJsonADF()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator("--cb_adf"))
     {
         validator.Validate(new[]
         {
             "shared | foo:2",
             "1:-2:.3 | foo:1"
         },
                            "{\"_label_Action\":1,\"_label_Cost\":-2,\"_label_Probability\":0.3,\"_multi\":[{\"foo\":1}],\"foo\":2,\"_labelIndex\":0}",
                            VowpalWabbitLabelComparator.ContextualBandit,
                            index: 1);
     }
 }
Ejemplo n.º 21
0
 public void TestJsonADF()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator("--cb_adf"))
     {
         validator.Validate(new[]
             {
                 "shared | foo:2",
                 "1:-2:.3 | foo:1"
             },
             "{\"_label_Action\":1,\"_label_Cost\":-2,\"_label_Probability\":0.3,\"_multi\":[{\"foo\":1}],\"foo\":2,\"_labelIndex\":0}",
             VowpalWabbitLabelComparator.ContextualBandit,
             index: 1);
     }
 }
Ejemplo n.º 22
0
        public void TestJsonRedirection()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator(new VowpalWabbitSettings("--cb_adf")))
            {
                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "0:-1:.3 | w2 w3"
                },
                                   "{\"_ignoreMe\":5,\"_sub\":{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}, \"_labelIndex\":1, \"_label_Cost\":-1, \"_label_Probability\":0.3}",
                                   VowpalWabbitLabelComparator.ContextualBandit,
                                   extension: (state, property) =>
                {
                    if (!property.Equals("_sub"))
                    {
                        return(false);
                    }

                    Assert.AreEqual(state.MultiIndex, -1);

                    state.Parse();

                    return(true);
                });

                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "0:-1:.3 | w2 w3"
                },
                                   "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\", \"_tag\":\"2\"}], \"_labelIndex\":1, \"_label_Cost\":-1, \"_label_Probability\":0.3}",
                                   VowpalWabbitLabelComparator.ContextualBandit,
                                   extension: (state, property) =>
                {
                    if (!property.Equals("_tag"))
                    {
                        return(false);
                    }

                    var tag = state.Reader.ReadAsString();

                    Assert.AreEqual(1, state.MultiIndex);
                    Assert.AreEqual("2", tag);

                    return(true);
                });
            }
        }
Ejemplo n.º 23
0
        public void TestJsonNumADFs()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator(""))
            {
                Assert.AreEqual(2,
                                VowpalWabbitJsonSerializer.GetNumberOfActionDependentExamples(
                                    "{\"_text\":\"a b c\",\"a\":{\"_text\":\"d e f\"},_multi:[{\"a\":1},{\"b\":2,\"c\":3}]}"));

                Assert.AreEqual(0,
                                VowpalWabbitJsonSerializer.GetNumberOfActionDependentExamples(
                                    "{\"_text\":\"a b c\",\"a\":{\"_text\":\"d e f\"},_multi:[]}"));

                Assert.AreEqual(0,
                                VowpalWabbitJsonSerializer.GetNumberOfActionDependentExamples(
                                    "{\"_text\":\"a b c\",\"a\":{\"_text\":\"d e f\"}}"));
            }
        }
Ejemplo n.º 24
0
        public void TestJsonMultiline()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
            {
                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    " | w2 w3"
                },
                                   "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}");

                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "2:-1:.3 | w2 w3"
                },
                                   "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\",\"_label\":\"2:-1:.3\"}]}",
                                   VowpalWabbitLabelComparator.ContextualBandit);
            }

            using (var validator = new VowpalWabbitExampleJsonValidator(
                       new VowpalWabbitSettings
            {
                Arguments = "--cb 2 --cb_type dr",
                PropertyConfiguration = new PropertyConfiguration
                {
                    MultiProperty = "adf",
                    TextProperty = "someText",
                    LabelProperty = "theLabel",
                    FeatureIgnorePrefix = "xxx"
                }
            }))
            {
                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "2:-1:.3 | w2 w3"
                },
                                   "{\"Age\":25,\"adf\":[{\"someText\":\"w1 w2\", \"a\":{\"x\":1}, \"xxxxIgnoreMe\":2}, {\"someText\":\"w2 w3\",\"theLabel\":\"2:-1:.3\"}]}",
                                   VowpalWabbitLabelComparator.ContextualBandit,
                                   enableNativeJsonValidation: false /* remapping of special properties is not supported in native JSON */);
            }
        }
Ejemplo n.º 25
0
        public void TestJsonDictReverse()
        {
            var vec = new float[] { 1, 2, 3 };

            var jsonResolver = new RefResolve();
            var settings     = new JsonSerializerSettings {
                ReferenceResolverProvider = () => jsonResolver
            };

            var ctx1 = new Context(vec, 1, settings);
            var ctx2 = new Context(vec, 2, settings);

            VowpalWabbitJsonSerializer delayedSerializer = null;

            using (var validator = new VowpalWabbitExampleJsonValidator())
                using (var vw = new VowpalWabbit(new VowpalWabbitSettings {
                    EnableStringExampleGeneration = true
                }))
                    using (var resolver = new VowpalWabbitJsonReferenceResolver(serializer => delayedSerializer = serializer))
                    {
                        var serializer2 = new VowpalWabbitJsonSerializer(vw, resolver);
                        var example2    = serializer2.ParseAndCreate(ctx2.JSON);

                        // incomplete data
                        Assert.IsNull(example2);

                        // triggers example2 completion
                        using (var serializer1 = new VowpalWabbitJsonSerializer(vw, resolver))
                            using (var example1 = serializer1.ParseAndCreate(ctx1.JSON))
                            {
                                validator.Validate("| Id:1 :1 :2 :3", example1);
                            }

                        Assert.IsNotNull(delayedSerializer);

                        using (var delayedExample2 = delayedSerializer.CreateExamples())
                        {
                            validator.Validate("| Id:2 :1 :2 :3", delayedExample2);
                        }

                        delayedSerializer.Dispose();
                    }
        }
Ejemplo n.º 26
0
        public void TestJsonMultiline()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
            {
                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    " | w2 w3"
                },
                                   "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}");

                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "2:-1:.3 | w2 w3"
                },
                                   "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\",\"_label\":\"2:-1:.3\"}]}",
                                   VowpalWabbitLabelComparator.ContextualBandit);
            }
        }
Ejemplo n.º 27
0
        public void TestJsonToVWString()
        {
            var jsonContext = new JsonContext()
            {
                Label = new SimpleLabel
                {
                    Label = 25
                },
                Ns1 = new Namespace1
                {
                    Foo                   = 1,
                    Age                   = "25",
                    DontConsider          = "XXX",
                    EscapeCharacterString = "a: a | a\ta",
                    EscapeCharactersText  = "b: b | b\tb"
                },
                Ns2 = new Namespace2
                {
                    FeatureA = true
                },
                Clicks = 5
            };

            var jsonContextString = JsonConvert.SerializeObject(jsonContext);

            using (var validator = new VowpalWabbitExampleJsonValidator(new VowpalWabbitSettings
            {
                Arguments = "--json",
                EnableStringExampleGeneration = true,
                EnableStringFloatCompact = true,
                EnableThreadSafeExamplePooling = true
            }))
            {
                validator.Validate("25  |  Clicks:5 MoreClicks:0  |a Bar:1 Age25 EscapeCharacterStringa__a___a_a b_ b _ b b  |b Marker",
                                   jsonContextString,
                                   VowpalWabbitLabelComparator.Simple);
            }
        }
Ejemplo n.º 28
0
        public void TestJsonMultiline()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
            {
                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    " | w2 w3"
                },
                                   "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}");

                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "2:-1:.3 | w2 w3"
                },
                                   "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\",\"_label\":\"2:-1:.3\"}]}",
                                   VowpalWabbitLabelComparator.ContextualBandit);
            }

            using (var validator = new VowpalWabbitExampleJsonValidator(
                       new VowpalWabbitSettings(
                           "--cb 2 --cb_type dr",
                           propertyConfiguration: new PropertyConfiguration(
                               multiProperty: "adf",
                               textProperty: "someText",
                               labelProperty: "theLabel",
                               featureIgnorePrefix: "xxx"))))
            {
                validator.Validate(new[] {
                    "shared | Age:25",
                    " | w1 w2 |a x:1",
                    "2:-1:.3 | w2 w3"
                },
                                   "{\"Age\":25,\"adf\":[{\"someText\":\"w1 w2\", \"a\":{\"x\":1}, \"xxxxIgnoreMe\":2}, {\"someText\":\"w2 w3\",\"theLabel\":\"2:-1:.3\"}]}",
                                   VowpalWabbitLabelComparator.ContextualBandit);
            }
        }
Ejemplo n.º 29
0
        public void TestJsonMultiline()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
            {
                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     " | w2 w3"
                    },
                    "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}");

                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "2:-1:.3 | w2 w3"
                    },
                    "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\",\"_label\":\"2:-1:.3\"}]}",
                    VowpalWabbitLabelComparator.ContextualBandit);
            }

            using (var validator = new VowpalWabbitExampleJsonValidator(
                new VowpalWabbitSettings(
                    "--cb 2 --cb_type dr",
                    propertyConfiguration: new PropertyConfiguration(
                        multiProperty: "adf",
                        textProperty: "someText",
                        labelProperty: "theLabel",
                        featureIgnorePrefix: "xxx"))))
            {
                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "2:-1:.3 | w2 w3"
                    },
                    "{\"Age\":25,\"adf\":[{\"someText\":\"w1 w2\", \"a\":{\"x\":1}, \"xxxxIgnoreMe\":2}, {\"someText\":\"w2 w3\",\"theLabel\":\"2:-1:.3\"}]}",
                    VowpalWabbitLabelComparator.ContextualBandit);
            }
        }
Ejemplo n.º 30
0
        public void TestJsonMultiline()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
            {
                validator.Validate(new[] { 
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     " | w2 w3"
                    },
                    "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}");

                validator.Validate(new[] { 
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "2:-1:.3 | w2 w3"
                    },
                    "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\",\"_label\":\"2:-1:.3\"}]}",
                    VowpalWabbitLabelComparator.ContextualBandit);
            }
        }
Ejemplo n.º 31
0
 public void TestJsonText()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator(""))
     {
         validator.Validate("| a b c |a d e f", "{\"_text\":\"a  b c\",\"a\":{\"_text\":\"d e f\"}}");
     }
 }
Ejemplo n.º 32
0
        public void TestJsonToVWString()
        {
            var jsonContext = new JsonContext()
            {
                Label = new SimpleLabel
                {
                    Label = 25
                },
                Ns1 = new Namespace1
                {
                    Foo = 1,
                    Age = "25",
                    DontConsider = "XXX",
                    EscapeCharacterString = "a: a | a\ta",
                    EscapeCharactersText = "b: b | b\tb"
                },
                Ns2 = new Namespace2
                {
                    FeatureA = true
                },
                Clicks = 5
            };

            var jsonContextString = JsonConvert.SerializeObject(jsonContext);
            using (var validator = new VowpalWabbitExampleJsonValidator(new VowpalWabbitSettings
            {
                Arguments = "--json",
                EnableStringExampleGeneration = true,
                EnableStringFloatCompact = true,
                EnableThreadSafeExamplePooling = true
            }))
            {
                validator.Validate("25  |  Clicks:5 MoreClicks:0  |a Bar:1 Age25 EscapeCharacterStringa__a___a_a b_ b _ b b  |b Marker",
                    jsonContextString,
                    VowpalWabbitLabelComparator.Simple);
            }
        }
Ejemplo n.º 33
0
 public void TestJsonSimpleLabelOverride()
 {
     using (var validator = new VowpalWabbitExampleJsonValidator())
     {
         validator.Validate("2 |a foo:1", "{\"_label\":{\"Label\":1},\"a\":{\"foo\":1}}", VowpalWabbitLabelComparator.Simple,
             new SimpleLabel { Label = 2 },
             enableNativeJsonValidation: false /* vw.Parse(json) doesn't support label overwrite */);
     }
 }
Ejemplo n.º 34
0
        public void TestJsonRedirection()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator(new VowpalWabbitSettings("--cb_adf")))
            {
                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "0:-1:.3 | w2 w3"
                    },
                    "{\"_ignoreMe\":5,\"_sub\":{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}, \"_labelIndex\":1, \"_label_Cost\":-1, \"_label_Probability\":0.3}",
                    VowpalWabbitLabelComparator.ContextualBandit,
                    extension: (state, property) =>
                    {
                        if (!property.Equals("_sub"))
                            return false;

                        Assert.AreEqual(state.MultiIndex, -1);

                        state.Parse();

                        return true;
                    });

                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "0:-1:.3 | w2 w3"
                    },
                    "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\", \"_tag\":\"2\"}], \"_labelIndex\":1, \"_label_Cost\":-1, \"_label_Probability\":0.3}",
                    VowpalWabbitLabelComparator.ContextualBandit,
                    extension: (state, property) =>
                    {
                        if (!property.Equals("_tag"))
                            return false;

                        var tag = state.Reader.ReadAsString();

                        Assert.AreEqual(1, state.MultiIndex);
                        Assert.AreEqual("2", tag);

                        return true;
                    });
            }
        }
Ejemplo n.º 35
0
        public void TestJsonNumADFs()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator(""))
            {
                Assert.AreEqual(2,
                    VowpalWabbitJsonSerializer.GetNumberOfActionDependentExamples(
                    "{\"_text\":\"a b c\",\"a\":{\"_text\":\"d e f\"},_multi:[{\"a\":1},{\"b\":2,\"c\":3}]}"));

                Assert.AreEqual(0,
                    VowpalWabbitJsonSerializer.GetNumberOfActionDependentExamples(
                    "{\"_text\":\"a b c\",\"a\":{\"_text\":\"d e f\"},_multi:[]}"));

                Assert.AreEqual(0,
                    VowpalWabbitJsonSerializer.GetNumberOfActionDependentExamples(
                    "{\"_text\":\"a b c\",\"a\":{\"_text\":\"d e f\"}}"));
            }
        }
Ejemplo n.º 36
0
        public void TestJsonMultiline()
        {
            using (var validator = new VowpalWabbitExampleJsonValidator("--cb 2 --cb_type dr"))
            {
                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     " | w2 w3"
                    },
                    "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\"}]}");

                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "2:-1:.3 | w2 w3"
                    },
                    "{\"Age\":25,\"_multi\":[{\"_text\":\"w1 w2\", \"a\":{\"x\":1}}, {\"_text\":\"w2 w3\",\"_label\":\"2:-1:.3\"}]}",
                    VowpalWabbitLabelComparator.ContextualBandit);
            }

            using (var validator = new VowpalWabbitExampleJsonValidator(
                new VowpalWabbitSettings
                {
                    Arguments = "--cb 2 --cb_type dr",
                    PropertyConfiguration = new PropertyConfiguration
                    {
                        MultiProperty = "adf",
                        TextProperty = "someText",
                        LabelProperty = "theLabel",
                        FeatureIgnorePrefix = "xxx"
                    }
                }))
            {
                validator.Validate(new[] {
                     "shared | Age:25",
                     " | w1 w2 |a x:1",
                     "2:-1:.3 | w2 w3"
                    },
                    "{\"Age\":25,\"adf\":[{\"someText\":\"w1 w2\", \"a\":{\"x\":1}, \"xxxxIgnoreMe\":2}, {\"someText\":\"w2 w3\",\"theLabel\":\"2:-1:.3\"}]}",
                    VowpalWabbitLabelComparator.ContextualBandit,
                    enableNativeJsonValidation: false /* remapping of special properties is not supported in native JSON */);
            }
        }
Ejemplo n.º 37
0
        public void TestJsonToVWString()
        {
            var jsonContext = new JsonContext()
            {
                Label = new SimpleLabel
                {
                    Label = 25
                },
                Ns1 = new Namespace1
                {
                    Foo = 1,
                    Age = "25",
                    DontConsider = "XXX"
                },
                Ns2 = new Namespace2
                {
                    FeatureA = true
                },
                Clicks = 5
            };

            var jsonContextString = JsonConvert.SerializeObject(jsonContext);
            using (var validator = new VowpalWabbitExampleJsonValidator(""))
            {
                validator.Validate("25 |a Bar:1 Age25 |b Marker | Clicks:5 MoreClicks:0",
                    jsonContextString,
                    VowpalWabbitLabelComparator.Simple);
            }
        }