File size: 7,480 Bytes
21baa2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176

'''
Created on ٢٩‏/٠٤‏/٢٠١٠

@Created by: Muhammad Altabba
'''

from ..Lexicon.RootAndPatterns.Root import *;
from ..Lexicon.RootAndPatterns.UnvoweledPattern import *;
from ..Lexicon.RootAndPatterns.VoweledPattern import *;
from ..Lexicon.RootAndPatterns.VoweledNominalPattern import *;
from ..Lexicon.RootAndPatterns.VoweledVerbalPattern import *;

from xml.dom import minidom;
import os;
from os.path import join, getsize;

Error_Log = [];

class RootsAndPatternsRepository(object):
    """
     # PyUML: Do not remove this line! # XMI_ID:_q0Bf1435Ed-gg8GOK1TmhA
    """
    '''
    classdocs
    '''
    NominalRoots = dict();
    VoweledNominalPatterns = dict();
    UnvoweledNominalPatterns = dict();
    
    VerbalRoots = dict();
    VoweledVerbalPatterns = dict();
    UnvoweledVerbalPatterns = dict();
    
    
    
    def __init__(self):
        '''
        Constructor
        '''
        self.NominalRoots = [];
        self.VoweledNominalPatterns = {};
        self.UnvoweledNominalPatterns = {};
        
        self.VerbalRoots = [];
        self.VoweledVerbalPatterns = {};
        self.UnvoweledVerbalPatterns = {};
        
    pass
    
    def Load(self, basePath, rootsFolder):
        #example: basePath= 'D:\1\Learning\NLP\برامج\الخليل\AlKhalil_1\db\';
                
        self.NominalRoots = self.LoadRoots(basePath + 'nouns/'+ rootsFolder +'/');
        self.VerbalRoots = self.LoadRoots(basePath + 'verbs/'+ rootsFolder +'/');
        self.UnvoweledNominalPatterns = self.LoadUnvoweledPatterns(basePath + 'nouns/patterns/Unvoweled/');
        self.UnvoweledVerbalPatterns = self.LoadUnvoweledPatterns(basePath + 'verbs/patterns/Unvoweled/');
        self.VoweledNominalPatterns = self.LoadVoweledNominalPatterns(basePath + 'nouns/patterns/Voweled/');
        self.VoweledVerbalPatterns = self.LoadVoweledVerbalPatterns(basePath + 'verbs/patterns/Voweled/');
        
        
    pass
    
    # Return Roots in an in-memory dictionary:
    def LoadRoots(self, path):
        
        tempDict = dict();
        
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith('.xml'):
                    tempSubDict = {};
                    xmldoc = minidom.parse(root+file);
                    for xmlRoot in xmldoc.getElementsByTagName('root'):
                        val = xmlRoot.attributes['val'].value;
                        vect = xmlRoot.attributes['vect'].value.split();
                        vect = [int(x) for x in vect];
                        
                        tempSubDict[val] = Root(val,vect);
                    tempDict[file[0]] = tempSubDict;

        return tempDict;
    pass
    
    # Return Unvoweled Patterns in an in-memory dictionary:
    def LoadUnvoweledPatterns(self, path):
        
        tempDict = {}; #Based on word length.
        
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith('.xml'):
                    tempSubDict = {}; #Based on the String of the Pattern 
                    tempDict[int(file[file.find('.')-1])] = tempSubDict;
                    xmldoc = minidom.parse(root+file);
                    for xmlRoot in xmldoc.getElementsByTagName('pattern'):
                        value = xmlRoot.attributes['value'].value;
                        rules = xmlRoot.attributes['rules'].value.split();
                        ids = xmlRoot.attributes['ids'].value.split();
                        ids = [int(x) for x in ids];
                        tempSubDict[value] = UnvoweledPattern(value, rules, ids);

        return tempDict;
    pass
        
    # Return Voweled Patterns in an in-memory dictionary:
    def LoadVoweledNominalPatterns(self, path):
        
        tempDict = {}; #Based on word length.
        
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith('.xml'):
                    tempSubDict = {}; #Based on IDs
                    tempDict[int(file[file.find('.')-1])] = tempSubDict;
                    xmldoc = minidom.parse(root+file);
                    for xmlRoot in xmldoc.getElementsByTagName('pattern'):
                        id = int(xmlRoot.attributes['id'].value);
                        diac = xmlRoot.attributes['diac'].value;
                        canonic = xmlRoot.attributes['canonic'].value;
                        type = xmlRoot.attributes['type'].value;
                        cas = xmlRoot.attributes['cas'].value;
                        ncg = xmlRoot.attributes['ncg'].value;
                        
                        if(ncg == ''):
                            Error_Log.append('Error At file [VoweledNominalPattern'+file[file.find('.')-1]+'.xml], for pattern with [id = '+str(id)+']'\
                                             +'\n\tthe [type] attribute\'s value ['+type+']!, '\
                                             +'\n\tthe [cas] attribute\'s value ['+cas+']!'\
                                             +'\n\t and the [ncg] attribute is empty!')
                            ncg = cas;
                            cas = type;
                            type = '';
                        
                        voweledPattern = VoweledNominalPattern(id, diac, canonic);
                        
                        if(type == 'ص'):
                            Error_Log.append('Error At file [VoweledNominalPattern'+file[file.find('.')-1]+'.xml], for pattern with [id = '+str(id)+']'\
                                             +'\n\tthe [type] attribute\'s value ['+type+']! which is undefined,'\
                                             +'\n\tthe [cas] attribute\'s value ['+cas+']'\
                                             +'\n\tand the [ncg] attribute\'s value ['+ncg+'!')
                            type = "صأ"
                            
                        voweledPattern.AssignFromAlKhalilDB(type, cas, ncg);
                        tempSubDict[id] = voweledPattern;

        return tempDict;
    pass
        
    # Return Voweled Verbal Patterns in an in-memory dictionary:
    def LoadVoweledVerbalPatterns(self, path):
        
        tempDict = {}; #Based on word length.
        
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith('.xml'):
                    tempSubDict = {}; #Based on IDs
                    tempDict[int(file[file.find('.')-1])] = tempSubDict;
                    xmldoc = minidom.parse(root+file);
                    for xmlRoot in xmldoc.getElementsByTagName('pattern'):
                        id = int(xmlRoot.attributes['id'].value);
                        diac = xmlRoot.attributes['diac'].value;
                        canonic = xmlRoot.attributes['canonic'].value;
                        type = xmlRoot.attributes['type'].value;
                        cas = xmlRoot.attributes['cas'].value;
                        ncg = xmlRoot.attributes['ncg'].value;
                        aug = xmlRoot.attributes['aug'].value;
                        trans = xmlRoot.attributes['trans'].value;
                        
                        voweledPattern = VoweledVerbalPattern(id, diac, canonic);
                        voweledPattern.AssignFromAlKhalilDB(type, cas, ncg, aug, trans);
                        tempSubDict[id] = voweledPattern;

        return tempDict;
    pass