-
Notifications
You must be signed in to change notification settings - Fork 4
/
lc-anc.py
237 lines (208 loc) · 7.36 KB
/
lc-anc.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
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
import string,re,sys,os,random
from math import sqrt,log
# adjust minimum sample size here
standard=50
# Returns the keys of dictionary d sorted by their values
def sort_by_value(d):
items=d.items()
backitems=[ [v[1],v[0]] for v in items]
backitems.sort()
return [ backitems[i][1] for i in range(0,len(backitems))]
# NDW for first z words in a sample
def getndwfirstz(z,lemmalist):
ndwfirstztype={}
for lemma in lemmalist[:z]:
ndwfirstztype[lemma]=1
return len(ndwfirstztype.keys())
# NDW expected random z words, 10 trials
def getndwerz(z,lemmalist):
ndwerz=0
for i in range(10):
ndwerztype={}
erzlemmalist=random.sample(lemmalist,z)
for lemma in erzlemmalist:
ndwerztype[lemma]=1
ndwerz+=len(ndwerztype.keys())
return ndwerz/10.0
# NDW expected random sequences of z words, 10 trials
def getndwesz(z,lemmalist):
ndwesz=0
for i in range(10):
ndwesztype={}
startword=random.randint(0,len(lemmalist)-z)
eszlemmalist=lemmalist[startword:startword+z]
for lemma in eszlemmalist:
ndwesztype[lemma]=1
ndwesz+=len(ndwesztype.keys())
return ndwesz/10.0
# MSTTR
def getmsttr(z,lemmalist):
samples=0
msttr=0.0
while len(lemmalist)>=z:
samples+=1
msttrtype={}
for lemma in lemmalist[:z]:
msttrtype[lemma]=1
msttr+=len(msttrtype.keys())/float(z)
lemmalist=lemmalist[z:]
return msttr/samples
def isLetterNumber(character):
if character in string.printable and not character in string.punctuation:
return 1
return 0
def isSentence(line):
for character in line:
if isLetterNumber(character):
return 1
return 0
#reads the bawe_list words
bawe_list = [line.split(',') for line in open("bawe_list.txt")]
# reads information from bnc wordlist
adjdict={}
verbdict={}
noundict={}
worddict={}
wordlistfile=open("anc_all_count.txt","r")
wordlist=wordlistfile.readlines()
wordlistfile.close()
for word in wordlist:
wordinfo=word.strip()
if not wordinfo or "Total words" in wordinfo:
continue
infolist=wordinfo.split()
lemma=infolist[1]
pos=infolist[2]
frequency=int(infolist[3])
worddict[lemma]=worddict.get(lemma,0)+frequency
if pos[0]=="J":
adjdict[lemma]=adjdict.get(lemma,0)+frequency
elif pos[0]=="V":
verbdict[lemma]=verbdict.get(lemma,0)+frequency
elif pos[0]=="N":
noundict[lemma]=noundict.get(lemma,0)+frequency
wordranks=sort_by_value(worddict)
verbranks=sort_by_value(verbdict)
# input file is output of morph
filename=sys.argv[1]
lemfile=open(filename,"r")
lemlines=lemfile.readlines()
lemfile.close()
filename=sys.argv[1].split("/")[-1]
# process input file
wordtypes={}
wordtokens=0
swordtypes={}
swordtokens=0
lextypes={}
lextokens=0
slextypes={}
slextokens=0
verbtypes={}
verbtokens=0
sverbtypes={}
adjtypes={}
adjtokens=0
advtypes={}
advtokens=0
nountypes={}
nountokens=0
sentences=0
lemmaposlist=[]
lemmalist=[]
for lemline in lemlines:
lemline=lemline.strip()
lemline=lemline.lower()
if not isSentence(lemline):
continue
sentences+=1
lemmas=lemline.split()
for lemma in lemmas:
word=lemma.split("_")[0]
pos=lemma.split("_")[-1]
if (not pos in string.punctuation) and pos!="sent" and pos!="sym":
lemmaposlist.append(lemma)
lemmalist.append(word)
wordtokens+=1
wordtypes[word]=1
if (not word in wordranks[-2000:] or word in bawe_list) and pos!="cd":
swordtypes[word]=1
swordtokens+=1
if pos[0]=="n":
lextypes[word]=1
nountypes[word]=1
lextokens+=1
nountokens+=1
if not word in wordranks[-2000:] or word in bawe_list:
slextypes[word]=1
slextokens+=1
elif pos[0]=="j":
lextypes[word]=1
adjtypes[word]=1
lextokens+=1
adjtokens+=1
if not word in wordranks[-2000:] or word in bawe_list:
slextypes[word]=1
slextokens+=1
elif pos[0]=="r" and (adjdict.has_key(word) or (word[-2:]=="ly" and adjdict.has_key(word[:-2]))):
lextypes[word]=1
advtypes[word]=1
lextokens+=1
advtokens+=1
if not word in wordranks[-2000:] or word in bawe_list:
slextypes[word]=1
slextokens+=1
elif pos[0]=="v" and not word in ["be","have"]:
verbtypes[word]=1
verbtokens+=1
lextypes[word]=1
lextokens+=1
if not word in wordranks[-2000:] or word in bawe_list:
sverbtypes[word]=1
slextypes[word]=1
slextokens+=1
# 0. basic statistics
mls=wordtokens/float(sentences)
# 1. lexical density
ld=float(lextokens)/wordtokens
# 2. lexical sophistication
# 2.1 lexical sophistication
ls1=slextokens/float(lextokens)
ls2=len(swordtypes.keys())/float(len(wordtypes.keys()))
# 2.2 verb sophistication
vs1=len(sverbtypes.keys())/float(verbtokens)
vs2=(len(sverbtypes.keys())*len(sverbtypes.keys()))/float(verbtokens)
cvs1=len(sverbtypes.keys())/sqrt(2*verbtokens)
# 3 lexical diversity or variation
# 3.1 NDW, may adjust the values of "standard"
ndw=ndwz=ndwerz=ndwesz=len(wordtypes.keys())
if len(lemmalist)>=standard:
ndwz=getndwfirstz(standard,lemmalist)
ndwerz=getndwerz(standard,lemmalist)
ndwesz=getndwesz(standard,lemmalist)
# 3.2 TTR
msttr=ttr=len(wordtypes.keys())/float(wordtokens)
if len(lemmalist)>=standard:
msttr=getmsttr(standard,lemmalist)
cttr=len(wordtypes.keys())/sqrt(2*wordtokens)
rttr=len(wordtypes.keys())/sqrt(wordtokens)
logttr=log(len(wordtypes.keys()))/log(wordtokens)
uber=(log(wordtokens,10)*log(wordtokens,10))/log(wordtokens/float(len(wordtypes.keys())),10)
# 3.3 verb diversity
vv1=len(verbtypes.keys())/float(verbtokens)
svv1=len(verbtypes.keys())*len(verbtypes.keys())/float(verbtokens)
cvv1=len(verbtypes.keys())/sqrt(2*verbtokens)
# 3.4 lexical diversity
lv=len(lextypes.keys())/float(lextokens)
vv2=len(verbtypes.keys())/float(lextokens)
nv=len(nountypes.keys())/float(nountokens)
adjv=len(adjtypes.keys())/float(lextokens)
advv=len(advtypes.keys())/float(lextokens)
modv=(len(advtypes.keys())+len(adjtypes.keys()))/float(lextokens)
print "filename, sentences, wordtypes, swordtypes, lextypes, slextypes, wordtokens, swordtokens, lextokens, slextokens, ld, ls1, ls2, vs1, vs2, cvs1, ndw, ndwz, ndwerz, ndwesz, ttr, msttr, cttr, rttr, logttr, uber, lv, vv1, svv1, cvv1, vv2, nv, adjv, advv, modv"
output=filename
for measure in [sentences, len(wordtypes.keys()), len(swordtypes.keys()), len(lextypes.keys()), len(slextypes.keys()), wordtokens, swordtokens, lextokens, slextokens, ld, ls1, ls2, vs1, vs2, cvs1, ndw, ndwz, ndwerz, ndwesz, ttr, msttr, cttr, rttr, logttr, uber, lv, vv1, svv1, cvv1, vv2, nv, adjv, advv, modv]:
if type(measure)==type(0.0):
measure="%.2f" % measure
output+=", "+str(measure)
print output