-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cutmorph.py
93 lines (78 loc) · 2.51 KB
/
Cutmorph.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/python
#-*- coding:utf-8 -*-
"""
文章の解析
->形態素解析
"""
import re
import sys
# ファイル関係
def FOpen(_file):
dat_file = ''
if len(sys.argv)==1: dat_file = _file
else: dat_file = sys.argv[1]
f = open(dat_file,'r')
lines = f.readlines()
return lines,dat_file
# unicode変換
def toUnicode(_lines):
lines = []
for i in range(len(_lines)):
lines.append(_lines[i].decode('utf-8'))
return lines
#形態素の切り出し
def Morph(_str):
nowMode = 'None'
preMode = 'None'
morphList = []
index = -1
for i in range(len(_str)):
# 長音「ー」は前の形態に引っ張られる
if re.match(u"[ー]", _str[i]) != None:
nowMode = preMode
elif re.match(u"[\u4e00-\u9fa5]", _str[i]) != None:
nowMode = 'ZEN_KANJI'
elif re.match(u"[ぁ-ゞ]", _str[i]) != None:
nowMode = 'ZEN_HIRAGANA'
elif re.match(u"[ァ-ヾ]", _str[i]) != None:
nowMode = 'ZEN_KATAKANA'
elif re.match(u"[ヲ-ン]", _str[i]) != None:
nowMode = 'HAN_KATAKANA'
elif re.match(u"[。、!?―()・]", _str[i]) != None:
nowMode = 'ZEN_KIGOU'
# if preMode.split("_")[0] == 'ZEN':
# nowMode = preMode
elif re.match(u"[\.,!\?\-\(\)…]", _str[i]) != None:
nowMode = 'HAN_KIGOU'
# if preMode.split("_")[0] == 'HAN':
# nowMode = preMode
elif re.match(u"[゙゚]", _str[i]) != None:
nowMode = 'OTHER'
if preMode == 'HAN_KATAKANA':
nowMode = preMode
elif re.match(u"[a-zA-Z]", _str[i]) != None:
nowMode = 'ALPHABET'
elif re.match(u"[0-9]", _str[i]) != None:
nowMode = 'HAN_NUMBER'
elif re.match(u"[0-9]", _str[i]) != None:
nowMode = 'ZEN_NUMBER'
else:
nowMode = 'OTHER'
if nowMode == 'OTHER': continue
if nowMode != preMode:
morphList.append(_str[i])
index += 1
else:
morphList[index]+=_str[i]
preMode = nowMode
return morphList
# main
lines,dat_file = FOpen(u'text/sample.txt')
lines = toUnicode(lines)
lines = "".join(lines)
morph = Morph(lines)
fn = u'save/m_'+(dat_file.split('/')[-1]).split('.')[0]+'.txt'
f = open(fn,'w')
for i in morph:
f.write(i.encode("utf-8")+"\n")
f.close()