preprocessing.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. '''
  2. 360万中文训练集标签修改
  3. '''
  4. # chinese characters dictionary for 3.6 million data set.
  5. with open('../char_std_5990.txt', 'rb') as file:
  6. char_dict = {num : char.strip().decode('gbk','ignore') for num, char in enumerate(file.readlines())}
  7. # processing output
  8. with open('../test.txt') as file:
  9. value_list = ['%s %s'%(segment_list.split(' ')[0], ''.join([char_dict[int(val)] for val in segment_list[:-1].split(' ')[1:]])) for segment_list in file.readlines()]
  10. # final output
  11. with open('test.txt', 'w', encoding='utf-8') as file:
  12. [file.write(val+'\n') for val in value_list]
  13. '''
  14. orginal version
  15. '''
  16. # with open('../char_std_5990.txt', 'rb') as file:
  17. # char_dict = {num : char.strip().decode('gbk','ignore') for num, char in enumerate(file.readlines())}
  18. # value_list = []
  19. # with open('../test.txt') as file:
  20. # label_list = file.readlines()
  21. # for segment_list in label_list:
  22. # key = segment_list.split(' ')[0]
  23. # segment_list = segment_list[:-1].split(' ')[1:]
  24. # temp = [char_dict[int(val)] for val in segment_list]
  25. # value_list.append('%s %s'%(key, ''.join(temp)))
  26. # with open('test.txt', 'w', encoding='utf-8') as file:
  27. # [ file.write(val+'\n') for val in value_list]