import re


def load_data_crf_string(crfString):
    """
    Load data and features from a CRF matrix file 
    the format is as follow:

    token_0 f0_0 f0_1 ... f0_n
    token_1 f1_0 f1_1 ... f1_n
    ...
    token_m fm_0 fm_1 ... fm_n

    field separator can be either space or tab

    Returns:
        tuple(numpy array, numpy array): tokens, features

    """
    sents = []
    featureSets = []
    tokens, features = [], []
    #print("crfString:", crfString)
    for line in crfString.splitlines():
        line = line.strip(' \t')
        if len(line) == 0:
            if len(tokens) != 0:
                sents.append(tokens)
                featureSets.append(features)
                tokens, features = [], []
        else:
            pieces = re.split(' |\t', line)
            token = pieces[0]
            localFeatures = pieces[1:len(pieces)]
            tokens.append(token)
            features.append(localFeatures)
    # last sequence
    if len(tokens) != 0:
        sents.append(tokens)
        featureSets.append(features)

    #print('sents:', len(sents))
    #print('featureSets:', len(featureSets))
    return sents, featureSets