-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipadic.py
42 lines (31 loc) · 1.11 KB
/
ipadic.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
# -*- coding: utf-8 -*-
import os
import re
from pytrie import StringTrie
pattern = re.compile((u'\(品詞 \((?P<pos>.*)\)\) '
u'\(\(見出し語 \((?P<basic>.*) (\d+)\)\) '
u'\(読み (?P<reading>.*)\) \(発音 (?P<pron>.*)\) \)'))
class IpadicTrie(StringTrie):
def __init__(self, dirname=None):
super(IpadicTrie, self).__init__(self)
if dirname: self.initialize(dirname)
def initialize(self, dirname):
for filename in os.listdir(dirname):
if filename.endswith('.dic'):
filepath = os.path.join(dirname, filename)
print 'reading', filepath
self.add_dic(filepath)
def add_dic(self, filepath, encoding='euc-jp'):
for line in file(filepath):
line = line.strip().decode(encoding)
m = pattern.match(line)
reading = m.group('reading')
basic = m.group('basic')
if reading not in self:
self[reading] = []
self[reading].append(basic)
def main():
import sys
trie = IpadicTrie(sys.argv[1])
if __name__ == '__main__':
main()