Blame view

semantics/management/commands/import_frames.py 15.5 KB
Bartłomiej Nitoń authored
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#! /usr/bin/python
# -*- coding: utf-8 -*-

import sys, os, codecs

from django.core.management.base import BaseCommand

from dictionary.models import Lemma
from semantics.models import SemanticRole, FramePosition, Complement, LexicalUnit, SemanticFrame
from settings import PROJECT_PATH

class Command(BaseCommand):
    args = 'none'
    help = ''

    def handle(self, **options):
        #clear_import_data()
        import_frames()

def clear_import_data():
    FramePosition.objects.all().delete()
    Complement.objects.all().delete()
    SemanticFrame.objects.all().delete()

def import_frames():
    verbs_file_path = os.path.join(PROJECT_PATH, 'data', 'Semantics', 'plWN_verbs.csv')
    columns = ['id', 'od', 'do', 'range', 'level', 'ile', 'art', '2ls', 'synset', '4ls', 'xls', 'fnf', 'light', 'alt', 'conv', 'dev', 'senses', 'typ', 'change', 'causephase', 'laspect', 'maspect', 'Agent', 'Manipulator', 'Effector', 'Cognizer', 'Protagonist', 'Benefactor', 'Cause', 'Stimulus', 'Communicator', 'Path', 'Instrument', 'Patient', 'Theme', 'Experiencer', 'Resultee', 'Beneficiary', 'Object', 'Asset', 'Product', 'Content', 'Source', 'Material', 'Goal', 'Entity', 'Part', 'Collection', 'Attribute', 'Event', 'Phase', 'State-of-Affairs', 'Scenario', 'Background', 'Focus', 'Instance', 'Type', 'Location', 'Time']

    fields = {}
    for column in columns:
        fields[column] = set()

    with_comma = 0
    with codecs.open(verbs_file_path, encoding='utf_8', mode='r') as infile:
        first = True
        lines = 0
        for line in iter(infile):
            data = {}
            if first:
                first = False
            else:
                cells = line.split('\t')
                if len(cells) < len(columns):
                    print cells[8], len(cells), len(columns)
                named_cells = zip(columns, cells)
                invalid = False
                for column, cell in named_cells[(22 + len(cells) - len(columns)):]:
                    if column == 'phase' or column == 'scenario':
                        continue
                    cell = cell.strip()
                    if ',' in cell:
                        invalid = True
                    if cell != '' and not invalid:
                        data[column] = []
                        delete = False
                        for item in cell.split('|'):
                            add = []
                            for part in item.split(':'):
                                if part != '':
                                    part = part.strip()
                                    if part == 'b' or part == 'zero':
                                        delete = True
                                        add.append('')
                                    elif part == 'i' or part == 'o' or part == 're' or part == 'neg':
                                        continue
                                    elif part[0] == '\'':
                                        continue
                                    elif part == 'abl' or part == 'adl':
                                        add.append('xp(' + part + ')')
                                    elif '+' in part:
                                        d = part.split('+')
                                        if len(d) == 1:
                                            add.append('np(' + d[0] + ')')
                                        elif len(d) == 2:
                                            prep, case = part.split('+')
                                            if len(prep.split(' ')) > 1:
                                                add.append('comprepnp(' + prep + ')')
                                            else:    
                                                add.append('prepnp(' + prep + ',' + case + ')')
                                        else:
                                            print part
                                    elif part == 'xp{locat}' or part == 'xp{locat)':
                                        add.append('xp(locat)')
                                    elif part == u'że':
                                        add.append(u'cp(' + part + ')')
                                    elif part == 'inf':
                                        add.append('infp(_)')
                                    # elif part == 'nom':
                                    #     add.append('np(str)')
                                    elif part == 'bf' or part == 'sg':
                                        add.append(part)
                                    else:
                                        add.append('np(' + part + ')')
                            data[column].append(':'.join(add))
                        if delete:
                            empty = True
                            for entry in data[column]:
                                if entry != '':
                                    empty = False
                            if empty:
                                del data[column] 
                if invalid:
                    continue
                if len(data) > 0:
                    alter = max([len(cell) for cell in data.values()])
                else:
                    alter = 1
                for role in data:
                    base = data[role]
                    while len(data[role]) < alter:
                        data[role] += base
                for i in range(alter):
                    realizations = {}
                    for key in data:
                        if data[key][i] != '':
                            if data[key][i] not in realizations:
                                realizations[data[key][i]] = [key]
                            else:
                                realizations[data[key][i]].append(key)
                    # background + focus
                    if 'bf' in realizations:
                        base_role = realizations['bf']
                        del realizations['bf']
                        for key in realizations:
                            if 'background' in realizations[key]:
                                realizations[key] += base_role
                            if 'focus' in realizations[key]:
                                realizations[key] += base_role
                    # source + goal
                    if 'sg' in realizations:
                        base_role = realizations['sg']
                        del realizations['sg']
                        for key in realizations:
                            if 'source' in realizations[key]:
                                realizations[key] += base_role
                            if 'goal' in realizations[key]:
                                realizations[key] += base_role
                    frame = {', '.join(l): r for r, l in realizations.items()}
                    if len(frame) > 0:
                        for unit in cells[8].split(','):
                            lu = unit.strip()
                            if lu[0] != u'k':
                                continue
                            lemmas = Lemma.objects.filter(entry=lu.split(' ')[0], old=False)
                            if len(lemmas) != 1:
                                # print lu, '->', len(lemmas), '!=', 1
                                continue
                            lemma = lemmas[0]
                            all_schemas = lemma.frames.all()
                            if len(lu.split(' ')) == 2:
                                unit = LexicalUnit.objects.get(base=lu.split(' ')[0], sense=int(lu.split(' ')[1]))
                                # create empty frame
                                f = SemanticFrame()
                                f.save()
                                f.lexical_units.add(unit)
                                schemas = []
                                for schema in all_schemas:
                                    c = schema.characteristics.get(type=u'ZWROTNOŚĆ')
                                    if c.value.value == u'':
                                        schemas.append(schema)
                                # create unconnected roles
                                complements = {}
                                for roles, argument in frame.items():
                                    complements[argument] = Complement(frame=f)
                                    complements[argument].save()
                                    for r in roles.split(','):
                                        role = r.strip()
                                        print role
                                        dbrole = SemanticRole.objects.get(role=role)
                                        complements[argument].roles.add(dbrole)
                                # connect to EVERY frame where ALL roles can be found
                                compatible = []
                                for schema in schemas:
                                    schema_ok = True
                                    positions = schema.positions.all()
                                    connections = []
                                    for argument in frame.values():
                                        argument_ok = False
                                        for position in positions:
                                            if len(position.arguments.filter(text_rep=argument)) > 0:
                                                argument_ok = True
                                                connections.append((complements[argument], schema, position, position.arguments.filter(text_rep=argument)[0]))
                                            if argument == u'np(nom)': # subj + np(str)
                                                if len(position.arguments.filter(text_rep=u'np(str)')) > 0 and len(position.categories.filter(category=u'subj')) > 0:
                                                    argument_ok = True
                                                    connections.append((complements[argument], schema, position, position.arguments.filter(text_rep=u'np(str)')[0]))
                                            if argument == u'np(acc)': # obj + np(str)
                                                if len(position.arguments.filter(text_rep=u'np(str)')) > 0 and len(position.categories.filter(category=u'obj')) > 0:
                                                    argument_ok = True
                                                    connections.append((complements[argument], schema, position, position.arguments.filter(text_rep=u'np(str)')[0]))
                                        schema_ok &= argument_ok
                                    if schema_ok:
                                        compatible.append(schema)
                                        for c, f, p, a in connections:
                                            x = FramePosition.objects.filter(frame=f, position=p, argument=a)
                                            if len(x) > 0:
                                                c.realizations.add(x[0])
                                            else:
                                                x = FramePosition(frame=f, position=p, argument=a)
                                                x.save()
                                                c.realizations.add(x)
                            elif len(lu.split(' ')) == 3 and lu.split(' ')[1] == u'się':
                                unit = LexicalUnit.objects.get(base=' '.join(lu.split(' ')[0:2]), sense=int(lu.split(' ')[2]))
                                # create empty frame
                                f = SemanticFrame()
                                f.save()
                                f.lexical_units.add(unit)
                                schemas = []
                                for schema in all_schemas:
                                    c = schema.characteristics.get(type=u'ZWROTNOŚĆ')
                                    if c.value.value == u'się':
                                        schemas.append(schema)
                                    else:
                                        for position in schema.positions.all():
                                            if len(position.arguments.filter(text_rep=u'refl')) > 0:
                                                schemas.append(schema)
                                                break
                                # create unconnected roles
                                complements = {}
                                for roles, argument in frame.items():
                                    complements[argument] = Complement(frame=f)
                                    complements[argument].save()
                                    for r in roles.split(','):
                                        role = r.strip()
                                        print role
                                        dbrole = SemanticRole.objects.get(role=role)
                                        complements[argument].roles.add(dbrole)
                                # connect to EVERY frame where ALL roles can be found
                                compatible = []
                                for schema in schemas:
                                    schema_ok = True
                                    positions = schema.positions.all()
                                    connections = []
                                    for argument in frame.values():
                                        argument_ok = False
                                        for position in positions:
                                            if len(position.arguments.filter(text_rep=argument)) > 0:
                                                argument_ok = True
                                                connections.append((complements[argument], schema, position, position.arguments.filter(text_rep=argument)[0]))
                                            if argument == u'np(nom)': # subj + np(str)
                                                if len(position.arguments.filter(text_rep=u'np(str)')) > 0 and len(position.categories.filter(category=u'subj')) > 0:
                                                    argument_ok = True
                                                    connections.append((complements[argument], schema, position, position.arguments.filter(text_rep=u'np(str)')[0]))
                                            if argument == u'np(acc)': # obj + np(str)
                                                if len(position.arguments.filter(text_rep=u'np(str)')) > 0 and len(position.categories.filter(category=u'obj')) > 0:
                                                    argument_ok = True
                                                    connections.append((complements[argument], schema, position, position.arguments.filter(text_rep=u'np(str)')[0]))
                                        schema_ok &= argument_ok
                                    if schema_ok:
                                        compatible.append(schema)
                                        for c, f, p, a in connections:
                                            x = FramePosition.objects.filter(frame=f, position=p, argument=a)
                                            if len(x) > 0:
                                                c.realizations.add(x[0])
                                            else:
                                                x = FramePosition(frame=f, position=p, argument=a)
                                                x.save()
                                                c.realizations.add(x)