Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions sanitizer/_text_pelias_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ function _sanitize (raw, clean) {
return messages;
}

function isAmbiguousStreetOrVenueParse(solutions){
if (!solutions || solutions.length < 2) {
return false;
}

const topTwoSolutions = _.take(solutions, 2);
const streetOnlySolution = topTwoSolutions.find(solution => {
return solution.pair &&
solution.pair[0].classification.label === 'street';
});
const venueOnlySolution = topTwoSolutions.find(solution => {
return solution.pair &&
solution.pair[0].classification.label === 'venue';
});

const restOfSolutionIsTheSame =
topTwoSolutions[0].pair.length === topTwoSolutions[1].pair.length &&
_.every(_.zip(topTwoSolutions[0].pair.slice(1), topTwoSolutions[1].pair.slice(1)), ([p1, p2]) =>
p1.span.body === p2.span.body && p1.classification.label === p2.classification.label
);

return (
streetOnlySolution &&
venueOnlySolution &&
streetOnlySolution.pair[0].span.body === venueOnlySolution.pair[0].span.body &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can put this before const restOfSolutionIsTheSame =.... ? something like

if (!streetOnlySolution || !venueOnlySolution || streetOnlySolution.pair[0].span.body !== venueOnlySolution.pair[0].span.body) {
  return false
}

restOfSolutionIsTheSame
);
}


function parse (clean) {

// parse text
Expand Down Expand Up @@ -95,6 +125,23 @@ function parse (clean) {
// ' VVVV NN SSSSSSS AAAAAA PPPPP '
let mask = solution.mask(t);

// (Theory) If the parser's two top solutions were labeling the entire query as a venue/place,
// and labeling the entire query as a street, then the parse is telling us it's pretty
// ambiguous, so we shouldn't bother boosting street or place matches in our search.
//
// This helps with queries like "wrigley field" that have a word (field) that could be
// a street suffix but is also a likely venue word.
//
// (Reality) There's no way to tell if the parser labeled the "whole query" because
// punctuation in the incoming query is unlabeled, so in reality this checks if
// the top two solutions were venue-only and place-only and labeled identical parts of the query
if (isAmbiguousStreetOrVenueParse(t.solution)) {
delete parsed_text.street;
delete parsed_text.venue;
mask = mask.replace(/S/g, ' ');
mask = mask.replace(/V/g, ' ');
}

// the entire input text as seen by the parser with any postcode classification(s) removed
let body = t.span.body.split('')
.map((c, i) => (mask[i] !== 'P') ? c : ' ')
Expand Down
8 changes: 8 additions & 0 deletions test/unit/sanitizer/_text_pelias_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ module.exports.tests.text_parser = function (test, common) {
cases.push(['Air & Space Museum D', { subject: 'Air & Space Museum' }, true]);
cases.push(['Air & Space Museum DC', { subject: 'Air & Space Museum' }, true]);

// ambiguous venue
cases.push(['mccarren park', {
subject: 'mccarren park',
}]);
cases.push(['wrigley field', {
subject: 'wrigley field',
}]);

// admin areas
cases.push(['N', { subject: 'N' }, true]);
cases.push(['Ne', { subject: 'Ne' }, true]);
Expand Down