-
Notifications
You must be signed in to change notification settings - Fork 5
/
scratchpad.py
316 lines (207 loc) · 8.32 KB
/
scratchpad.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from batchalign import *
import json
from glob import glob
from pathlib import Path
from rich.console import Console
import copy
import os
import logging as L
LOG_FORMAT = '[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s'
L.basicConfig(format=LOG_FORMAT, level=L.ERROR)
L.getLogger("stanza").setLevel(L.ERROR)
L.getLogger('batchalign').setLevel(L.DEBUG)
########
# from batchalign.models.utterance import infer
# engine = infer.BertUtteranceModel("talkbank/CHATUtterance-zh_CN")
# engine("我 现在 想 听 你说 一些 你 自己 经 历 过 的 故 事 好不好 然后 呢 我们 会 一起 讨 论 有 六 种 不同 的 情 景 然后 在 每 一个 情 景 中 都 需要 你 去 讲 一个 关 于 你 自己 的 一个 故 事 小 故 事")
# doc = Document.new(media_path="/Users/houjun/Downloads/trial.mp3", lang="zho")
# print(doc)
# pipe = BatchalignPipeline.new("asr", lang="zho", num_speakers=2, engine="rev")
# res = pipe(doc)
# # with open("schema.json", 'w') as df:
# # json.dump(Document.model_json_schema(), df, indent=4)
# ########### The Batchalign Core Test Harness ###########
from batchalign.formats.chat.parser import chat_parse_utterance
# print(str(CHATFile(doc=ut)))
# doc = CHATFile(path="../talkbank-alignment/input/barry.cha").doc
# doc[3][0]
# て
# print(str(CHATFile(doc=res)))
# # j.coref_chains) for j in i.words] for i in coref_chains]
# # dir(coref_chains[0][0][1][0])
# # coref_chains[0][0][1][0].chain.index
# # coref_chains[0][0][1][0].is_start
# # coref_chains[0][0][1][0].is_end
# # coref_chains[0].word
# # ng = NgramRetraceEngine()
# # # disf = DisfluencyReplacementEngine()
# doc = Document.new("I am a very large chicken indeed.", lang="eng")
# forms, delim = chat_parse_utterance("I am a very large chicken indeed.", None, None, None, None)
# utterance = Utterance(content=forms, delim=delim)
# gold = Document(content=[utterance], langs=["eng"])
# pipeline = BatchalignPipeline(EvaluationEngine())
# result = pipeline(doc, gold=gold)
# # pipeline = BatchalignPipeline.new("morphosyntax")
# # result2 = pipeline(gold)
# # print(str(CHATFile(doc=result2)))
# result
# print(result["diff"])
# # # # doc[0].content[4].text = "maman,"
# # # # doc[0].content[5].text = "maman,"
# # pipe = BatchalignPipeline(ng, disf)
# # tmp = pipe(doc)
# # tmp
# # tmp[0].content
# # import stanza
# # nlp = stanza.Pipeline("en", packages="tokenize,pos,constituency")
# # tree = nlp("I am dead, and he is also dead, but Josh isn't dead because \"only the brave die young and he knows the best is yet to come\".").sentences[0].constituency
# # i am dead and he is also dead but Josh isn't dead because only the brave die young and he knows the best is yet to come.
# # parse_tree(tree)
# # def rollout_to_leaf(tree):
# # """Extract the leaf nodes from a subtree via dfs"""
# # try:
# # children = tree.children
# # except AttributeError:
# # breakpoint()
# # leafs = []
# # for c in children:
# # if c.is_leaf():
# #さっき[* s] 食べた事ある. leafs.append(c.label)
# # else:
# # leafs += rollout_to_leaf(c)
# # return leafs
# from batchalign.models import BertUtteranceModel
# from batchalign.pipelines import BatchalignPipeline
# pipe = BatchalignPipeline.new("morphosyntax", "fra")
# res = pipe(ut)
# print(str(CHATFile(doc=res)))
# tmp[-1].content
# tmp[-1]
# tmp[6]
# tmp
# tmp1 = sue(tmp)
# # tmp1
# # tmp
# # control x . oh yeah nine x . so we're on this side .
# pipe = BatchalignPipeline.new("asr", num_speakers=2, asr="whisper", lang="spa")
# uts = Document.new(media_path="../talkbank-alignment/test_harness/input/1576.mp3")
# # pipe(uts)
# # tmp = pipe("../talkbank-alignment/test_harness/input/crop.mp3")
# # tmp[8]
# # CHATFile(doc=tmp).write("../talkbank-alignment/test_harness/input/crop.cha")
# ut = BertUtteranceModel("../talkbank-alignment/train/models/utterance/mandarin/utterance_mandarin")
# ut("早上好 中国 我在吃一个 冰淇淋 这个 冰淇淋 很 好吃 但是 我不知道")
# text = "ice ice cream ice cream"
# function = "morphosyntax"
# lang = "jpn"
# num_speakers = 1
# ut = "Swimming is really fun."
# forms, delim = chat_parse_utterance(ut, None, None, None, None)
# # forms
# utterance = Utterance(content=forms, delim=delim, text=ut)
sec = "⁎あったったったった:⁎@wp [: あった] か ."
forms, delim = chat_parse_utterance(sec, None, None, None, None)
utterance = Utterance(content=forms, delim=delim, text=sec)
# # # # # =======
# ut = Document(content=[utterance], langs=["jpn"])
# pipeline = BatchalignPipeline.new("morphosyntax", lang="jpn")
# res = pipeline(ut, retokenize=True)
# print(str(CHATFile(doc=res)))
# >>>>>>> theirs
ut = Document(content=[utterance], langs=["jpn"])
pipeline = BatchalignPipeline.new("morphosyntax", lang="jpn")
res = pipeline(ut, retokenize=True)
# >>>>>>> Stashed changes
# # print(str(CHA
print(str(CHATFile(doc=res)))
# ut = Document(content=[utterance], langs=[lang])
# pipeline = BatchalignPipeline.new("morphosyntax", lang=lang)
# res = pipeline(ut, retokenize=True)
# print(str(CHATFile(doc=res)))
# print(u"\u202bwhat up with that?")
# print("אויתאויונסתהאויסו".encode().decode("").encode().decode("utf-8"))
########### The Batchalign Individual Engine Harness ###########
# text = "We should be friends! Yes we should."
# # text = "ice ice cream ice ice cream ice ice cream"
# # ice ice cream ice cream
# # ice [/] <ice cream> [/] ice cream
# # ice cream ice cream ice cream ice ice cream cream
# lang = "eng"
# forms, delim = chat_parse_utterance(text, None, None, None, None)
# utterance = Utterance(content=forms, delim=delim)
# ut = Document(content=[utterance], langs=[lang])
# doc = Document.new(text, lang=lang)
# retrace = StanzaEngine()
# pipe = BatchalignPipeline(retrace)
# doc = pipe(doc)
# doc
# # # doc[0].content
# print(str(CHATFile(doc=doc)))
########### The Batchalign String Test Harness ###########
# from batchalign.formats.chat.parser import chat_parse_utterance
# file = "/Users/houjun/Documents/Projects/talkbank-alignment/test_harness/input/Untitled.wav"
# function = "asr"
# lang = "spa"
# num_speakers = 1
# ut = Document.new(media_path=file, lang=lang)
# pipeline = BatchalignPipeline.new(function, lang=lang, num_speakers=num_speakers)
# doc = pipeline(ut)
# doc.content
# # doc[0][-1]
# # doc[0][-2].model_dump()
# # doc[0].content[-2]
# print(str(CHATFile(doc=doc)))
########### The Batchalign Parser Harness ###########
# from batchalign.formats.chat import CHATFile
# in_dir = "../talkbank-alignment/test_harness/input"
# in_files = glob(str(Path(in_dir)/"*.cha"))
# parent, _, files = zip(*list(os.walk(in_dir)))
# in_files = [os.path.join(a,i)
# for a,b in zip(parent, files)
# for i in b
# if ".cha" in i]
# for file in in_files:
# try:
# f = CHATFile(path=os.path.abspath(file))
# except Exception as e:
# print(file)
# raise e
# f.doc[12]
# d = f.doc
# wer = EvaluationEngine()
# wer(d_alt, gold=d)
# dp time
# f.doc[1][1]
# f.doc.media
# doc = f.doc
# doc[-382][1]
########### The Batchalign CLI Harness ###########
# from batchalign.cli.dispatch import _dispatch
# in_dir = "../talkbank-alignment/test_harness/input/"
# out_dir = "../talkbank-alignment/test_harness/output/"
# in_format = "cha"
# function = "morphotag"
# lang = "hrv"
# num_speakers = 1
# class Context:
# obj = {"verbose": 3}
# def loader(file):
# return CHATFile(path=os.path.abspath(file)).doc
# # return file
# def writer(doc, output):
# CHATFile(doc=doc).write(output)
# # CHATFile(doc=doc).write(output
# # .replace(".wav", ".cha")
# # .replace(".mp4", ".cha")
# # .replace(".mp3", ".cha"))
# _dispatch(function, lang, num_speakers, [in_format], Context(),
# in_dir, out_dir,
# loader, writer, Console())
########## The Batchalign CHAT Test Tarness ##########
# from batchalign.formats.chat.parser import chat_parse_utterance
# from batchalign.formats.chat.lexer import lex
# from batchalign.formats.chat.utils import annotation_clean
# main = "+\" and then some Indians came and took something away from him and he said +\"/. [+ dia] •884995_892418•"
# mor = None
# gra = None
# chat_parse_utterance(main, mor, gra, None, None)