-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfdx2aub.py
291 lines (221 loc) · 14.4 KB
/
fdx2aub.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
import xml.etree.ElementTree as ET
# tree = ET.parse(r'D:\pythonEnvs\stable_LLM\Big_Fish.fdx')
# root = tree.getroot()
# for child in root:
# print(child.tag, child.attrib)
import sys
import sqlite3
import pandas as pd
from Aubrushli import Aubrushli
import os
class fdx2aub:
def createbigcsv(self, csvpath, screenplayid):
current_dir = os.path.dirname(os.path.abspath(__file__))
connection = sqlite3.connect(current_dir + "/aubrushli.db")
connection.execute('PRAGMA synchronous = NORMAL')
connection.execute('PRAGMA journal_mode = WAL')
connection.execute('PRAGMA cache_size = -8192')
connection.commit()
sql = "SELECT linenumber, line, linetype, scenenum, length, pagenum, charsinscene FROM fdxlines where screenplayid = ? order by linenumber"
castcsvdf = pd.read_sql(sql, connection, params=(screenplayid,))
#print(castcsvdf)
#connection.commit()
castcsvdf.to_csv(csvpath, index=False)
def writelinetodb(self, connection, screenplayID, line, linenum, linetype, scenenum, scenetypenum, officialnum='', length='', pagenum='', chars=0 ):
#print(connection, screenplayID, line, linenum, linetype, scenenum, scenetypenum, officialnum, length, pagenum, chars )
cursor = connection.cursor()
cursor.execute('''INSERT INTO fdxlines (screenplayID, line, linenumber, linetype, scenenum, scenetypenum, officialnum, length, pagenum, charsinscene) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (screenplayID, line, linenum, linetype, scenenum, scenetypenum, officialnum, length, pagenum, chars))
connection.commit()
# # last_id = cursor.lastrowid
def create_cast_list(self,castlistpath, screenplayid):
#print(screenplayid)
current_dir = os.path.dirname(os.path.abspath(__file__))
connection = sqlite3.connect(current_dir + "/aubrushli.db")
connection.execute('PRAGMA synchronous = NORMAL')
connection.execute('PRAGMA journal_mode = WAL')
connection.execute('PRAGMA cache_size = -8192')
connection.commit()
sql = "select line as character, count(line) as lines from fdxlines where linetype = 'character' and screenplayid = ? group by line order by lines desc"
castcsvdf = pd.read_sql(sql, connection, params=(screenplayid,))
#print(castcsvdf)
#connection.commit()
castcsvdf.to_csv(castlistpath, index=False)
def fdx2dbaub(self, input_file_name, shotsdef, outpath):
current_dir = os.path.dirname(os.path.abspath(__file__))
connection = sqlite3.connect(current_dir + "/aubrushli.db")
connection.execute('PRAGMA synchronous = NORMAL')
connection.execute('PRAGMA journal_mode = WAL')
connection.execute('PRAGMA cache_size = -8192')
connection.commit()
#output_file_name = r"D:\pythonEnvs\stable_LLM\test.txt"
#input_file_name = r'G:\Aubrushli_images_current\Big_Fish.fdx'
#shotsdef = r'G:\Aubrushli_images_current\shot_vals.csv'
#outpath = 'G:/Aubrushli_images_current/fdxshotlists/'
#file = open(output_file_name,"w")
cursor = connection.cursor()
cursor.execute('''INSERT INTO screenplay_document (Title,'Path') VALUES (?, ?)''', ('Big_Fish', input_file_name ))
connection.commit()
screenplayID = cursor.lastrowid
#print(screenplayID)
tree = ET.parse(input_file_name)
# line, linenumber, linetype, linelength, numwords, firstwordpos, firstword, lastwordpos, lastword
# you do not need all the above as already parsed by definition so just line, linenumber and linetype
# need to get line number which would be if its action, character, dialogue, para, scene, note not empty lines
# so you can create if you ever convert into pdf for example.
linenum =0
scenenum=0
#typenum=0
scenetypenum=0
charsinscene=0
root = tree.getroot()
for paragraph in root.iterfind('Content/Paragraph'):
#print(paragraph)
if paragraph.attrib['Type'] == "Action":
#print("action")
if hasattr(paragraph.find('Text'), 'text'):
for index, text_element in enumerate(paragraph.findall('Text')):
if (text_element.text is not None):
linenum = linenum + 1
scenetypenum= scenetypenum + 1
self.writelinetodb(connection, screenplayID, text_element.text , linenum, 'action', scenenum, scenetypenum )
#print(text_element.text)
elif paragraph.attrib['Type'] == "General":
if hasattr(paragraph.find('Text'), 'text'):
for index, text_element in enumerate(paragraph.findall('Text')):
if (text_element.text is not None):
linenum = linenum + 1
scenetypenum= scenetypenum + 1
self.writelinetodb(connection, screenplayID, text_element.text, linenum, 'label', scenenum, scenetypenum )
elif paragraph.attrib['Type'] == "Transition":
if hasattr(paragraph.find('Text'), 'text'):
for index, text_element in enumerate(paragraph.findall('Text')):
if (text_element.text is not None):
linenum = linenum + 1
#scenenum = scenenum + 1
#typenum = typenum +1
scenetypenum= scenetypenum =0
self.writelinetodb(connection, screenplayID, text_element.text.upper(), linenum, 'transition', scenenum, scenetypenum )
elif paragraph.attrib['Type'] == "Character":
if hasattr(paragraph.find('Text'), 'text'):
for index, text_element in enumerate(paragraph.findall('Text')):
if (text_element.text is not None):
linetype =''
if "(" in text_element.text or ")" in text_element.text or "cont’d" in text_element.text:
#charsinscene= charsinscene +1
linetype = 'parenthetical'
else:
#print(text_element.text, linenum)
charsinscene= charsinscene +1
linetype = 'character'
linenum = linenum + 1
# typenum = typenum +1
scenetypenum= scenetypenum + 1
self.writelinetodb(connection, screenplayID, text_element.text.upper().strip(), linenum, linetype, scenenum, scenetypenum, chars=charsinscene )
elif paragraph.attrib['Type'] == "Parenthetical":
if hasattr(paragraph.find('Text'), 'text'):
for index, text_element in enumerate(paragraph.findall('Text')):
if (text_element.text is not None):
linenum = linenum + 1
# typenum = typenum +1
scenetypenum= scenetypenum + 1
self.writelinetodb(connection, screenplayID, text_element.text.upper(), linenum, 'parenthetical', scenenum, scenetypenum )
elif paragraph.attrib['Type'] == "Dialogue":
if hasattr(paragraph.find('Text'), 'text'):
for index, text_element in enumerate(paragraph.findall('Text')):
if (text_element.text is not None):
linenum = linenum + 1
# typenum = typenum +1
scenetypenum= scenetypenum + 1
self.writelinetodb(connection, screenplayID, text_element.text, linenum, 'dialogue', scenenum, scenetypenum )
elif paragraph.attrib['Type'] == "Scene Heading":
off= paragraph.get('Number')
scene_properties = paragraph.find('SceneProperties').attrib
# Access the values of the SceneProperties attributes
length = scene_properties['Length']
page = scene_properties['Page']
#print(child.attrib)
if hasattr(paragraph.find('Text'), 'text'):
thetext = ''
for index, text_element in enumerate(paragraph.findall('Text')):
if (text_element.text is not None):
thetext = thetext + ' ' + text_element.text.upper()
charsinscene=0
linenum = linenum + 1
# typenum = typenum +1
scenenum = scenenum + 1
scenetypenum=0
self.writelinetodb(connection, screenplayID, thetext , linenum, 'scene', scenenum, scenetypenum, off, length, page, chars=charsinscene)
#*************************steph need a characters in scene update here to distinct characters*************************************
cursor = connection.cursor()
sql = "CREATE TEMP TABLE distchars as select distinct line, scenenum from fdxlines where linetype = 'character' group by scenenum, line order by scenenum"
sql1 ="create temp table finchars as select count(line) thechars, scenenum from distchars group by scenenum"
sql2 = "update fdxlines set charsinscene = f.thechars from (select thechars, scenenum from finchars) as f where f.scenenum = fdxlines.scenenum"
sql3 = "INSERT INTO characters (screenplayID, charactername) select distinct screenplayid, trim(line) from fdxlines where linetype = 'character'"
cursor.execute(sql)
cursor.execute(sql1)
cursor.execute(sql2)
cursor.execute(sql3)
connection.commit()
cursor = connection.cursor()
sql4 = "SELECT characterID, screenplayID, charactername FROM characters where screenplayid =? "
params = (screenplayID,) # create a tuple of parameters
cursor.execute(sql4, params) # pass the tuple to cursor.execute()
for row in cursor:
print(row[1], row[2])
cursor1 = connection.cursor()
sql5= "INSERT INTO characteractions (characterID, actionID, charactername, theline) select ? , fdxlineID , ? , line from fdxlines where screenplayid =? and linetype = 'action' and (line like '% ' || ? || '%' or line like ? || '%') "
params1 = (row[0], row[2], screenplayID, row[2], row[2])
cursor1.execute(sql5, params1)
#for newrow in cursor1:
# print(newrow)
#b= row
cursor = connection.cursor()
sql6= "update fdxlines set actchars = c.actchars from (select count(actionid) as actchars, actionid FROM characteractions group by actionid, theline) as c where c.actionid = fdxlines.fdxlineid"
cursor.execute(sql6)
connection.commit()
# need to loop round each scene in db for shots call aubrushli.py
# need a complete DF of the fdxlines table in the same format as fountain
# need to put an id in fdxlines to match to the screenplay you need.
sql = 'SELECT line, linetype, linenumber, scenenum, case when actchars is null then charsinscene else actchars end as charsinscene from fdxlines where screenplayID=?'
df = pd.read_sql(sql, connection, params=(screenplayID,))
df = df.rename(columns={'line': 'element_text', 'linetype': 'element_type', 'linenumber': 'original_line'})
#print(df.columns)
# need a shot vals df - taken from csv
shot_vals_df = pd.read_csv(shotsdef)
# need an actions in the current scene df in the same format as fountain
scenes =[]
cursor = connection.cursor()
sql = 'SELECT distinct scenenum from fdxlines where screenplayID=?'
params = (screenplayID,) # create a tuple of parameters
cursor.execute(sql, params) # pass the tuple to cursor.execute()
scenes = cursor.fetchall() # fetch the results
connection.commit()
for scene in scenes:
scenedf = df[(df['scenenum'] == scene[0])]
scenedf = scenedf.reset_index(drop=True)
currscenenamedf = scenedf[(scenedf['element_type']== 'scene')]
currscenenamedf = currscenenamedf.reset_index(drop=True)
#print(currscenenamedf)
if len(currscenenamedf) >0:
curscenename = currscenenamedf['element_text'][0]
#print(curscenename)
charsinscene = scenedf['charsinscene'][0]
currsceneno = scenedf['scenenum'][0]
actionsdf = scenedf[(scenedf['element_type'] == 'action')]
# get the minimum value in column A
startline = scenedf['original_line'].min()
# get the maximum value in column A
endline = scenedf['original_line'].max()
combined=[]
# #print(scene[0])
# print(scenedf)
# print(actionsdf)
# print(startline)
# print(endline)
# print(charsinscene)
aubshot = Aubrushli()
aubshot.createshotlist( charsinscene, outpath, actionsdf, df, shot_vals_df, 2, startline, endline, currsceneno, curscenename, combined, 'Big_FDX_Fish')
#createshotlist( numcharacters, outpath, action_df, df, shot_vals_df, sending_format=1, startline=f_start_scene_line, endline=f_next_scene_line, currscene=currscneno, currscenename=f_scene_name, combined=combine_chars):
# also need a start and end line for the scene
# use the sending format param to allow extra
connection.close()