<commit_before>package acr.browser.lightning.search.suggestions

import acr.browser.lightning.database.SearchSuggestion
import acr.browser.lightning.extensions.safeUse
import acr.browser.lightning.log.Logger
import io.reactivex.Single
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.ResponseBody
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.util.*

/**
 * The base search suggestions API. Provides common fetching and caching functionality for each
 * potential suggestions provider.
 */
abstract class BaseSuggestionsModel internal constructor(
    private val httpClient: OkHttpClient,
    private val requestFactory: RequestFactory,
    private val encoding: String,
    locale: Locale,
    private val logger: Logger
) : SuggestionsRepository {

    private val language = locale.language.takeIf(String::isNotEmpty) ?: DEFAULT_LANGUAGE

    /**
     * Create a URL for the given query in the given language.
     *
     * @param query    the query that was made.
     * @param language the locale of the user.
     * @return should return a [HttpUrl] that can be fetched using a GET.
     */
    abstract fun createQueryUrl(query: String, language: String): HttpUrl

    /**
     * Parse the results of an input stream into a list of [SearchSuggestion].
     *
     * @param responseBody the raw [ResponseBody] to parse.
     */
    @Throws(Exception::class)
    protected abstract fun parseResults(responseBody: ResponseBody): List<SearchSuggestion>

    override fun resultsForSearch(rawQuery: String): Single<List<SearchSuggestion>> = Single.fromCallable {
        val query = try {
            URLEncoder.encode(rawQuery, encoding)
        } catch (throwable: UnsupportedEncodingException) {
            logger.log(TAG, "Unable to encode the URL", throwable)

            return@fromCallable emptyList<SearchSuggestion>()
        }

        return@fromCallable downloadSuggestionsForQuery(query, language)
            ?.let(Response::body)
            ?.safeUse(::parseResults)
            ?.take(MAX_RESULTS)
    }
<commit_msg>Fixing crash caused by network connectivity problems<commit_after>package acr.browser.lightning.search.suggestions

import acr.browser.lightning.database.SearchSuggestion
import acr.browser.lightning.extensions.safeUse
import acr.browser.lightning.log.Logger
import io.reactivex.Single
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.ResponseBody
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.util.*

/**
 * The base search suggestions API. Provides common fetching and caching functionality for each
 * potential suggestions provider.
 */
abstract class BaseSuggestionsModel internal constructor(
    private val httpClient: OkHttpClient,
    private val requestFactory: RequestFactory,
    private val encoding: String,
    locale: Locale,
    private val logger: Logger
) : SuggestionsRepository {

    private val language = locale.language.takeIf(String::isNotEmpty) ?: DEFAULT_LANGUAGE

    /**
     * Create a URL for the given query in the given language.
     *
     * @param query    the query that was made.
     * @param language the locale of the user.
     * @return should return a [HttpUrl] that can be fetched using a GET.
     */
    abstract fun createQueryUrl(query: String, language: String): HttpUrl

    /**
     * Parse the results of an input stream into a list of [SearchSuggestion].
     *
     * @param responseBody the raw [ResponseBody] to parse.
     */
    @Throws(Exception::class)
    protected abstract fun parseResults(responseBody: ResponseBody): List<SearchSuggestion>

    override fun resultsForSearch(rawQuery: String): Single<List<SearchSuggestion>> = Single.fromCallable {
        val query = try {
            URLEncoder.encode(rawQuery, encoding)
        } catch (throwable: UnsupportedEncodingException) {
            logger.log(TAG, "Unable to encode the URL", throwable)

            return@fromCallable emptyList<SearchSuggestion>()
        }

        return@fromCallable downloadSuggestionsForQuery(query, language)
            ?.let(Response::body)
            ?.safeUse(::parseResults)
            ?.take(MAX_RESULTS) ?: emptyList()
    }

{'fraction_non_alphanumeric': 0.08018433179723503, 'fraction_numerical': 0.0018433179723502304, 'mean_word_length': 3.5646687697160884, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4827289', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1138, 'n_words': 347}