-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.py
134 lines (106 loc) · 4.09 KB
/
tag.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
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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# Copyright (c) 2017 Adler Neves <[email protected]>
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import json
from utils import getListItemOr, sortedListBinarySearchNoRaise
def deepTupleConversion(lst):
if isinstance(lst, list):
for i, l in enumerate(lst):
if isinstance(l, list) or isinstance(l, dict):
lst[i] = deepTupleConversion(l)
return tuple(lst)
elif isinstance(lst, dict):
for k, v in lst.items():
if isinstance(v, list) or isinstance(v, dict):
lst[k] = deepTupleConversion(v)
return lst
with open('unitexable_test/corpus.txt.untagged.txt') as f:
corpus = [[wrd.strip().lower() for wrd in snt.split(' ') if len(wrd)>0] for snt in f.read().strip().splitlines() if len(snt)>0]
print('readed corpus')
with open('unitexable_test/traindata.json') as f:
taggerData = json.loads(f.read())
deepTupleConversion(taggerData)
tagset = taggerData['tagset']
tokset = taggerData['tokset']
print('loaded tagger data')
tokCorpus = list()
for i,snt in enumerate(corpus):
tokCorpus.append([sortedListBinarySearchNoRaise(tokset, wrd) for wrd in snt])
if i%25000 == 0:
print('%d of %d'%(i,len(corpus)))
tokCorpus = deepTupleConversion(tokCorpus)
print('loaded corpus - tagging may begin')
dictEmptyTriples = dict(taggerData['keypairs']['3empty'])
dictTriples = dict(taggerData['keypairs']['3middle'])
dictTuplesL = dict(taggerData['keypairs']['2left'])
dictTuplesR = dict(taggerData['keypairs']['2right'])
dictFallback = dict(taggerData['keypairs']['1fallback'])
def getTagFallback(triple):
return dictFallback.get[triple[1:2]]
def getTagTuplesL(triple):
return dictTuplesL[triple[0:2]]
def getTagTuplesR(triple):
return dictTuplesR[triple[1:3]]
def getTagTriples(triple):
return dictTuplesR[triple[0:3]]
def getTagTriplesEmpty(triple):
return dictEmptyTriples[triple[0:3:2]]
taggingStrategy = [
getTagTriples,
getTagTuplesR,
getTagTuplesL,
getTagTriplesEmpty,
getTagFallback
]
def getTagWithStategy(triple):
for stategy in taggingStrategy:
try:
return stategy(triple)
except:
pass
return -1
def processSentence(tokSentence):
l = list()
for i in range(len(tokSentence)):
triple = (getListItemOr(tokSentence, i-1, None), tokSentence[i], getListItemOr(tokSentence, i+1, None))
tag = getTagWithStategy(triple)
l.append((tokSentence[i], tag))
return l
tokTagged = list()
for i,snt in enumerate(tokCorpus):
tokTagged.append(processSentence(snt))
if i%25000 == 0:
print('%d of %d'%(i,len(tokCorpus)))
print('corpus tagged - preparing for serialization')
taggedCorpus = list()
for words in tokTagged:
sentence = list()
for word in words:
sentence.append(getListItemOr(tagset, word[1], "???"))
taggedCorpus.append(sentence)
stg = '\n\n'.join(['\n'.join(snt) for snt in taggedCorpus])
print('serialized')
with open('unitexable_test/corpus.txt.tagged.txt','w') as f:
f.write(stg)
print('done')