from typing import Counter


def collect_entities(triples, min_freq=0):
    """
    Given a list of triples, as returned by ``read_triples()``, collect the entities used on the LHS
    and RHS of the facts, filtering out ones that occur rarely.

    """
    terms = sum(([l, r] for (l, p, r) in triples), [])
    term_counts = Counter(terms)
    # Select a query term from those that have at least a few facts about them
    freq_terms = list(set(t for (t, c) in term_counts.items() if c >= min_freq))
    return freq_terms