-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-week01.py
executable file
·180 lines (174 loc) · 5.14 KB
/
test-week01.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
'''
tid2tm2begin2text = {}
tid2begin2text = {}
fr = open('allcaptions-2021-09-24.csv','rb')
fr.readline()
for line in fr:
arr = line.strip('\r\n').split('\t')
if not len(arr) == 6: continue
tid,tm,begin,text = arr[0],arr[1],arr[3],arr[5]
if not tid in tid2tm2begin2text:
tid2tm2begin2text[tid] = {}
if not tm in tid2tm2begin2text[tid]:
tid2tm2begin2text[tid][tm] = {}
if not begin in tid2tm2begin2text[tid][tm]:
tid2tm2begin2text[tid][tm][begin] = []
tid2tm2begin2text[tid][tm][begin].append(text)
if not tid in tid2begin2text:
tid2begin2text[tid] = {}
tid2begin2text[tid][begin] = text
fr.close()
fw = open('corpus_original.txt','w')
for tid,tm2begin2text in sorted(tid2tm2begin2text.items(),key=lambda x:x[0]):
s = ' '
for tm,begin2text in sorted(tm2begin2text.items(),key=lambda x:x[0]):
for begin,texts in sorted(begin2text.items(),key=lambda x:x[0]):
for text in texts:
s += ' '+text
s += ' '
s = s.replace('\t',' ').replace('\r',' ')
while ' ' in s:
s = s.replace(' ',' ')
fw.write(tid+'\t'+s[1:-1]+'\n')
fw.close()
fw = open('corpus_corrected.txt','w')
for tid,begin2text in sorted(tid2begin2text.items(),key=lambda x:x[0]):
s = ' '
for begin,text in sorted(begin2text.items(),key=lambda x:x[0]):
s += ' '+text
s += ' '
s = s.replace('\t',' ').replace('\r',' ')
while ' ' in s:
s = s.replace(' ',' ')
fw.write(tid+'\t'+s[1:-1]+'\n')
fw.close()
'''
'''
import spacy
from spacy.pipeline import Sentencizer
nlp = spacy.load('en_core_web_sm')
sentencizer = nlp.add_pipe("sentencizer")
sentencizer = Sentencizer()
fw = open('nounchunks.txt','w')
fr = open('corpus_corrected.txt','r')
for line in fr:
arr = line.strip('\r\n').split('\t')
tid = arr[0]
text = arr[1]
doc = nlp(text)
n = len(list(doc.sents))
fw.write(tid+'\t'+str(n)+'\n')
sid = 0
for text in doc.sents:
_text_ = str(text)
sid += 1
fw.write('#s'+str(sid)+'\t'+_text_+'\n')
sent = nlp(_text_)
s = ''
for chunk in sent.noun_chunks:
s += ';'+chunk.text
if not s == '':
fw.write('#c'+str(sid)+'\t'+s[1:]+'\n')
fr.close()
fw.close()
'''
'''
word2count = {}
fr = open('corpus_corrected.txt','r')
for line in fr:
arr = line.strip('\r\n').split('\t')
text = ' '+arr[1]+' '
text = text.replace('.',' . ').replace(',',' , ').replace(';',' ; ').replace("'"," '")
text = text.replace('?',' ? ').replace('!',' ! ').replace('(',' ( ').replace(')',' ) ')
while ' ' in text:
text = text.replace(' ',' ')
text = text[1:-1].lower()
for word in text.split(' '):
if not word in word2count:
word2count[word] = 0
word2count[word] += 1
fr.close()
fw = open('vocabulary.txt','w')
for word,count in sorted(word2count.items(),key=lambda x:-x[1]):
fw.write(word+'\t'+str(count)+'\n')
fw.close()
'''
'''
import spacy
nlp = spacy.load('en_core_web_sm')
stopwords = nlp.Defaults.stop_words
words,counts = [],[]
wordsStop,countsStop = [],[]
fw = open('vocabulary-nonstop.txt','w')
fr = open('vocabulary.txt','r')
for line in fr:
arr = line.strip('\r\n').split('\t')
word,count = arr[0],int(arr[1])
words.append(word)
counts.append(count)
if word in stopwords or len(word) < 2:
wordsStop.append(word)
countsStop.append(count)
else:
fw.write(word+'\t'+str(count)+'\n')
fr.close()
fw.close()
n = len(words)
m = sum(counts)
nStop = len(wordsStop)
mStop = sum(countsStop)
print(n,nStop,100.*nStop/n)
print(m,mStop,100.*mStop/m)
'''
'''
nums = []
fr = open('nounchunks.txt','r')
for line in fr:
if not line.startswith('#'):
arr = line.strip('\r\n').split('\t')
nums.append(int(arr[1]))
fr.close()
fw = open('numsentences.txt','w')
for num in sorted(nums,key=lambda x:-x):
fw.write(str(num)+'\n')
fw.close()
'''
'''
import spacy
nlp = spacy.load('en_core_web_sm')
stopwords = nlp.Defaults.stop_words
chunk2count = {}
fr = open('nounchunks.txt','r')
for line in fr:
if line.startswith('#c'):
arr = line.strip('\r\n').split('\t')
chunks = arr[1]
for chunk in chunks.split(';'):
words = chunk.split(' ')
if len(words) > 0:
while words[0].lower() in stopwords:
words.pop(0)
if len(words) == 0: break
if len(words) > 0:
while words[-1].lower() in stopwords:
words.pop(-1)
if len(words) == 0: break
if len(words) > 0:
_chunk_ = ''
for word in words:
_chunk_ += ' '+word
_chunk_ = _chunk_[1:]
if not _chunk_ in chunk2count:
chunk2count[_chunk_] = 0
chunk2count[_chunk_] += 1
fr.close()
fw1 = open('word2count.txt','w')
fw2 = open('phrase2count.txt','w')
for chunk,count in sorted(chunk2count.items(),key=lambda x:-x[1]):
if ' ' in chunk:
fw2.write(chunk+'\t'+str(count)+'\n')
else:
fw1.write(chunk+'\t'+str(count)+'\n')
fw2.close()
fw1.close()
'''