11
11
* - detection (or specification) of query type. i.e. an address shouldn't match an admin address.
12
12
*/
13
13
14
+ const _ = require ( 'lodash' ) ;
14
15
const stats = require ( 'stats-lite' ) ;
15
16
const logger = require ( 'pelias-logger' ) . get ( 'api' ) ;
16
- const check = require ( 'check-types' ) ;
17
17
const field = require ( '../helper/fieldValue' ) ;
18
18
19
19
var RELATIVE_SCORES = true ;
20
20
21
21
function setup ( peliasConfig ) {
22
- if ( check . assigned ( peliasConfig ) ) {
22
+ if ( ! _ . isNil ( peliasConfig ) ) {
23
23
RELATIVE_SCORES = peliasConfig . hasOwnProperty ( 'relativeScores' ) ? peliasConfig . relativeScores : true ;
24
24
}
25
25
return computeScores ;
26
26
}
27
27
28
28
function computeScores ( req , res , next ) {
29
29
// do nothing if no result data set or if query is not of the pelias_parser variety
30
- if ( check . undefined ( req . clean ) || check . undefined ( res ) ||
31
- check . undefined ( res . data ) || check . undefined ( res . meta ) ||
30
+ if ( _ . isUndefined ( req . clean ) || _ . isUndefined ( res ) ||
31
+ _ . isUndefined ( res . data ) || _ . isUndefined ( res . meta ) ||
32
32
res . meta . query_type !== 'search_pelias_parser' ) {
33
33
return next ( ) ;
34
34
}
@@ -91,17 +91,17 @@ function computeConfidenceScore(req, mean, stdev, hit) {
91
91
* @returns {bool }
92
92
*/
93
93
function checkForDealBreakers ( req , hit ) {
94
- if ( check . undefined ( req . clean . parsed_text ) ) {
94
+ if ( _ . isUndefined ( req . clean . parsed_text ) ) {
95
95
return false ;
96
96
}
97
97
98
- if ( check . assigned ( req . clean . parsed_text . state ) && check . assigned ( hit . parent ) &&
98
+ if ( ! _ . isNil ( req . clean . parsed_text . state ) && ! _ . isNil ( hit . parent ) &&
99
99
hit . parent . region_a && req . clean . parsed_text . state !== hit . parent . region_a [ 0 ] ) {
100
100
logger . debug ( '[confidence][deal-breaker]: state !== region_a' ) ;
101
101
return true ;
102
102
}
103
103
104
- if ( check . assigned ( req . clean . parsed_text . postalcode ) && check . assigned ( hit . address_parts ) &&
104
+ if ( ! _ . isNil ( req . clean . parsed_text . postalcode ) && ! _ . isNil ( hit . address_parts ) &&
105
105
req . clean . parsed_text . postalcode !== hit . address_parts . zip ) {
106
106
return true ;
107
107
}
@@ -131,7 +131,7 @@ function checkDistanceFromMean(score, mean, stdev) {
131
131
*/
132
132
function checkName ( text , parsed_text , hit ) {
133
133
// parsed_text name should take precedence if available since it's the cleaner name property
134
- if ( check . assigned ( parsed_text ) && check . assigned ( parsed_text . name ) &&
134
+ if ( ! _ . isNil ( parsed_text ) && ! _ . isNil ( parsed_text . name ) &&
135
135
field . getStringValue ( hit . name . default ) . toLowerCase ( ) === parsed_text . name . toLowerCase ( ) ) {
136
136
return 1 ;
137
137
}
@@ -154,9 +154,9 @@ function checkName(text, parsed_text, hit) {
154
154
* @returns {number }
155
155
*/
156
156
function checkQueryType ( text , hit ) {
157
- if ( check . assigned ( text ) && check . assigned ( text . number ) &&
158
- ( check . undefined ( hit . address_parts ) ||
159
- ( check . assigned ( hit . address_parts ) && check . undefined ( hit . address_parts . number ) ) ) ) {
157
+ if ( ! _ . isNil ( text ) && ! _ . isNil ( text . number ) &&
158
+ ( _ . isUndefined ( hit . address_parts ) ||
159
+ ( ! _ . isNil ( hit . address_parts ) && _ . isUndefined ( hit . address_parts . number ) ) ) ) {
160
160
return 0 ;
161
161
}
162
162
return 1 ;
@@ -173,22 +173,22 @@ function checkQueryType(text, hit) {
173
173
function propMatch ( textProp , hitProp , expectEnriched ) {
174
174
175
175
// both missing, but expect to have enriched value in result => BAD
176
- if ( check . undefined ( textProp ) && check . undefined ( hitProp ) && check . assigned ( expectEnriched ) ) { return 0 ; }
176
+ if ( _ . isUndefined ( textProp ) && _ . isUndefined ( hitProp ) && ! _ . isNil ( expectEnriched ) ) { return 0 ; }
177
177
178
178
// both missing, and no enrichment expected => GOOD
179
- if ( check . undefined ( textProp ) && check . undefined ( hitProp ) ) { return 1 ; }
179
+ if ( _ . isUndefined ( textProp ) && _ . isUndefined ( hitProp ) ) { return 1 ; }
180
180
181
181
// text has it, result doesn't => BAD
182
- if ( check . assigned ( textProp ) && check . undefined ( hitProp ) ) { return 0 ; }
182
+ if ( ! _ . isNil ( textProp ) && _ . isUndefined ( hitProp ) ) { return 0 ; }
183
183
184
184
// text missing, result has it, and enrichment is expected => GOOD
185
- if ( check . undefined ( textProp ) && check . assigned ( hitProp ) && check . assigned ( expectEnriched ) ) { return 1 ; }
185
+ if ( _ . isUndefined ( textProp ) && ! _ . isNil ( hitProp ) && ! _ . isNil ( expectEnriched ) ) { return 1 ; }
186
186
187
187
// text missing, result has it, enrichment not desired => 50/50
188
- if ( check . undefined ( textProp ) && check . assigned ( hitProp ) ) { return 0.5 ; }
188
+ if ( _ . isUndefined ( textProp ) && ! _ . isNil ( hitProp ) ) { return 0.5 ; }
189
189
190
190
// both present, values match => GREAT
191
- if ( check . assigned ( textProp ) && check . assigned ( hitProp ) &&
191
+ if ( ! _ . isNil ( textProp ) && ! _ . isNil ( hitProp ) &&
192
192
textProp . toString ( ) . toLowerCase ( ) === hitProp . toString ( ) . toLowerCase ( ) ) { return 1 ; }
193
193
194
194
// ¯\_(ツ)_/¯
@@ -218,7 +218,7 @@ function checkAddress(text, hit) {
218
218
var checkCount = 5 ;
219
219
var res = 0 ;
220
220
221
- if ( check . assigned ( text ) && check . assigned ( text . number ) && check . assigned ( text . street ) ) {
221
+ if ( ! _ . isNil ( text ) && ! _ . isNil ( text . number ) && ! _ . isNil ( text . street ) ) {
222
222
res += propMatch ( text . number , ( hit . address_parts ? hit . address_parts . number : null ) , false ) ;
223
223
res += propMatch ( text . street , ( hit . address_parts ? hit . address_parts . street : null ) , false ) ;
224
224
res += propMatch ( text . postalcode , ( hit . address_parts ? hit . address_parts . zip : null ) , true ) ;
0 commit comments