/
ConsumptionDemo.cs
144 lines (132 loc) · 5.12 KB
/
ConsumptionDemo.cs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Axaprj.LexFeatExtr.Grammar;
using Axaprj.LexFeatExtr.Grammar.Attributes;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Xunit;
using Xunit.Abstractions;
namespace Axaprj.LexFeatExtr.Tests
{
public class ConsumptionDemo : TestBase
{
protected override Productions Prod
=> new ProdsConsumption();
protected override Productions.Grammar Grammar
=> new ProdsConsumption.ConsumptionGrammar();
public ConsumptionDemo(ITestOutputHelper output) : base(output) { }
enum SYM
{
[NonTerm]
Text,
[AnyToken(addToResult: false)]
token,
[CollectCount]
finished,
[Empty]
eof,
[NonTerm]
Consumption,
[NonTerm]
Val,
[NonTerm]
Vals,
[NonTerm]
CO2Val,
[SpecCharToken(CHSP.Punctuations)]
separator
}
public enum SymUoM
{
[TextC(
":Word(liters) :Word(per) :Word(100) :Word(kilometers)",
":Word(L,l) :Word(/,per) :Word(100) :Word(km)")]
l100km,
[TextC(
":Word(grams) :Word(per) :Word(kilometer)",
":Word(g) :Word(/) :Word(km)")]
gkm,
[TextC(
":Word(mpg) :Word?(US)")]
mpg,
[TextC("val:Decimal")]
val,
[TextC("vfrom:Decimal :Word(-) vto:Decimal")]
valrange
}
public enum SymTxt
{
[TextC(
":Word(fuel) :Word(consumption)")]
consumption,
[TextC(
":Word(combined)")]
combined,
[TextC(
":Word(urban)")]
urban,
[TextC(":Word(extra) :Word?(-) :Word(urban)", ":Word(extraurban)")]
exurban
}
public class ProdsConsumption : Productions
{
internal class ConsumptionGrammar : Grammar
{
public ConsumptionGrammar() : base(SYM.Text)
{
AddRULE(SYM.Text,
EXPR(SYM.Consumption, SYM.Text),
EXPR(SYM.CO2Val, SYM.Text),
EXPR(SYM.token, SYM.Text),
EXPR(SYM.eof)
);
AddRULE(SYM.Consumption,
EXPR(RANGE(1, 0, SymTxt.combined), SymTxt.consumption, RANGE(0, 6, SYM.token), SYM.Vals),
EXPR(RANGE(1, 0, SymTxt.exurban), SymTxt.consumption, RANGE(0, 6, SYM.token), SYM.Vals),
EXPR(RANGE(1, 0, SymTxt.urban), SymTxt.consumption, RANGE(0, 6, SYM.token), SYM.Vals)
);
AddRULE(SYM.Vals,
EXPR(SYM.Val, RANGE(1, 0, SYM.separator), SYM.Vals),
EXPR(SYM.Val),
EXPR(SYM.finished)
);
AddRULE(SYM.Val,
EXPR(RANGE(1, 0, SymTxt.combined), SymUoM.valrange, SymUoM.l100km),
EXPR(RANGE(1, 0, SymTxt.exurban), SymUoM.valrange, SymUoM.l100km),
EXPR(RANGE(1, 0, SymTxt.urban), SymUoM.valrange, SymUoM.l100km),
EXPR(RANGE(1, 0, SymTxt.combined), SymUoM.val, SymUoM.l100km),
EXPR(RANGE(1, 0, SymTxt.exurban), SymUoM.val, SymUoM.l100km),
EXPR(RANGE(1, 0, SymTxt.urban), SymUoM.val, SymUoM.l100km),
EXPR(SymUoM.valrange, SymUoM.mpg),
EXPR(SymUoM.val, SymUoM.mpg)
);
AddRULE(SYM.CO2Val,
EXPR(SymUoM.val, SymUoM.gkm),
EXPR(SymUoM.valrange, SymUoM.gkm)
);
}
}
}
string Txt1 = "its combined fuel consumption is reduced to up to 2.4 liters per 100 kilometers ( 98 mpg US ) ,"
+ " with CO 2 emissions of up to 56 grams per kilometer .";
string Txt2 = "The lowest combined fuel consumption for the range is 7.9 L / 100 km for LS-U and LS-T 4 x 4 autos " +
"( 209 CO 2 emissions ) , and peaks at 8.1 L / 100 km ( for all LS-T and LS-U 4 x 2 and LS-M 4 x 4 ) .";
string Txt3 = "Its combined fuel consumption is 141.2 - 122.8 mpg , while CO 2 emissions are at least 49 g / km .";
string Txt4 = "NEDC combined fuel consumption is 4.2 - 4.1 l / 100 km " +
"( preliminary fuel consumption NEDC 1 : urban 4.9 - 4.8 l / 100 km ; extra - urban 3.8 - 3.6 l / 100 km ;" +
" combined 4.2 - 4.1 l / 100 km , 95 - 93 g / km CO 2 ;";
void ProduceConsumption(string txt)
{
var prod = Produce(txt);
Assert.NotNull(prod);
Log($"Prod: '{prod}'");
}
[Fact]
public void ConsumptionTest()
{
ProduceConsumption(Txt1);
ProduceConsumption(Txt2);
ProduceConsumption(Txt3);
ProduceConsumption(Txt4);
}
}
}