Exemplo n.º 1
0
def get_prediction(db_user: User, db_issue: Issue, search_value: str,
                   mode: int, statement_uid: int) -> dict:
    """
    Get dictionary with matching words, based on the given mode

    :param statement_uid: the uid of the statement to be looked at
    :param db_user: Current user
    :param db_issue:  current Issue the user looks at, used to get the uid of the issue to search at
    :param search_value: users value, which should be the base for searching
    :param mode: form of search the user chooses
    :return: Dictionary with the corresponding search results
    """

    history = db_user.history
    seen_statements = get_seen_statements_from(
        history[len(history) - 1].path) if history != [] else []

    if is_host_resolved(SEARCH_HOST):
        if is_socket_open(SEARCH_HOST, SEARCH_PORT):
            elastic_results = elastic_search(db_issue, search_value, mode,
                                             statement_uid)
            elastic_results['values'] = [
                item for item in elastic_results.get('values')
                if item.get('statement_uid') not in seen_statements
            ]
            return elastic_results
    levensthein_results = __levensthein_search(db_user, db_issue, search_value,
                                               mode, statement_uid)
    levensthein_results['values'] = [
        item for item in levensthein_results.get('values')
        if item.get('statement_uid') not in seen_statements
    ]
    return levensthein_results
Exemplo n.º 2
0
def get_all_statements_matching(search_value: str) -> dict:
    """
    This returns all similar statements and the corresponding information for a given search term.
    Either Elasticsearch or the Levensthein distance is used. The default is Levensthein distance.
    If search is running on port 5000, the results will be generated by Elasticsearch.
    It is tested with each request whether the host (search) is reachable or not.

    :param search_value: A word or sentence to be searched for.
    :return: A dictionary containing the results and additional information of the search.
    """
    if is_host_resolved(SEARCH_HOST):
        if is_socket_open(SEARCH_HOST, SEARCH_PORT):
            return get_statements_with_similarity_to(search_value)
    return get_all_statements_by_levensthein_similar_to(search_value)