summaryrefslogtreecommitdiffstats
path: root/third_party/rust/suggest/src/yelp.rs
blob: 0eb6c10d56de17c6143600945e2096af072f0b19 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

use rusqlite::types::ToSqlOutput;
use rusqlite::{named_params, Result as RusqliteResult, ToSql};
use sql_support::ConnExt;
use url::form_urlencoded;

use crate::{
    db::SuggestDao,
    provider::SuggestionProvider,
    rs::{DownloadedYelpSuggestion, SuggestRecordId},
    suggestion::Suggestion,
    Result, SuggestionQuery,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[repr(u8)]
enum Modifier {
    Pre = 0,
    Post = 1,
    Yelp = 2,
}

impl ToSql for Modifier {
    fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
        Ok(ToSqlOutput::from(*self as u8))
    }
}

/// This module assumes like following query.
/// "Yelp-modifier? Pre-modifier? Subject Post-modifier? (Location-modifier | Location-sign Location?)? Yelp-modifier?"
/// For example, the query below is valid.
/// "Yelp (Yelp-modifier) Best(Pre-modifier) Ramen(Subject) Delivery(Post-modifier) In(Location-sign) Tokyo(Location)"
/// Also, as everything except Subject is optional, "Ramen" will be also valid query.
/// However, "Best Best Ramen" and "Ramen Best" is out of the above appearance order rule,
/// parsing will be failed. Also, every words except Location needs to be registered in DB.
/// Please refer to the query test in store.rs for all of combination.
/// Currently, the maximum query length is determined while refering to having word lengths in DB
/// and location names.
/// max subject: 50 + pre-modifier: 10 + post-modifier: 10 + location-sign: 7 + location: 50 = 127 = 150.
const MAX_QUERY_LENGTH: usize = 150;

/// The max number of words consisting the modifier. To improve the SQL performance by matching with
/// "keyword=:modifier" (please see is_modifier()), define this how many words we should check.
const MAX_MODIFIER_WORDS_NUMBER: usize = 2;

/// At least this many characters must be typed for a subject to be matched.
const SUBJECT_PREFIX_MATCH_THRESHOLD: usize = 2;

impl<'a> SuggestDao<'a> {
    /// Inserts the suggestions for Yelp attachment into the database.
    pub(crate) fn insert_yelp_suggestions(
        &mut self,
        record_id: &SuggestRecordId,
        suggestion: &DownloadedYelpSuggestion,
    ) -> Result<()> {
        for keyword in &suggestion.subjects {
            self.scope.err_if_interrupted()?;
            self.conn.execute_cached(
                "INSERT INTO yelp_subjects(record_id, keyword) VALUES(:record_id, :keyword)",
                named_params! {
                    ":record_id": record_id.as_str(),
                    ":keyword": keyword,
                },
            )?;
        }

        for keyword in &suggestion.pre_modifiers {
            self.scope.err_if_interrupted()?;
            self.conn.execute_cached(
                "INSERT INTO yelp_modifiers(record_id, type, keyword) VALUES(:record_id, :type, :keyword)",
                named_params! {
                    ":record_id": record_id.as_str(),
                    ":type": Modifier::Pre,
                    ":keyword": keyword,
                },
            )?;
        }

        for keyword in &suggestion.post_modifiers {
            self.scope.err_if_interrupted()?;
            self.conn.execute_cached(
                "INSERT INTO yelp_modifiers(record_id, type, keyword) VALUES(:record_id, :type, :keyword)",
                named_params! {
                    ":record_id": record_id.as_str(),
                    ":type": Modifier::Post,
                    ":keyword": keyword,
                },
            )?;
        }

        for keyword in &suggestion.yelp_modifiers {
            self.scope.err_if_interrupted()?;
            self.conn.execute_cached(
                "INSERT INTO yelp_modifiers(record_id, type, keyword) VALUES(:record_id, :type, :keyword)",
                named_params! {
                    ":record_id": record_id.as_str(),
                    ":type": Modifier::Yelp,
                    ":keyword": keyword,
                },
            )?;
        }

        for sign in &suggestion.location_signs {
            self.scope.err_if_interrupted()?;
            self.conn.execute_cached(
                "INSERT INTO yelp_location_signs(record_id, keyword, need_location) VALUES(:record_id, :keyword, :need_location)",
                named_params! {
                    ":record_id": record_id.as_str(),
                    ":keyword": sign.keyword,
                    ":need_location": sign.need_location,
                },
            )?;
        }

        self.scope.err_if_interrupted()?;
        self.conn.execute_cached(
            "INSERT INTO yelp_custom_details(record_id, icon_id, score) VALUES(:record_id, :icon_id, :score)",
            named_params! {
                ":record_id": record_id.as_str(),
                ":icon_id": suggestion.icon_id,
                ":score": suggestion.score,
            },
        )?;

        Ok(())
    }

    /// Fetch Yelp suggestion from given user's query.
    pub(crate) fn fetch_yelp_suggestions(
        &self,
        query: &SuggestionQuery,
    ) -> Result<Vec<Suggestion>> {
        if !query.providers.contains(&SuggestionProvider::Yelp) {
            return Ok(vec![]);
        }

        if query.keyword.len() > MAX_QUERY_LENGTH {
            return Ok(vec![]);
        }

        let query_string = &query.keyword.trim();
        if !query_string.contains(' ') {
            let Some((subject, subject_exact_match)) = self.find_subject(query_string)? else {
                return Ok(vec![]);
            };
            let (icon, icon_mimetype, score) = self.fetch_custom_details()?;
            let builder = SuggestionBuilder {
                subject: &subject,
                subject_exact_match,
                pre_modifier: None,
                post_modifier: None,
                location_sign: None,
                location: None,
                need_location: false,
                icon,
                icon_mimetype,
                score,
            };
            return Ok(vec![builder.into()]);
        }

        // Find the yelp keyword modifier and remove them from the query.
        let (query_without_yelp_modifiers, _, _) =
            self.find_modifiers(query_string, Modifier::Yelp, Modifier::Yelp)?;

        // Find the location sign and the location.
        let (query_without_location, location_sign, location, need_location) =
            self.find_location(&query_without_yelp_modifiers)?;

        if let (Some(_), false) = (&location, need_location) {
            // The location sign does not need the specific location, but user is setting something.
            return Ok(vec![]);
        }

        if query_without_location.is_empty() {
            // No remained query.
            return Ok(vec![]);
        }

        // Find the modifiers.
        let (subject_candidate, pre_modifier, post_modifier) =
            self.find_modifiers(&query_without_location, Modifier::Pre, Modifier::Post)?;

        let Some((subject, subject_exact_match)) = self.find_subject(&subject_candidate)? else {
            return Ok(vec![]);
        };

        let (icon, icon_mimetype, score) = self.fetch_custom_details()?;
        let builder = SuggestionBuilder {
            subject: &subject,
            subject_exact_match,
            pre_modifier,
            post_modifier,
            location_sign,
            location,
            need_location,
            icon,
            icon_mimetype,
            score,
        };
        Ok(vec![builder.into()])
    }

    /// Fetch the custom details for Yelp suggestions.
    /// It returns the location tuple as follows:
    /// (
    ///   Option<Vec<u8>>: Icon data. If not found, returns None.
    ///   Option<String>: Mimetype of the icon data. If not found, returns None.
    ///   f64: Reflects score field in the yelp_custom_details table.
    /// )
    ///
    /// Note that there should be only one record in `yelp_custom_details`
    /// as all the Yelp assets are stored in the attachment of a single record
    /// on Remote Settings. The following query will perform a table scan against
    /// `yelp_custom_details` followed by an index search against `icons`,
    /// which should be fine since there is only one record in the first table.
    fn fetch_custom_details(&self) -> Result<(Option<Vec<u8>>, Option<String>, f64)> {
        let result = self.conn.query_row_and_then_cachable(
            r#"
            SELECT
              i.data, i.mimetype, y.score
            FROM
              yelp_custom_details y
            LEFT JOIN
              icons i
              ON y.icon_id = i.id
            LIMIT
              1
            "#,
            (),
            |row| -> Result<_> {
                Ok((
                    row.get::<_, Option<Vec<u8>>>(0)?,
                    row.get::<_, Option<String>>(1)?,
                    row.get::<_, f64>(2)?,
                ))
            },
            true,
        )?;

        Ok(result)
    }

    /// Find the location information from the given query string.
    /// It returns the location tuple as follows:
    /// (
    ///   String: Query string that is removed found location information.
    ///   Option<String>: Location sign found in yelp_location_signs table. If not found, returns None.
    ///   Option<String>: Specific location name after location sign. If not found, returns None.
    ///   bool: Reflects need_location field in the table.
    /// )
    fn find_location(&self, query: &str) -> Result<(String, Option<String>, Option<String>, bool)> {
        let query_with_spaces = format!(" {} ", query);
        let mut results: Vec<(usize, usize, i8)> = self.conn.query_rows_and_then_cached(
            "
        SELECT
          INSTR(:query, ' ' || keyword || ' ') AS sign_index,
          LENGTH(keyword) AS sign_length,
          need_location
        FROM yelp_location_signs
        WHERE
          sign_index > 0
        ORDER BY
          sign_length DESC
        LIMIT 1
        ",
            named_params! {
                ":query": &query_with_spaces.to_lowercase(),
            },
            |row| -> Result<_> {
                Ok((
                    row.get::<_, usize>("sign_index")?,
                    row.get::<_, usize>("sign_length")?,
                    row.get::<_, i8>("need_location")?,
                ))
            },
        )?;

        let (sign_index, sign_length, need_location) = if let Some(res) = results.pop() {
            res
        } else {
            return Ok((query.trim().to_string(), None, None, false));
        };

        let pre_location = query_with_spaces
            .get(..sign_index)
            .map(str::trim)
            .map(str::to_string)
            .unwrap_or_default();
        let location_sign = query_with_spaces
            .get(sign_index..sign_index + sign_length)
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_string);
        let location = query_with_spaces
            .get(sign_index + sign_length..)
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_string);

        Ok((pre_location, location_sign, location, need_location == 1))
    }

    /// Find the pre/post modifier from the given query string.
    /// It returns the modifiers tuple as follows:
    /// (
    ///   String: Query string that is removed found the modifiers.
    ///   Option<String>: Pre-modifier found in the yelp_modifiers table. If not found, returns None.
    ///   Option<String>: Post-modifier found in the yelp_modifiers table. If not found, returns None.
    /// )
    fn find_modifiers(
        &self,
        query: &str,
        pre_modifier_type: Modifier,
        post_modifier_type: Modifier,
    ) -> Result<(String, Option<String>, Option<String>)> {
        if !query.contains(' ') {
            return Ok((query.to_string(), None, None));
        }

        let words: Vec<_> = query.split_whitespace().collect();

        let mut pre_modifier = None;
        for n in (1..=MAX_MODIFIER_WORDS_NUMBER).rev() {
            let mut candidate_chunks = words.chunks(n);
            let candidate = candidate_chunks.next().unwrap_or(&[""]).join(" ");
            if self.is_modifier(&candidate, pre_modifier_type)? {
                pre_modifier = Some(candidate);
                break;
            }
        }

        let mut post_modifier = None;
        for n in (1..=MAX_MODIFIER_WORDS_NUMBER).rev() {
            let mut candidate_chunks = words.rchunks(n);
            let candidate = candidate_chunks.next().unwrap_or(&[""]).join(" ");
            if self.is_modifier(&candidate, post_modifier_type)? {
                post_modifier = Some(candidate);
                break;
            }
        }

        let mut without_modifiers = query;
        if let Some(ref modifier) = pre_modifier {
            without_modifiers = &without_modifiers[modifier.len()..];
        }
        if let Some(ref modifier) = post_modifier {
            without_modifiers = &without_modifiers[..without_modifiers.len() - modifier.len()];
        }

        Ok((
            without_modifiers.trim().to_string(),
            pre_modifier,
            post_modifier,
        ))
    }

    /// Find the subject from the given string.
    /// It returns the Option. If it is not none, it contains the tuple as follows:
    /// (
    ///   String: Subject.
    ///   bool: Whether the subject matched exactly with the paramter.
    /// )
    fn find_subject(&self, candidate: &str) -> Result<Option<(String, bool)>> {
        if candidate.is_empty() {
            return Ok(None);
        }

        // If the length of subject candidate is less than
        // SUBJECT_PREFIX_MATCH_THRESHOLD, should exact match.
        if candidate.len() < SUBJECT_PREFIX_MATCH_THRESHOLD {
            return Ok(if self.is_subject(candidate)? {
                Some((candidate.to_string(), true))
            } else {
                None
            });
        }

        // Otherwise, apply prefix-match.
        Ok(
            match self.conn.query_row_and_then_cachable(
                "SELECT keyword
                 FROM yelp_subjects
                 WHERE keyword BETWEEN :candidate AND :candidate || x'FFFF'
                 ORDER BY LENGTH(keyword) ASC, keyword ASC
                 LIMIT 1",
                named_params! {
                    ":candidate": candidate.to_lowercase(),
                },
                |row| row.get::<_, String>(0),
                true,
            ) {
                Ok(keyword) => {
                    debug_assert!(candidate.len() <= keyword.len());
                    Some((
                        format!("{}{}", candidate, &keyword[candidate.len()..]),
                        candidate.len() == keyword.len(),
                    ))
                }
                Err(_) => None,
            },
        )
    }

    fn is_modifier(&self, word: &str, modifier_type: Modifier) -> Result<bool> {
        let result = self.conn.query_row_and_then_cachable(
            "
        SELECT EXISTS (
            SELECT 1 FROM yelp_modifiers WHERE type = :type AND keyword = :word LIMIT 1
        )
        ",
            named_params! {
                ":type": modifier_type,
                ":word": word.to_lowercase(),
            },
            |row| row.get::<_, bool>(0),
            true,
        )?;

        Ok(result)
    }

    fn is_subject(&self, word: &str) -> Result<bool> {
        let result = self.conn.query_row_and_then_cachable(
            "
        SELECT EXISTS (
            SELECT 1 FROM yelp_subjects WHERE keyword = :word LIMIT 1
        )
        ",
            named_params! {
                ":word": word.to_lowercase(),
            },
            |row| row.get::<_, bool>(0),
            true,
        )?;

        Ok(result)
    }
}

struct SuggestionBuilder<'a> {
    subject: &'a str,
    subject_exact_match: bool,
    pre_modifier: Option<String>,
    post_modifier: Option<String>,
    location_sign: Option<String>,
    location: Option<String>,
    need_location: bool,
    icon: Option<Vec<u8>>,
    icon_mimetype: Option<String>,
    score: f64,
}

impl<'a> From<SuggestionBuilder<'a>> for Suggestion {
    fn from(builder: SuggestionBuilder<'a>) -> Suggestion {
        // This location sign such the 'near by' needs to add as a description parameter.
        let location_modifier = if !builder.need_location {
            builder.location_sign.as_deref()
        } else {
            None
        };
        let description = [
            builder.pre_modifier.as_deref(),
            Some(builder.subject),
            builder.post_modifier.as_deref(),
            location_modifier,
        ]
        .iter()
        .flatten()
        .copied()
        .collect::<Vec<_>>()
        .join(" ");

        // https://www.yelp.com/search?find_desc={description}&find_loc={location}
        let mut url = String::from("https://www.yelp.com/search?");
        let mut parameters = form_urlencoded::Serializer::new(String::new());
        parameters.append_pair("find_desc", &description);
        if let (Some(location), true) = (&builder.location, builder.need_location) {
            parameters.append_pair("find_loc", location);
        }
        url.push_str(&parameters.finish());

        let title = [
            builder.pre_modifier.as_deref(),
            Some(builder.subject),
            builder.post_modifier.as_deref(),
            builder.location_sign.as_deref(),
            builder.location.as_deref(),
        ]
        .iter()
        .flatten()
        .copied()
        .collect::<Vec<_>>()
        .join(" ");

        Suggestion::Yelp {
            url,
            title,
            icon: builder.icon,
            icon_mimetype: builder.icon_mimetype,
            score: builder.score,
            has_location_sign: location_modifier.is_none() && builder.location_sign.is_some(),
            subject_exact_match: builder.subject_exact_match,
            location_param: "find_loc".to_string(),
        }
    }
}