Skip to content

Instantly share code, notes, and snippets.

@safehammad
Last active February 15, 2021 08:33
Show Gist options
  • Save safehammad/04d95b5f6548459fb68aa8e8633eddc0 to your computer and use it in GitHub Desktop.
Save safehammad/04d95b5f6548459fb68aa8e8633eddc0 to your computer and use it in GitHub Desktop.
Explorations on James Bond film title generation
;; Explorations on James Bond film titles
;; See blog post:
;; Full source code: https://github.com/safehammad/bond-title-generator
(def titles ["Dr No" "From Russia with Love" "Gold finger" "Thunder ball" "You Only Live Twice" "On Her Majesty’s Secret Service" "Diamonds Are Forever" "Live and Let Die" "The Man with the Golden Gun" "The Spy Who Loved Me" "Moon raker" "For Your Eyes Only" "Octo pussy" "Never Say Never Again" "A View to a Kill" "The Living Day lights" "Licence to Kill" "Golden" "Eye" "Tomorrow Never Dies" "The World is Not Enough" "Die Another Day" "Casino Royale" "Quantum of Solace" "Sky fall" "Spectre" "No Time to Die"])
(def tokenised-words (map str/lower-case (mapcat #(str/split % #" ") titles)))
; => ["dr" "no" "from" "russia" "with" "love" "gold" "finger" "thunder" "ball" "you" "only" "live" "twice" "on" "her" "majesty’s" "secret" "service" "diamonds" "are" "forever" "live" "and" "let" "die" "the" "man" "with" "the" "golden" "gun" "the" "spy" "who" "loved" "me" "moon" "raker" "for" "your" "eyes" "only" "octo" "pussy" "never" "say" "never" "again" "a" "view" "to" "a" "kill" "the" "living" "day" "lights" "licence" "to" "kill" "golden" "eye" "tomorrow" "never" "dies" "the" "world" "is" "not" "enough" "die" "another" "day" "casino" "royale" "quantum" "of" "solace" "sky" "fall" "spectre" "no" "time" "to" "die"]
(def word-count (count tokenised-words))
; => 86
(def unique-word-count (count (set tokenised-words)))
; => 68
(def repeated-occurrences (->> tokenised-words frequencies (remove #(= (val %) 1)) (sort-by val >)))
; => (["the" 5] ["to" 3] ["never" 3] ["die" 3] ["live" 2] ["golden" 2] ["only" 2] ["kill" 2] ["day" 2] ["with" 2] ["no" 2] ["a" 2])
(def stop-words #{"the" "to" "with" "a"})
(def distinctive-occurrences (remove (comp stop-words key) repeated-occurrences))
; => (["never" 3] ["die" 3] ["live" 2] ["golden" 2] ["only" 2] ["kill" 2] ["day" 2] ["no" 2])
(def distinctive-word-count (count distinctive-occurrences))
; => 8
(def distinctive-word-count-occurrences (apply + (map second distinctive-occurrences)))
; => 18
(def distinctive-words (set (map first distinctive-occurrences)))
; => #{"die" "never" "no" "day" "kill" "only" "golden" "live"}
(def titles-with-distinctive-words (count (filter (partial some distinctive-words) (map (partial map str/lower-case) (map #(str/split % #" ") titles)))))
; => 13
(def title-word-lengths
(->> titles
(map #(str/split % #" "))
(map count)
((juxt (partial apply min) (partial apply max)))))
; => [1 6]
(defn random-title []
(take (inc (rand-int 6)) (shuffle tokenised-words)))
; => ("the" "tomorrow" "dies" "are" "die" "finger")
; => ("on" "live" "no" "the")
; => ("with" "secret" "again" "the" "her" "dies")
; => ("love" "me" "diamonds" "the" "a" "golden")
; => ("enough" "for" "never" "only")
; => ("dr" "ball")
; => ("gun" "world" "dr")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment