Query label

Find the indexed files that are the most similar to the query label.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Python

def query_category(index_file, category_string, n_top=3):
    """Find the indexed files that are the most similar to
    the query label.

    The list of all available labels:
    https://cloud.google.com/natural-language/docs/categories
    """

    with io.open(index_file, "r") as f:
        index = json.load(f)

    # Make the category_string into a dictionary so that it is
    # of the same format as what we get by calling classify.
    query_categories = {category_string: 1.0}

    similarities = []
    for filename, categories in six.iteritems(index):
        similarities.append((filename, similarity(query_categories, categories)))

    similarities = sorted(similarities, key=lambda p: p[1], reverse=True)

    print("=" * 20)
    print("Query: {}\n".format(category_string))
    print("\nMost similar {} indexed texts:".format(n_top))
    for filename, sim in similarities[:n_top]:
        print("\tFilename: {}".format(filename))
        print("\tSimilarity: {}".format(sim))
        print("\n")

    return similarities

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser