-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection2txt
executable file
·172 lines (154 loc) · 4.47 KB
/
connection2txt
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-
# -*- ruby -*-
require 'romakana'
files = ARGV
outs = {} # 接続先が存在するかどうか
number = {}
number1 = 1 # 非自立語の番号
number2 = 1000 # 自立語の番号
rk = Romakana.new(File.readlines(File.dirname($0) + '/' + 'rklist.pep'),true)
def valid(word) # 自立語かどうか
invalid = [
'[な否定]',
'[ず否定]',
'[た希望]',
'[ば仮定]',
'[ら仮定]',
'[否定希望]',
'[丁寧]',
'[受身接続]',
'[ても]',
'[でも]',
'[だ]',
'[た]',
'[ずる]',
'[せる]',
'[じゃ]',
'[ちゃ]',
'[て]',
'[てくる]',
'[で]',
'[でくる]',
'[なだ]',
'[な]',
'[なあ]',
'[たがる]',
'[漢数字3]', # 「億」などは先頭に来ない
]
!invalid.member?(word) && word !~ /語尾\]/
end
#
# パス1: 単語または単語クラスに番号をふる
#
lines = {}
files.each { |file|
defaultwordclass = ''
defaultnextclass = ''
File.open(file){ |f|
# STDERR.puts "#{file}...."
f.each { |line|
line.chomp!
next if line =~ /^\s*$/ # 空白行除去
next if line =~ /^#/ # コメント除去
(yomi, word, wordclass, nextclass) = line.split(/\s+/)
if !wordclass && defaultwordclass == '' then
wordclass = word
end
if wordclass && !nextclass then
nextclass = ''
end
wordclass = defaultwordclass unless wordclass
nextclass = defaultnextclass unless nextclass
defaultwordclass = wordclass
defaultnextclass = nextclass
l = "#{yomi} #{word} #{wordclass} #{nextclass}"
next if lines[l] # 同じエントリ除去
lines[l] = true
# STDERR.puts "#{yomi} #{word} <#{wordclass}> <#{nextclass}>"
if nextclass != '' then
outs[nextclass] = true # 接続先が存在するとき
end
#
# 単語や単語クラス番号づけ
# xxxという単語の番号はnumber[xxx]に格納
#
if wordclass != '' && number[wordclass].nil? then
#
# 自立語はnumber2, 非自立語はnumber1の番号を割り当てる
#
if valid(wordclass) then # 自立語
number[wordclass] = number2
number2 += 1
else
number[wordclass] = number1
number1 += 1
end
end
if nextclass != '' && nextclass && number[nextclass].nil? then
if valid(nextclass) then
number[nextclass] = number2
number2 += 1
else
number[nextclass] = number1
number1 += 1
end
end
}
}
}
number.each { |key,val|
# STDERR.puts "#{key} #{val}"
}
#
# パス2:
#
wroma = {}
words = {} # 出力された単語リスト
nextwords = {} # 接続先として出現した単語リスト
lines = {}
files.each { |file|
defaultwordclass = ''
defaultnextclass = ''
File.open(file){ |f|
# STDERR.puts "#{file}...."
f.each { |line|
line.chomp!
next if line =~ /^\s*$/ # 空白行除去
next if line =~ /^#/ # コメント除去
# lines[line] = true
(yomi, word, wordclass, nextclass) = line.split(/\s+/)
if !wordclass && defaultwordclass == '' then
wordclass = word
end
if wordclass && !nextclass then
nextclass = ''
end
wordclass = defaultwordclass unless wordclass
nextclass = defaultnextclass unless nextclass
defaultwordclass = wordclass
defaultnextclass = nextclass
l = "#{yomi} #{word} #{wordclass} #{nextclass}"
next if lines[l] # 同じエントリ除去
lines[l] = true
#
# 接続先が単語クラスでない場合は接続先単語のリストを生成
#
if nextclass !~ /^\[.*\]$/ then
nextwords[nextclass] = true # 接続先リスト
end
if number[wordclass] then
if wordclass !~ /^\[.*\]$/ then
words[wordclass] = true # 出力された単語リスト
end
romas = [yomi]
if yomi =~ /^\xe3[\x81\x82]/ then
romas = rk.hiragana2roma(yomi)
end
wroma[word] = romas
romas.each { |roma|
puts "#{roma}\t#{word}\t#{number[nextclass].to_i}\t#{number[wordclass].to_i}"
}
end
}
}
}