-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathAbstractNumberModel.cs
149 lines (119 loc) · 5.01 KB
/
AbstractNumberModel.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
145
146
147
148
149
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Recognizers.Text.Utilities;
namespace Microsoft.Recognizers.Text.Number
{
public abstract class AbstractNumberModel : IModel
{
// Languages supporting subtypes in the resolution to be added here
private static readonly List<string> ExtractorsSupportingSubtype = new List<string>
{
Constants.ARABIC,
Constants.ENGLISH,
Constants.PORTUGUESE,
Constants.SPANISH,
Constants.SWEDISH,
Constants.KOREAN,
Constants.FRENCH,
// TODO: Temporarily disabled as existing TestSpec not supporting
// Constants.JAPANESE_SUBS,
// Constants.KOREAN,
};
private string culture;
private string requestedCulture;
protected AbstractNumberModel(IParser parser, IExtractor extractor)
{
this.Parser = parser;
this.Extractor = extractor;
}
public abstract string ModelTypeName { get; }
public string Culture => this.culture;
public string RequestedCulture => this.requestedCulture;
protected IExtractor Extractor { get; private set; }
protected IParser Parser { get; private set; }
public List<ModelResult> Parse(string query)
{
var parsedNumbers = new List<ParseResult>();
// Preprocess the query
query = QueryProcessor.Preprocess(query, caseSensitive: true);
try
{
var extractResults = Extractor.Extract(query);
foreach (var result in extractResults)
{
var parseResult = Parser.Parse(result);
if (parseResult.Data is List<ParseResult> parseResults)
{
parsedNumbers.AddRange(parseResults);
}
else
{
parsedNumbers.Add(parseResult);
}
}
var modelResults = parsedNumbers.Select(BuildModelResult).Where(r => r != null).ToList();
return modelResults;
}
catch (Exception)
{
// Nothing to do. Exceptions in parse should not break users of recognizers.
// No result.
}
return new List<ModelResult>();
}
public void SetCultureInfo(string culture, string requestedCulture = null)
{
this.culture = culture;
this.requestedCulture = requestedCulture;
}
private ModelResult BuildModelResult(ParseResult pn)
{
try
{
var end = pn.Start.Value + pn.Length.Value - 1;
var resolution = new SortedDictionary<string, object>();
if (pn.Value != null)
{
resolution.Add(ResolutionKey.Value, pn.ResolutionStr);
}
var extractorSupportsSubtype = ExtractorsSupportingSubtype.Exists(e => Extractor.GetType().ToString().Contains(e));
// Check if current extractor supports the Subtype field in the resolution
// As some languages like German, we miss handling some subtypes between "decimal" and "integer"
if (!string.IsNullOrEmpty(pn.Type) &&
Constants.ValidSubTypes.Contains(pn.Type) && extractorSupportsSubtype)
{
resolution.Add(ResolutionKey.SubType, pn.Type);
}
string specificNumberType;
// For ordinal and ordinal.relative - "ordinal.relative" only available in English for now
if (ModelTypeName.Equals(Constants.MODEL_ORDINAL, StringComparison.Ordinal))
{
specificNumberType = pn.Type;
resolution.Add(ResolutionKey.Offset, pn.Metadata.Offset);
resolution.Add(ResolutionKey.RelativeTo, pn.Metadata.RelativeTo);
}
else
{
specificNumberType = ModelTypeName;
}
return new ModelResult
{
Start = pn.Start.Value,
End = end,
Resolution = resolution,
Text = pn.Text,
TypeName = specificNumberType,
};
}
catch (Exception)
{
// Nothing to do. Exceptions in result process should not affect other extracted entities.
// No result.
}
return null; // Only in failure cases. These will be filtered out before final output.
}
}
}