~jtv/corpusfiltergraph/cross-python

« back to all changes in this revision

Viewing changes to trunk/lib/corpusfg/graphs/extract-dictionary/aligner-extract-dictionary.py

  • Committer: tahoar
  • Date: 2012-05-02 15:46:23 UTC
  • Revision ID: svn-v4:bc069b21-dff4-4e29-a776-06a4e04bad4e::266
new layout. need to update code to use the new layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# -*- coding: utf8 -*-
 
3
 
 
4
#===============================================================================
 
5
# Author: Walapa Muangjeen
 
6
#===============================================================================
 
7
 
 
8
#version:
 
9
#4.0.264 - version update
 
10
 
 
11
import re
 
12
import common as cf
 
13
 
 
14
class aligner(object):
 
15
 
 
16
        cfg = {
 
17
                'version': '4.0.264',
 
18
                }
 
19
        isopen = False
 
20
        p = object
 
21
        errors = []
 
22
 
 
23
        def open(self,parent,cfg):
 
24
                self.newkey = {}
 
25
                self.regex = {}
 
26
                self.regex['zh_hk'] = re.compile(u"“(.*?)”( |)\((.*?)\)")
 
27
                self.regex['en']    = re.compile(u"\"(.*?)\"( |)\((.*?)\)")
 
28
                self.a = ( u'( \(|\()', u'(\)\*|\)|\) )' )
 
29
                self.b = {}
 
30
                self.b['zh_hk'] = ( u'”、“', u'”和“' )
 
31
                self.b['en']    = ( u'', u'' )
 
32
                self.z = {}
 
33
                self.z['zh_hk'] = ( u'”、“', '', u', ' )
 
34
                self.z['en']    = ( u'" and "','' , u'、' )
 
35
 
 
36
        def run(self,ktgt):
 
37
                '''run filter'''
 
38
 
 
39
                ksrc = list(ktgt)
 
40
                ksrc[cf.rdrlang] = ktgt[cf.srclang]
 
41
                ksrc = tuple(ksrc)
 
42
 
 
43
                newfilelist = list(self.p.cfoutput[ksrc]['filelist'])
 
44
                newfilelist[cf.kind] = newfilelist[cf.kind] + '-dictionary'
 
45
                self.newkey[(ktgt[cf.srclang],ktgt[cf.srclang])] = tuple(newfilelist[cf.stage+1:])
 
46
 
 
47
                newfilelist = list(self.p.cfoutput[ktgt]['filelist'])
 
48
                newfilelist[cf.kind] = newfilelist[cf.kind] + '-dictionary'
 
49
                self.newkey[(ktgt[cf.srclang],ktgt[cf.rdrlang])] = tuple(newfilelist[cf.stage+1:])
 
50
 
 
51
                newfilelist = list(self.p.cfoutput[ktgt]['filelist'])
 
52
                newfilelist[cf.kind] = newfilelist[cf.kind] + '-dictionary'
 
53
                newfilelist[cf.srclang] = ktgt[cf.rdrlang]
 
54
                self.newkey[(ktgt[cf.rdrlang],ktgt[cf.rdrlang])] = tuple(newfilelist[cf.stage+1:])
 
55
 
 
56
                newfilelist = list(self.p.cfoutput[ksrc]['filelist'])
 
57
                newfilelist[cf.kind] = newfilelist[cf.kind] + '-dictionary'
 
58
                newfilelist[cf.srclang] = ktgt[cf.rdrlang]
 
59
                self.newkey[(ktgt[cf.rdrlang],ktgt[cf.srclang])] = tuple(newfilelist[cf.stage+1:])
 
60
 
 
61
                for linenum in range(len(self.p.cfoutput[ktgt]['tempbuff'])):
 
62
                        self.p.cfoutput[ktgt]['tempbuff'][linenum] = self.__filter(self.p.cfoutput[ktgt]['tempbuff'][linenum], linenum, ktgt, ktgt[cf.rdrlang])
 
63
                        self.p.cfoutput[ksrc]['tempbuff'][linenum] = self.__filter(self.p.cfoutput[ksrc]['tempbuff'][linenum], linenum, ktgt, ktgt[cf.srclang])
 
64
 
 
65
                self.p.writer.run(self.p.cfextract)
 
66
                self.p.writer.flush(self.p.cfextract)
 
67
 
 
68
        def flush(self,ktgt):
 
69
                return
 
70
 
 
71
        def close(self):
 
72
                return
 
73
 
 
74
        def __filter(self,text,linenum,ktgt,lang):
 
75
                '''extract dictionary'''
 
76
                results = re.findall(self.regex[lang], text)
 
77
                for found in results:
 
78
                        for lefttxt in found[0].split(self.z[lang][0]):
 
79
                                for righttxt in found[2].split(self.z[lang][2]):
 
80
                                        if lang == ktgt[cf.srclang]:
 
81
                                                self.p.cfoutput[self.newkey[(ktgt[cf.srclang],ktgt[cf.srclang])]]['tempbuff'].append(lefttxt)
 
82
                                                self.p.cfoutput[self.newkey[(ktgt[cf.srclang],ktgt[cf.rdrlang])]]['tempbuff'].append(righttxt)
 
83
                                        else:
 
84
                                                self.p.cfoutput[self.newkey[(ktgt[cf.rdrlang],ktgt[cf.srclang])]]['tempbuff'].append(righttxt)
 
85
                                                self.p.cfoutput[self.newkey[(ktgt[cf.rdrlang],ktgt[cf.rdrlang])]]['tempbuff'].append(lefttxt)
 
86
                                        text = re.sub(self.a[0] + found[2] + self.a[1], '', text)
 
87
                                        text = re.sub(self.b[lang][0], self.b[lang][1], text)
 
88
                return text
 
89
 
 
90
def usage():
 
91
        '''Command prompt help.'''
 
92
        return "\n%s\n\tUsage:\n\tfrom %s import filtergraph\n"%(
 
93
        os.path.basename(sys.argv[0]),
 
94
        os.path.splitext(os.path.basename(sys.argv[0]))[0]
 
95
        )
 
96
 
 
97
licensetxt=u'''CorpusFiltergraph™ v4.0
 
98
Copyright © 2010-2012 Precision Translation Tools Co., Ltd.
 
99
 
 
100
This program is free software: you can redistribute it and/or modify
 
101
it under the terms of the GNU Lesser General Public License as published by
 
102
the Free Software Foundation, either version 3 of the License, or
 
103
(at your option) any later version.
 
104
 
 
105
This program is distributed in the hope that it will be useful,
 
106
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
107
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
108
GNU Lesser General Public License for more details.
 
109
 
 
110
You should have received a copy of the GNU Lesser General Public License
 
111
along with this program.  If not, see http://www.gnu.org/licenses/.
 
112
 
 
113
For more information, please contact Precision Translation Tools Co., Ltd.
 
114
at: http://www.precisiontranslationtools.com'''
 
115
 
 
116
if __name__ == "__main__":
 
117
        import os
 
118
        import sys
 
119
        sys.stdout.write(usage().encode('utf8')+'\n')