<commit_before>
# A completion engine provides search suggestions for a search engine.  A search engine is identified by a
# "searchUrl", e.g. Settings.get("searchUrl"), or a custom search engine URL.
#
# Each completion engine defines three functions:
#
#   1. "match" - This takes a searchUrl and returns a boolean indicating whether this completion engine can
#      perform completion for the given search engine.
#
#   2. "getUrl" - This takes a list of query terms (queryTerms) and generates a completion URL, that is, a URL
#      which will provide completions for this completion engine.
#
#   3. "parse" - This takes a successful XMLHttpRequest object (the request has completed successfully), and
#      returns a list of suggestions (a list of strings).  This method is always executed within the context
#      of a try/catch block, so errors do not propagate.
#
# Each new completion engine must be added to the list "CompletionEngines" at the bottom of this file.
#
# The lookup logic which uses these completion engines is in "./completion_search.coffee".
#

# A base class for common regexp-based matching engines.
class RegexpEngine
  constructor: (args...) -> @regexps = args.map (regexp) -> new RegExp regexp
  match: (searchUrl) -> Utils.matchesAnyRegexp @regexps, searchUrl

# Several Google completion engines package XML responses in this way.
class GoogleXMLRegexpEngine extends RegexpEngine
  parse: (xhr) ->
    for suggestion in xhr.responseXML.getElementsByTagName "suggestion"
      continue unless suggestion = suggestion.getAttribute "data"
      suggestion

class Google extends GoogleXMLRegexpEngine
  # Example search URL: http://www.google.com/search?q=%s
  constructor: (regexps = null) ->
    super regexps ? "^https?://[a-z]+\\.google\\.(com|ie|co\\.uk|ca|com\\.au)/"

  getUrl: (queryTerms) ->
    Utils.createSearchUrl queryTerms,
      "http://suggestqueries.google.com/complete/search?ss_protocol=legace&client=toolbar&q=%s"

# A wrapper class for Google completions.  This adds prefix terms to the query, and strips those terms from
# the resulting suggestions.  For example, for Google Maps, we add "map of" as a prefix, then strip "map of"
# from the resulting suggestions.
class GoogleWithPrefix
  constructor: (prefix, args...) ->
    @engine = new Google args...
    @prefix = "#{prefix.trim()} "
    @queryTerms = @prefix.split /\s+/
  match: (args...) -> @engine.match args...
  getUrl: (queryTerms) -> @engine.getUrl [ @queryTerms..., queryTerms... ]
  parse: (xhr) ->
    @engine.parse(xhr)
      .filter (suggestion) => suggestion.startsWith @prefix
      .map (suggestion) => suggestion[@prefix.length..].ltrim()

# For Google Maps, we add the prefix "map of" to the query, and send it to Google's general search engine,
# then strip "map of" from the resulting suggestions.
class GoogleMaps extends GoogleWithPrefix
  # Example search URL: https://www.google.com/maps?q=%s
  constructor: -> super "map of", "https?://[a-z]+\\.google\\.(com|ie|co\\.uk|ca|com\\.au)/maps"

class Youtube extends GoogleXMLRegexpEngine
  # Example search URL: http://www.youtube.com/results?search_query=%s
  constructor: ->
    super "^https?://[a-z]+\\.youtube\\.com/results"<commit_msg>Tweak Google Maps URL.<commit_after>
# A completion engine provides search suggestions for a search engine.  A search engine is identified by a
# "searchUrl", e.g. Settings.get("searchUrl"), or a custom search engine URL.
#
# Each completion engine defines three functions:
#
#   1. "match" - This takes a searchUrl and returns a boolean indicating whether this completion engine can
#      perform completion for the given search engine.
#
#   2. "getUrl" - This takes a list of query terms (queryTerms) and generates a completion URL, that is, a URL
#      which will provide completions for this completion engine.
#
#   3. "parse" - This takes a successful XMLHttpRequest object (the request has completed successfully), and
#      returns a list of suggestions (a list of strings).  This method is always executed within the context
#      of a try/catch block, so errors do not propagate.
#
# Each new completion engine must be added to the list "CompletionEngines" at the bottom of this file.
#
# The lookup logic which uses these completion engines is in "./completion_search.coffee".
#

# A base class for common regexp-based matching engines.
class RegexpEngine
  constructor: (args...) -> @regexps = args.map (regexp) -> new RegExp regexp
  match: (searchUrl) -> Utils.matchesAnyRegexp @regexps, searchUrl

# Several Google completion engines package XML responses in this way.
class GoogleXMLRegexpEngine extends RegexpEngine
  parse: (xhr) ->
    for suggestion in xhr.responseXML.getElementsByTagName "suggestion"
      continue unless suggestion = suggestion.getAttribute "data"
      suggestion

class Google extends GoogleXMLRegexpEngine
  # Example search URL: http://www.google.com/search?q=%s
  constructor: (regexps = null) ->
    super regexps ? "^https?://[a-z]+\\.google\\.(com|ie|co\\.uk|ca|com\\.au)/"

  getUrl: (queryTerms) ->
    Utils.createSearchUrl queryTerms,
      "http://suggestqueries.google.com/complete/search?ss_protocol=legace&client=toolbar&q=%s"

# A wrapper class for Google completions.  This adds prefix terms to the query, and strips those terms from
# the resulting suggestions.  For example, for Google Maps, we add "map of" as a prefix, then strip "map of"
# from the resulting suggestions.
class GoogleWithPrefix
  constructor: (prefix, args...) ->
    @engine = new Google args...
    @prefix = "#{prefix.trim()} "
    @queryTerms = @prefix.split /\s+/
  match: (args...) -> @engine.match args...
  getUrl: (queryTerms) -> @engine.getUrl [ @queryTerms..., queryTerms... ]
  parse: (xhr) ->
    @engine.parse(xhr)
      .filter (suggestion) => suggestion.startsWith @prefix
      .map (suggestion) => suggestion[@prefix.length..].ltrim()

# For Google Maps, we add the prefix "map of" to the query, and send it to Google's general search engine,
# then strip "map of" from the resulting suggestions.
class GoogleMaps extends GoogleWithPrefix
  # Example search URL: https://www.google.com/maps?q=%s
  constructor: -> super "map of", "^https?://[a-z]+\\.google\\.(com|ie|co\\.uk|ca|com\\.au)/maps"

class Youtube extends GoogleXMLRegexpEngine
  # Example search URL: http://www.youtube.com/results?search_query=%s
  constructor: ->
    super "^https?://[a-z]+\\.youtube\\.com/results"
{'fraction_non_alphanumeric': 0.11768349334159182, 'fraction_numerical': 0.0009290802105915144, 'mean_word_length': 3.7809030347890453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2879934', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1836, 'n_words': 694}