text_model/data.py
def read_wembed_txt(wembed_file, sep=" "):
    """
    Read word embedding from text file.

    Args:
        sep: seperate character within a single line.
    """
    print("Reading word embeddings from txt file: %s" % (wembed_file))
    with open(wembed_file) as fin:
        header = fin.readline().strip('\n').split(sep)
        num = int(header[0])
        dim = int(header[1])
        embeddings = np.zeros(shape=[num, dim], dtype=NP_DTYPE)
        for idx, line in enumerate(fin.readlines()):
            line = line.strip('\n')
            word_pos = line.find(sep)
            word = line[:word_pos]
            array = line[word_pos+1:]
            embeddings[idx] = np.fromstring(array, sep=sep)
    if (idx+1) != num:
        raise ValueError("Word embedding num[%d] not match with header[%d]" % (idx+1, num))
    print("Read word embeddings finished, vocab_size=%d, dim=%d" % (num, dim))
    return embeddings

    Read word embedding from text file.

    Args:
        sep: seperate character within a single line.
    
Read word embedding from text file.
Read word embedding from text file .
def read_wembed_txt(wembed_file, sep=" "):
    print("Reading word embeddings from txt file: %s" % (wembed_file))
    with open(wembed_file) as fin:
        header = fin.readline().strip('\n').split(sep)
        num = int(header[0])
        dim = int(header[1])
        embeddings = np.zeros(shape=[num, dim], dtype=NP_DTYPE)
        for idx, line in enumerate(fin.readlines()):
            line = line.strip('\n')
            word_pos = line.find(sep)
            word = line[:word_pos]
            array = line[word_pos+1:]
            embeddings[idx] = np.fromstring(array, sep=sep)
    if (idx+1) != num:
        raise ValueError("Word embedding num[%d] not match with header[%d]" % (idx+1, num))
    print("Read word embeddings finished, vocab_size=%d, dim=%d" % (num, dim))
    return embeddings
def read_wembed_txt ( wembed_file , sep = " " ) : print ( "Reading word embeddings from txt file: %s" % ( wembed_file ) ) with open ( wembed_file ) as fin : header = fin . readline ( ) . strip ( '\n' ) . split ( sep ) num = int ( header [ 0 ] ) dim = int ( header [ 1 ] ) embeddings = np . zeros ( shape = [ num , dim ] , dtype = NP_DTYPE ) for idx , line in enumerate ( fin . readlines ( ) ) : line = line . strip ( '\n' ) word_pos = line . find ( sep ) word = line [ : word_pos ] array = line [ word_pos + 1 : ] embeddings [ idx ] = np . fromstring ( array , sep = sep ) if ( idx + 1 ) != num : raise ValueError ( "Word embedding num[%d] not match with header[%d]" % ( idx + 1 , num ) ) print ( "Read word embeddings finished, vocab_size=%d, dim=%d" % ( num , dim ) ) return embeddings
Read word embedding from text file.
Read word embedding from text file .
"""
    Read word embedding from text file.

    Args:
        sep: seperate character within a single line.
    """
wembed_file
sep
seperate character within a single line.
seperate character within a single line .