gaussic / text-classification-cnn-rnn

CNN-RNN中文文本分类,基于TensorFlow
MIT License
4.14k stars 1.47k forks source link

about build_vocab #135

Closed rejae closed 4 years ago

rejae commented 4 years ago

构建vocab后的txt中第17行为''字符,

, 的 。 一 是 在 0 有 不 了 中 1 人 大 、 国 2 这 在word_to_id的过程中: : {'': 0, ',': 1, '的': 2, '。': 3, '一': 4, '是': 5, '在': 6, '0': 7, '有': 8, '不': 9, '了': 10, '中': 11, '1': 12, '人': 13, '大': 14, '、': 15, '国': 16, '': 3903, '2': 18, '这': 19, '上': 20, '为': 21, '个': 22, '“': 23, '”': 24, '年': 25, '学': 26, '时': 27, '我': 28, '地': 29, '和': 30, '以': 31, '到': 32, '出': 33, '来': 34, '会': 35, '行': 36, '发': 37, ':': 38, '对': 39, '们': 40, '要': 41, '生': 42, '家': 43, '他': 44, '能': 45, '也': 46, '业': 47, '金': 48, '3': 49, '成': 50, '可': 51, '分': 52, '多': 53, '现': 54, '5': 55, '就': 56, '场': 57, '新': 58, '后': 59, '于': 60, '下': 61, '日': 62, '经': 63, '市': 64, '前': 65, '过': 66, '方': 67, '得': 68, '作': 69, '月': 70, '最': 71, '开': 72, '房': 73, '》': 74, '《': 75, '高': 76, '9': 77, '8': 78, '.': 79, '而': 80, '比': 81, '公': 82, '4': 83, '说': 84, ')': 85, '将': 86, '(': 87, '都': 88, '资': 89, 'e': 90, '6': 91, '基': 92, '用': 93, '面': 94, '产': 95, '还': 96, '自': 97, '者': 98, '本': 99... 在我调试vocab.txt后发现: with open('cnews.vocab.txt', 'r', encoding='utf-8') as f: for i in range( 20): w = f.readline() if i == 17: print(len(w)) print(i, ord(w[0])) print(i, ord(w[1])) # # 2 # 17 32 # 17 10 # for i in range(20): # w = f.readline() # if i == 17: # w = w.replace('\n', '') # print(w) # print(len(w)) # print(i, w, ord(w)) ##17 32 即国之后的空格字符后面跟了个换行符, 跟踪到方法: def read_vocab(vocab_dir): """读取词汇表""" # words = open_file(vocab_dir).read().strip().split('\n') with open_file(vocab_dir) as fp: # 如果是py2 则每个值都转化为unicode words = [native_content(_.strip()) for _ in fp.readlines()]######################## word_to_id = dict(zip(words, range(len(words)))) return words, word_to_id 发现是####################这一行将空格字符的那行strip掉了,然后莫名其妙的就出现了3903的数字,于是我修改###################这一行为: #words = [native_content(item.replace('\n', '')) for item in fp.readlines()] word_to_id就能正确赋值了。 至于那个3903那个奇怪的数字我也没想明白,每个人构建的vocab不同,会有差异,我构建测试了两侧,