• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "common/fml-parser.h"
18 
19 #include "common/feature-descriptors.h"
20 #include "gtest/gtest.h"
21 
22 namespace libtextclassifier {
23 namespace nlp_core {
24 
TEST(FMLParserTest,NoFeature)25 TEST(FMLParserTest, NoFeature) {
26   FMLParser fml_parser;
27   FeatureExtractorDescriptor descriptor;
28   const std::string kFeatureName = "";
29   EXPECT_TRUE(fml_parser.Parse(kFeatureName, &descriptor));
30   EXPECT_EQ(0, descriptor.feature_size());
31 }
32 
TEST(FMLParserTest,FeatureWithNoParams)33 TEST(FMLParserTest, FeatureWithNoParams) {
34   FMLParser fml_parser;
35   FeatureExtractorDescriptor descriptor;
36   const std::string kFeatureName = "continuous-bag-of-relevant-scripts";
37   EXPECT_TRUE(fml_parser.Parse(kFeatureName, &descriptor));
38   EXPECT_EQ(1, descriptor.feature_size());
39   EXPECT_EQ(kFeatureName, descriptor.feature(0).type());
40 }
41 
TEST(FMLParserTest,FeatureWithOneKeywordParameter)42 TEST(FMLParserTest, FeatureWithOneKeywordParameter) {
43   FMLParser fml_parser;
44   FeatureExtractorDescriptor descriptor;
45   EXPECT_TRUE(fml_parser.Parse("myfeature(start=2)", &descriptor));
46   EXPECT_EQ(1, descriptor.feature_size());
47   EXPECT_EQ("myfeature", descriptor.feature(0).type());
48   EXPECT_EQ(1, descriptor.feature(0).parameter_size());
49   EXPECT_EQ("start", descriptor.feature(0).parameter(0).name());
50   EXPECT_EQ("2", descriptor.feature(0).parameter(0).value());
51   EXPECT_FALSE(descriptor.feature(0).has_argument());
52 }
53 
TEST(FMLParserTest,FeatureWithDefaultArgumentNegative)54 TEST(FMLParserTest, FeatureWithDefaultArgumentNegative) {
55   FMLParser fml_parser;
56   FeatureExtractorDescriptor descriptor;
57   EXPECT_TRUE(fml_parser.Parse("offset(-3)", &descriptor));
58   EXPECT_EQ(1, descriptor.feature_size());
59   EXPECT_EQ("offset", descriptor.feature(0).type());
60   EXPECT_EQ(0, descriptor.feature(0).parameter_size());
61   EXPECT_EQ(-3, descriptor.feature(0).argument());
62 }
63 
TEST(FMLParserTest,FeatureWithDefaultArgumentPositive)64 TEST(FMLParserTest, FeatureWithDefaultArgumentPositive) {
65   FMLParser fml_parser;
66   FeatureExtractorDescriptor descriptor;
67   EXPECT_TRUE(fml_parser.Parse("delta(7)", &descriptor));
68   EXPECT_EQ(1, descriptor.feature_size());
69   EXPECT_EQ("delta", descriptor.feature(0).type());
70   EXPECT_EQ(0, descriptor.feature(0).parameter_size());
71   EXPECT_EQ(7, descriptor.feature(0).argument());
72 }
73 
TEST(FMLParserTest,FeatureWithDefaultArgumentZero)74 TEST(FMLParserTest, FeatureWithDefaultArgumentZero) {
75   FMLParser fml_parser;
76   FeatureExtractorDescriptor descriptor;
77   EXPECT_TRUE(fml_parser.Parse("delta(0)", &descriptor));
78   EXPECT_EQ(1, descriptor.feature_size());
79   EXPECT_EQ("delta", descriptor.feature(0).type());
80   EXPECT_EQ(0, descriptor.feature(0).parameter_size());
81   EXPECT_EQ(0, descriptor.feature(0).argument());
82 }
83 
TEST(FMLParserTest,FeatureWithManyKeywordParameters)84 TEST(FMLParserTest, FeatureWithManyKeywordParameters) {
85   FMLParser fml_parser;
86   FeatureExtractorDescriptor descriptor;
87   EXPECT_TRUE(fml_parser.Parse("myfeature(ratio=0.316,start=2,name=\"foo\")",
88                                &descriptor));
89   EXPECT_EQ(1, descriptor.feature_size());
90   EXPECT_EQ("myfeature", descriptor.feature(0).type());
91   EXPECT_EQ(3, descriptor.feature(0).parameter_size());
92   EXPECT_EQ("ratio", descriptor.feature(0).parameter(0).name());
93   EXPECT_EQ("0.316", descriptor.feature(0).parameter(0).value());
94   EXPECT_EQ("start", descriptor.feature(0).parameter(1).name());
95   EXPECT_EQ("2", descriptor.feature(0).parameter(1).value());
96   EXPECT_EQ("name", descriptor.feature(0).parameter(2).name());
97   EXPECT_EQ("foo", descriptor.feature(0).parameter(2).value());
98   EXPECT_FALSE(descriptor.feature(0).has_argument());
99 }
100 
TEST(FMLParserTest,FeatureWithAllKindsOfParameters)101 TEST(FMLParserTest, FeatureWithAllKindsOfParameters) {
102   FMLParser fml_parser;
103   FeatureExtractorDescriptor descriptor;
104   EXPECT_TRUE(
105       fml_parser.Parse("myfeature(17,ratio=0.316,start=2)", &descriptor));
106   EXPECT_EQ(1, descriptor.feature_size());
107   EXPECT_EQ("myfeature", descriptor.feature(0).type());
108   EXPECT_EQ(2, descriptor.feature(0).parameter_size());
109   EXPECT_EQ("ratio", descriptor.feature(0).parameter(0).name());
110   EXPECT_EQ("0.316", descriptor.feature(0).parameter(0).value());
111   EXPECT_EQ("start", descriptor.feature(0).parameter(1).name());
112   EXPECT_EQ("2", descriptor.feature(0).parameter(1).value());
113   EXPECT_EQ(17, descriptor.feature(0).argument());
114 }
115 
TEST(FMLParserTest,FeatureWithWhitespaces)116 TEST(FMLParserTest, FeatureWithWhitespaces) {
117   FMLParser fml_parser;
118   FeatureExtractorDescriptor descriptor;
119   EXPECT_TRUE(fml_parser.Parse(
120       "  myfeature\t\t\t\n(17,\nratio=0.316  ,  start=2)  ", &descriptor));
121   EXPECT_EQ(1, descriptor.feature_size());
122   EXPECT_EQ("myfeature", descriptor.feature(0).type());
123   EXPECT_EQ(2, descriptor.feature(0).parameter_size());
124   EXPECT_EQ("ratio", descriptor.feature(0).parameter(0).name());
125   EXPECT_EQ("0.316", descriptor.feature(0).parameter(0).value());
126   EXPECT_EQ("start", descriptor.feature(0).parameter(1).name());
127   EXPECT_EQ("2", descriptor.feature(0).parameter(1).value());
128   EXPECT_EQ(17, descriptor.feature(0).argument());
129 }
130 
TEST(FMLParserTest,Broken_ParamWithoutValue)131 TEST(FMLParserTest, Broken_ParamWithoutValue) {
132   FMLParser fml_parser;
133   FeatureExtractorDescriptor descriptor;
134   EXPECT_FALSE(
135       fml_parser.Parse("myfeature(17,ratio=0.316,start)", &descriptor));
136 }
137 
TEST(FMLParserTest,Broken_MissingCloseParen)138 TEST(FMLParserTest, Broken_MissingCloseParen) {
139   FMLParser fml_parser;
140   FeatureExtractorDescriptor descriptor;
141   EXPECT_FALSE(fml_parser.Parse("myfeature(17,ratio=0.316", &descriptor));
142 }
143 
TEST(FMLParserTest,Broken_MissingOpenParen)144 TEST(FMLParserTest, Broken_MissingOpenParen) {
145   FMLParser fml_parser;
146   FeatureExtractorDescriptor descriptor;
147   EXPECT_FALSE(fml_parser.Parse("myfeature17,ratio=0.316)", &descriptor));
148 }
149 
TEST(FMLParserTest,Broken_MissingQuote)150 TEST(FMLParserTest, Broken_MissingQuote) {
151   FMLParser fml_parser;
152   FeatureExtractorDescriptor descriptor;
153   EXPECT_FALSE(fml_parser.Parse("count(17,name=\"foo)", &descriptor));
154 }
155 
156 }  // namespace nlp_core
157 }  // namespace libtextclassifier
158