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
|
/*
*/
var ParsedComponents = require('../../result').ParsedComponents;
var Refiner = require('../refiner').Refiner;
var PATTERN = new RegExp("^\\s*(at|after|before|on|,|-|\\(|\\))?\\s*$");
function isMoreSpecific(prevResult, currResult) {
var moreSpecific = false;
if (prevResult.start.isCertain('year')) {
if (!currResult.start.isCertain('year')) {
moreSpecific = true;
} else {
if (prevResult.start.isCertain('month')) {
if (!currResult.start.isCertain('month')) {
moreSpecific = true;
} else {
if (prevResult.start.isCertain('day') && !currResult.start.isCertain('day')) {
moreSpecific = true;
}
}
}
}
}
return moreSpecific;
}
function isAbleToMerge(text, prevResult, currResult) {
var textBetween = text.substring(prevResult.index + prevResult.text.length, currResult.index);
// Only accepts merge if one of them comes from casual relative date
var includesRelativeResult = (prevResult.tags['ENRelativeDateFormatParser'] || currResult.tags['ENRelativeDateFormatParser']);
// We assume they refer to the same date if all date fields are implied
var referToSameDate = !prevResult.start.isCertain('day') && !prevResult.start.isCertain('month') && !prevResult.start.isCertain('year');
// If both years are certain, that determines if they refer to the same date
// but with one more specific than the other
if (prevResult.start.isCertain('year') && currResult.start.isCertain('year'))
referToSameDate = (prevResult.start.get('year') === currResult.start.get('year'));
// We now test with the next level (month) if they refer to the same date
if (prevResult.start.isCertain('month') && currResult.start.isCertain('month'))
referToSameDate = (prevResult.start.get('month') === currResult.start.get('month')) && referToSameDate;
return includesRelativeResult && textBetween.match(PATTERN) && referToSameDate;
}
function mergeResult(text, specificResult, nonSpecificResult){
var specificDate = specificResult.start;
var nonSpecificDate = nonSpecificResult.start;
var startIndex = Math.min(specificResult.index, nonSpecificResult.index);
var endIndex = Math.max(
specificResult.index + specificResult.text.length,
nonSpecificResult.index + nonSpecificResult.text.length);
specificResult.index = startIndex;
specificResult.text = text.substring(startIndex, endIndex);
for (var tag in nonSpecificResult.tags) {
specificResult.tags[tag] = true;
}
specificResult.tags['ENPrioritizeSpecificDateRefiner'] = true;
return specificResult;
}
exports.Refiner = function ENPrioritizeSpecificDateRefiner() {
Refiner.call(this);
this.refine = function(text, results, opt) {
if (results.length < 2) return results;
var mergedResult = [];
var currResult = null;
var prevResult = null;
for (var i = 1; i < results.length; i++) {
currResult = results[i];
prevResult = results[i-1];
if (isMoreSpecific(prevResult, currResult)
&& isAbleToMerge(text, prevResult, currResult)) {
prevResult = mergeResult(text, prevResult, currResult);
currResult = null;
i += 1;
} else if (isMoreSpecific(currResult, prevResult)
&& isAbleToMerge(text, prevResult, currResult)) {
prevResult = mergeResult(text, currResult, prevResult);
currResult = null;
i += 1;
}
mergedResult.push(prevResult);
}
if (currResult != null) {
mergedResult.push(currResult);
}
return mergedResult;
}
}
|