-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from anGie44/f-season-connections
feat: add back in connections support
- Loading branch information
Showing
7 changed files
with
145 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package data | ||
|
||
import ( | ||
combinations "github.com/mxschmitt/golang-combinations" | ||
) | ||
|
||
type Connection struct { | ||
Episode int `bson:"episode" json:"episode"` | ||
EpisodeName string `bson:"episode_name" json:"episode_name"` | ||
Links []*Link `bson:"links" json:"links"` | ||
Nodes []*Node `bson:"nodes" json:"nodes"` | ||
} | ||
|
||
type Link struct { | ||
Source string `bson:"source" json:"source"` | ||
Target string `bson:"target" json:"target"` | ||
Value int `bson:"value" json:"value"` | ||
} | ||
|
||
type Node struct { | ||
Id string `bson:"id" json:"id"` | ||
} | ||
|
||
type Connections []*Connection | ||
|
||
type episode struct { | ||
name string | ||
scenes map[int]Quotes | ||
} | ||
|
||
func GetConnectionsPerEpisode(seasonQuotes Quotes) Connections { | ||
// Map of quotes per episode | ||
quotesByEpisode := make(map[int]*episode) | ||
|
||
for _, quote := range seasonQuotes { | ||
_, ok := quotesByEpisode[quote.Episode] | ||
if ok { | ||
_, sceneOk := quotesByEpisode[quote.Episode].scenes[quote.Scene] | ||
if sceneOk { | ||
quotesByEpisode[quote.Episode].scenes[quote.Scene] = append(quotesByEpisode[quote.Episode].scenes[quote.Scene], quote) | ||
} else { | ||
quotesByEpisode[quote.Episode].scenes[quote.Scene] = Quotes{quote} | ||
} | ||
} else { | ||
quotesByEpisode[quote.Episode] = &episode{ | ||
name: quote.EpisodeName, | ||
scenes: map[int]Quotes{ | ||
quote.Scene: []*Quote{quote}, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
var connections Connections | ||
|
||
for episodeNum, episode := range quotesByEpisode { | ||
// Loop through scenes in an episode | ||
linksMap := make(map[Link]int) | ||
nodesMap := make(map[string]struct{}) | ||
|
||
for _, quotes := range episode.scenes { | ||
charactersInScene := make(map[string]struct{}) | ||
var exists = struct{}{} | ||
|
||
for _, quote := range quotes { | ||
charactersInScene[quote.Character] = exists | ||
} | ||
|
||
var chars []string | ||
for c := range charactersInScene { | ||
chars = append(chars, c) | ||
nodesMap[c] = exists | ||
} | ||
|
||
if len(chars) < 2 { | ||
continue | ||
} | ||
|
||
combinations := combinations.Combinations(chars, 2) | ||
|
||
for _, combo := range combinations { | ||
link := Link{ | ||
Source: combo[0], | ||
Target: combo[1], | ||
} | ||
|
||
linksMap[link]++ | ||
} | ||
} | ||
|
||
var nodes []*Node | ||
for c := range nodesMap { | ||
nodes = append(nodes, &Node{ | ||
Id: c, | ||
}) | ||
} | ||
|
||
var links []*Link | ||
for l, count := range linksMap { | ||
links = append(links, &Link{ | ||
Source: l.Source, | ||
Target: l.Target, | ||
Value: count, | ||
}) | ||
} | ||
|
||
connections = append(connections, &Connection{ | ||
Episode: episodeNum, | ||
EpisodeName: episode.name, | ||
Links: links, | ||
Nodes: nodes, | ||
}) | ||
} | ||
|
||
return connections | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters