-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpos2word.py
40 lines (34 loc) · 1.19 KB
/
pos2word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: 52nlpcn@gmail.com
# Copyright 2014 @ YuZhen Technology
#
# Combining characters based the 4-tag tagging info
# modify by hcs
import codecs
import sys
def character_2_word(input_file, output_file):
input_data = codecs.open(input_file, 'r', 'utf-8')
output_data = codecs.open(output_file, 'w', 'utf-8')
# 4 tags for character tagging: B(Begin), E(End), M(Middle), S(Single)
for line in input_data.readlines():
char_tag_list = line.strip().split()
for char_tag in char_tag_list:
char_tag_pair = char_tag.split('/')
char = char_tag_pair[0]
tag = char_tag_pair[1]
if tag == 'B':
output_data.write(' ' + char)
elif tag == 'M':
output_data.write(char)
elif tag == 'E':
output_data.write(char + ' ')
else: # tag == 'S'
output_data.write(' ' + char + ' ')
output_data.write("\n")
input_data.close()
output_data.close()
if __name__ == '__main__':
input_file = 'output/pos_tagging.txt'
output_file = 'output/result.txt'
character_2_word(input_file, output_file)