import locale


def generate_sorted_sem_terms(sem_features):
    """
    Generates 1, 2, 3, 4-grams of lexicographically sorted semantic terms
    according to UTF-8 order.

    Args:
        sem_features: list of semantic features.

    Returns:
        List of semantic terms in lexicographic order.
    """
    terms = list()

    if len(sem_features) <= 0:
        return terms

    # Sort terms according to UTF-8
    locale.setlocale(locale.LC_ALL, "")
    sem_features.sort(cmp=locale.strcoll)

    # 4 terms
    terms = [[sem_features[i] + '$' + sem_features[i + 1] + '$' +
              sem_features[i + 2] + '$' + sem_features[i + 3]
              for i in range(len(sem_features) - 3)]]

    # 3 terms
    terms += [[sem_features[i] + '$' + sem_features[i + 1] + '$' +
               sem_features[i + 2] for i in range(len(sem_features) - 2)]]

    # 2 terms
    terms += [[sem_features[i] + '$' + sem_features[i + 1]
               for i in range(len(sem_features) - 1)]]

    # 1 term
    terms += [[sem_features[i] for i in range(len(sem_features))]]

    return terms