使用TensorFlow将词语转化为数字索引

TensorFlow中内建的类tf.contrib.learn.preprocessing.VocabularyProcessor( max_document_length, min_frequency=0, vocabulary=None, tokenizer_fn=None)可以返回一个“能够将文档中的词汇转化为数字索引文档”的对象。其中,max_document_length表示转换完之后,文档中,每句话的长度,min_frequency=0表示文档中,每个词出现的频次最小数。

from tensorflow.contrib import learn

texts = ['go until jurong point crazy available only in bugis n great world la e buffet cine there got amore wat',
 'ok lar joking wif u oni',
 'free entry in a wkly comp to win fa cup final tkts st may text fa to to receive entry questionstd txt ratetcs apply overs',
 'u dun say so early hor u c already then say',
 'nah i dont think he goes to usf he lives around here though']

texts2 = texts[0:5]
vocab_processor = learn.preprocessing.VocabularyProcessor(20, min_frequency=1)
transformed_texts = np.array([x for x in vocab_processor.transform(texts)])
print(transformed_texts)

## 运行结果:
[[   1    2    3 ...   18   19   20]
 [  21   22   23 ...    0    0    0]
 [  27   28    8 ...   32   41   28]
 ...
 [7687  302    8 ...    0    0    0]
 [ 128 3066  205 ...  166   68   54]
 [3173   64 1156 ...    0    0    0]]

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值