-
Notifications
You must be signed in to change notification settings - Fork 72
/
2_getMentions.fsx
209 lines (182 loc) · 7.39 KB
/
2_getMentions.fsx
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
#load "packages/FsLab/FsLab.fsx"
#load "parseScripts.fs"
open FSharp.Data
open System
open System.IO
open StarWars.ParseScripts
open System.Text.RegularExpressions
/// Extract interactions between characters from the individual scripts
let characters =
let stdCharacters = File.ReadAllLines(__SOURCE_DIRECTORY__ + "/data/characters.csv")
let allNames =
[| 0 .. scriptUrls.Length-1 |]
|> Array.collect
(fun ep ->
Seq.append aliasesForEpisodes.[ep].Keys aliasesForEpisodes.[ep].Values
|> Array.ofSeq)
stdCharacters
|> Array.append allNames
|> Array.map (fun s -> s.ToLower())
|> Array.distinct
// Some names occur also as a part of other words - check for that as well
// problematic names: Han, Sola
let containsName (scene:string) (name:string) =
scene.Contains(name) && (
Regex.IsMatch(scene,"[^a-z]+" + name + "[^a-z]+") ||
Regex.IsMatch(scene,"^" + name + "[^a-z]+") ||
Regex.IsMatch(scene,"[^a-z]+" + name + "$"))
/// Create JSON network
let getJsonNetwork nodes links =
let jsonNodes =
nodes
|> Seq.map (fun (name, count) ->
JsonValue.Record
[| "name", JsonValue.String name;
"value", JsonValue.Number (decimal (count+1))
"colour", JsonValue.String (getCharacterColour name)|] )
|> Array.ofSeq
|> JsonValue.Array
// translate links from names to idxs
let nodeIdxs = Seq.mapi (fun i (name, count) -> name, i) nodes |> dict
let jsonLinks =
links
|> Seq.map (fun ((n1, n2), value) ->
JsonValue.Record [| "source", JsonValue.Number (decimal nodeIdxs.[n1]);
"target", JsonValue.Number (decimal nodeIdxs.[n2]);
"value", JsonValue.Number (decimal value)|])
|> Array.ofSeq
|> JsonValue.Array
(JsonValue.Record [| "nodes", jsonNodes ; "links", jsonLinks |]).ToString()
let getMentionsNetwork includeRobots countThreshold episodeIdx =
let episode, url = scriptUrls.[episodeIdx]
let script = getScriptElement url
let scriptParts = script.Elements()
let mainScript =
scriptParts
|> Seq.map (fun element -> element.ToString())
|> Seq.toArray
// Now every element of the list is a single scene
let scenes =
splitByScene mainScript [] |> List.rev
let interactions =
scenes
|> List.map (fun scene ->
let lscene =
scene |> Array.map (fun s -> s.ToLower()) // some names contain lower-case characters
characters
|> Array.filter (characterCheck episodeIdx)
|> Array.map (fun name ->
lscene
|> Array.map (fun contents -> if containsName contents name then Some name else None )
|> Array.choose id)
|> Array.concat
|> Array.choose (fun name ->
let newName = mapName episodeIdx (name.ToUpper())
if includeRobots then Some newName
elif
newName = "R2-D2"
|| newName = "C-3PO"
|| newName = "BB-8"
then None else Some newName)
|> Array.distinct )
// |> List.mapi (fun i cs ->
// if (cs |> Array.contains "SNOKE") && (cs |> Array.contains "CHEWBACCA") then printfn "%d" i
// cs)
|> List.filter (Array.isEmpty >> not)
// Create weighted network
let nodes =
interactions
|> Seq.collect id
|> Seq.countBy id
|> Seq.filter (fun (name, count) -> count >= countThreshold)
let nodeLookup = nodes |> Seq.map fst |> set
let links =
interactions
|> List.collect (fun sceneNames ->
[ for i in 0..sceneNames.Length - 1 do
for j in i+1..sceneNames.Length - 1 do
let n1 = sceneNames.[i]
let n2 = sceneNames.[j]
if nodeLookup.Contains(n1) && nodeLookup.Contains(n2) then
yield min n1 n2, max n1 n2 ])
|> Seq.countBy id
|> Array.ofSeq
nodes, links
let generateMentionsNetwork episodeIdx =
let nodes, links = getMentionsNetwork true 0 episodeIdx
File.WriteAllText(__SOURCE_DIRECTORY__ + "/networks/starwars-episode-" + string (episodeIdx + 1) + "-mentions.json",
getJsonNetwork nodes links)
for i in 0..5 do generateMentionsNetwork i
generateMentionsNetwork 6
// =====================================================================
// Generate global network
let includeRobots = true
let countThreshold = 0
let linkThreshold = 0
let nodes, links =
[0..6] |> List.map (getMentionsNetwork includeRobots 0) |> List.unzip
let summarizeEpisodes data =
data
|> Seq.collect id
|> Seq.groupBy fst
|> Seq.map (fun (name, episodeCounts) -> name, episodeCounts |> Seq.sumBy snd)
let allNodes =
summarizeEpisodes nodes
|> Seq.filter (fun (name, count) -> count >= countThreshold)
let nodeLookup = allNodes |> Seq.map fst |> set
let allLinks =
summarizeEpisodes links
|> Seq.filter (fun ((n1, n2), _) -> nodeLookup.Contains(n1) && nodeLookup.Contains(n2))
|> Seq.filter (fun ((n1, n2), count) -> count > linkThreshold)
File.WriteAllText(__SOURCE_DIRECTORY__ + "/networks/starwars-full-mentions.json",
getJsonNetwork allNodes allLinks)
// ===============================================================
// Get timelines of mentions for each character
/// Get indices of scenes where individual characters appear (where they are mentioned).
let getSceneAppearances episodeIdx =
let episode, url = scriptUrls.[episodeIdx]
let script = getScriptElement url
let scriptParts = script.Elements()
let mainScript =
scriptParts
|> Seq.map (fun element -> element.ToString())
|> Seq.toArray
// Now every element of the list is a single scene
let scenes =
splitByScene mainScript [] |> List.rev
let totalScenes = scenes.Length
scenes
|> List.mapi (fun sceneIdx scene ->
let lscene =
scene |> Array.map (fun s -> s.ToLower()) // some names contain lower-case characters
characters
|> Array.filter (characterCheck episodeIdx)
|> Array.map (fun name ->
lscene
|> Array.map (fun contents -> if containsName contents name then Some name else None )
|> Array.choose id)
|> Array.concat
|> Array.map (fun name -> mapName episodeIdx (name.ToUpper()))
|> Seq.distinct
|> Seq.map (fun name -> sceneIdx, name)
|> List.ofSeq)
|> List.collect id,
totalScenes
let appearances =
[0 .. 6]
|> List.map getSceneAppearances
|> List.mapi (fun episodeIdx (sceneAppearances, total) ->
sceneAppearances
|> List.map (fun (scene, name) ->
float episodeIdx + float scene / float total, name))
|> List.collect id
|> Seq.groupBy snd
|> Seq.map (fun (name, inScenes) -> name, inScenes |> Seq.map fst |> Array.ofSeq)
|> Array.ofSeq
// Save appearances as pseudo-csv file
let appearancesFilename = __SOURCE_DIRECTORY__ + "/data/charactersPerScene.csv"
let appearsString =
appearances
|> Array.map (fun (name, appears) ->
appears |> Array.map string |> Array.append [|name|] |> String.concat ",")
File.WriteAllLines(appearancesFilename, appearsString)