<filename>simiir/query_generators/google_suggest_random_generator.py
from simiir.query_generators.base_generator import BaseQueryGenerator
import requests
import random
import urllib.parse

class GoogleSuggestRandomGenerator(BaseQueryGenerator):
    """
    A query generator that choses a random candidate queries from the Google Search Suggest API.
    """
    def __init__(self, stopword_file, background_file=[], max_depth=5):
        super(GoogleSuggestRandomGenerator, self).__init__(stopword_file, background_file=background_file)
        self.__max_depth = max_depth
    
    def generate_query_list(self, search_context):
        """
        Given a Topic object, produces a list of query terms that could be issued by the simulated agent.
        """

        topic = search_context.topic.title
        generated_queries = [(topic, 1)]

        for i in range(1, self.__max_depth):
            response = requests.get('https://suggestqueries.google.com/complete/search?&output=firefox&hl=en&gl=us&q=' + urllib.parse.quote_plus(generated_queries[-1][0]))
            suggestions_web = response.json()
            queries = suggestions_web[1]
            if len(queries) < 2:
                break
            del queries[0]
            generated_queries.append((random.choice(queries), 1))
        return generated_queries

<filename>simiir/query_generators/google_suggest_random_generator.py
from simiir.query_generators.base_generator import BaseQueryGenerator
import requests
import random
import urllib.parse

class GoogleSuggestRandomGenerator(BaseQueryGenerator):
    """
    A query generator that choses a random candidate queries from the Google Search Suggest API.
    """
    def __init__(self, stopword_file, background_file=[], max_depth=5):
        super(GoogleSuggestRandomGenerator, self).__init__(stopword_file, background_file=background_file)
        self.__max_depth = max_depth
    
    def generate_query_list(self, search_context):
        """
        Given a Topic object, produces a list of query terms that could be issued by the simulated agent.
        """

        topic = search_context.topic.title
        generated_queries = [(topic, 1)]

        for i in range(1, self.__max_depth):
            response = requests.get('https://suggestqueries.google.com/complete/search?&output=firefox&hl=en&gl=us&q=' + urllib.parse.quote_plus(generated_queries[-1][0]))
            suggestions_web = response.json()
            queries = suggestions_web[1]
            if len(queries) < 2:
                break
            del queries[0]
            generated_queries.append((random.choice(queries), 1))
        return generated_queries

A query generator that choses a random candidate queries from the Google Search Suggest API. Given a Topic object, produces a list of query terms that could be issued by the simulated agent.