{"id":2680,"date":"2023-09-06T18:14:54","date_gmt":"2023-09-07T02:14:54","guid":{"rendered":"https:\/\/enfascination.com\/weblog\/?p=2680"},"modified":"2023-10-19T18:55:29","modified_gmt":"2023-10-20T02:55:29","slug":"understanding-taylor-swift-with-python","status":"publish","type":"post","link":"https:\/\/enfascination.com\/weblog\/post\/2680","title":{"rendered":"Understanding Taylor Swift with Python"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/enfascination.com\/weblog\/wp-content\/uploads\/2023\/09\/ts-banner-1800-600-px-e1694052757261.png\" alt=\"\" width=\"315\" height=\"423\" class=\"alignnone size-full wp-image-2681\" srcset=\"https:\/\/enfascination.com\/weblog\/wp-content\/uploads\/2023\/09\/ts-banner-1800-600-px-e1694052757261.png 315w, https:\/\/enfascination.com\/weblog\/wp-content\/uploads\/2023\/09\/ts-banner-1800-600-px-e1694052757261-223x300.png 223w\" sizes=\"(max-width: 315px) 100vw, 315px\" \/>Here are the complete lyrics of Taylor Swift&#8217;s &#8220;Shake it off&#8221;, in the form of a Python string<\/p>\n<pre>shakeItOffComplete = \"\"\"\r\nI stay out too late\r\nGot nothing in my brain\r\nThat's what people say, mm, mm\r\nThat's what people say, mm, mm\r\nI go on too many dates\r\nBut I can't make them stay\r\nAt least that's what people say, mm, mm\r\nThat's what people say, mm, mm\r\n\r\nBut I keep cruisin'\r\nCan't stop, won't stop movin'\/groovin'\r\nIt's like I got this music in my mind\r\nSaying it's gonna be alright\r\n\r\n'Cause the players gonna play, play, play, play, play, \r\nAnd the haters gonna hate, hate, hate, hate, hate, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\nHeartbreakers gonna break, break, break, break, break, \r\nAnd the fakers gonna fake, fake, fake, fake, fake, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\n\r\nI never miss a beat\r\nI'm lightning on my feet\r\nAnd that's what they don\u2019t see, mm, mm\r\nThat's what they don\u2019t see, mm, mm\r\nI'm dancing on my own (dancing on my own)\r\nI make the moves up as I go (moves up as I go)\r\nAnd that's what they don't know, mm, mm\r\nThat\u2019s what they don\u2019t know, mm, mm\r\n\r\nBut I keep cruisin'\r\nCan't stop, won't stop movin'\/groovin'\r\nIt's like I got this music in my mind\r\nSaying it's gonna be alright\r\n\r\n'Cause the players gonna play, play, play, play, play, \r\nAnd the haters gonna hate, hate, hate, hate, hate, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\nHeartbreakers gonna break, break, break, break, break, \r\nAnd the fakers gonna fake, fake, fake, fake, fake, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\n\r\nShake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\n\r\nHey, hey, hey\r\nJust think while you've been getting down and out about the liars\r\nAnd the dirty, dirty cheats of the world\r\nYou could've been getting down\r\nTo this sick beat\r\n\r\nMy ex-man brought his new girlfriend\r\nShe's like, \u201cOh my God,\u201d but I'm just gonna shake\r\nAnd to the fella over there with the hella good hair\r\nWon't you come on over, baby?\r\nWe can shake, shake, shake\r\nYeah, oh, oh, oh\r\n\r\n'Cause the players gonna play, play, play, play, play, \r\nAnd the haters gonna hate, hate, hate, hate, hate, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\nHeartbreakers gonna break, break, break, break, break, \r\nAnd the fakers gonna fake, fake, fake, fake, fake, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\n\r\nShake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\n\r\nShake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\n\r\nShake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\n\"\"\"<\/pre>\n<pre>print( type(shakeItOffComplete))<\/pre>\n<p>By representing it in Python we can learn about the formulae underlying pop music.  Let&#8217;s break it up into parts to see how it is structured.<\/p>\n<pre>verse1 = \"\"\"\r\nI stay out too late\r\nGot nothing in my brain\r\nThat's what people say, mm, mm\r\nThat's what people say, mm, mm\r\nI go on too many dates\r\nBut I can't make them stay\r\nAt least that's what people say, mm, mm\r\nThat's what people say, mm, mm\r\n\"\"\"\r\n\r\nprechorus = \"\"\"\r\nBut I keep cruisin'\r\nCan't stop, won't stop movin'\/groovin'\r\nIt's like I got this music in my mind\r\nSaying it's gonna be alright\r\n\"\"\"\r\n\r\nchorus = \"\"\"\r\n'Cause the players gonna play, play, play, play, play, \r\nAnd the haters gonna hate, hate, hate, hate, hate, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\nHeartbreakers gonna break, break, break, break, break, \r\nAnd the fakers gonna fake, fake, fake, fake, fake, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\n\"\"\"\r\n\r\nverse2 = \"\"\"\r\nI never miss a beat\r\nI'm lightning on my feet\r\nAnd that's what they don\u2019t see, mm, mm\r\nThat's what they don\u2019t see, mm, mm\r\nI'm dancing on my own (dancing on my own)\r\nI make the moves up as I go (moves up as I go)\r\nAnd that's what they don't know, mm, mm\r\nThat\u2019s what they don\u2019t know, mm, mm\r\n\"\"\"\r\n\r\npostchorus = \"\"\"\r\nShake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\n\"\"\"\r\n\r\ninterlude = \"\"\"\r\nHey, hey, hey\r\nJust think while you've been getting down and out about the liars\r\nAnd the dirty, dirty cheats of the world\r\nYou could've been getting down\r\nTo this sick beat\r\n\"\"\"\r\n\r\nbridge = \"\"\"\r\nMy ex-man brought his new girlfriend\r\nShe's like, \u201cOh my God,\u201d but I'm just gonna shake\r\nAnd to the fella over there with the hella good hair\r\nWon't you come on over, baby?\r\nWe can shake, shake, shake\r\nYeah, oh, oh, oh\r\n\"\"\"<\/pre>\n<p>With those parts, the variables verse1, verse2, prechorus, chorus, postchorus, interlude, and bridge we can see how &#8220;Shake it off&#8221; is structured (and also represent it with a lot less typing).<\/p>\n<pre>shakeItOffReconstructed = (verse1 +   # (it's ok to stretch expressions over several lines. It can help readabiilty)\r\n                          prechorus + \r\n                          chorus + \r\n                          verse2 + \r\n                          prechorus + \r\n                          chorus + \r\n                          postchorus + \r\n                          interlude + \r\n                          bridge + \r\n                          chorus + \r\n                          postchorus * 3 )  # repeats three times\r\n#print( shakeItOffReconstructed )<\/pre>\n<p>Is it really that simple? Let&#8217;s test and see if these strings are the same.<\/p>\n<pre>shakeItOffComplete == shakeItOffReconstructed<\/pre>\n<h2>Verse-level representation<\/h2>\n<p>That&#8217;s some nice compression, but we can do better. There is a lot of repetition all over the song that we can capture in variables and chunk down. For example, the &#8220;mm, mm&#8221;s and &#8220;That&#8217;s what people say&#8221;&#8216;s in the verses could be chunked down. But the most redundancy in any pop song is going to be the in the chorus and, in this song, especially the post-chorus. Let&#8217;s see if we can rewrite them into a more compact form.<\/p>\n<pre>### Original\r\nchorus = \"\"\"\r\n'Cause the players gonna play, play, play, play, play, \r\nAnd the haters gonna hate, hate, hate, hate, hate, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\nHeartbreakers gonna break, break, break, break, break, \r\nAnd the fakers gonna fake, fake, fake, fake, fake, \r\nBaby, I'm just gonna shake, shake, shake, shake, shake, \r\nI shake it off, I shake it off\r\n\"\"\"\r\n\r\n### Refrain\r\nshk = 'I shake it off'   # this gets used a lot, so it gets a variable \r\n\r\n### Replacement\r\nchorusReconstructed = \"\"\"\r\n'Cause the players gonna {}\r\nAnd the haters gonna {}\r\nBaby, I'm just gonna {}\r\n{}, {}\r\nHeartbreakers gonna {}\r\nAnd the fakers gonna {}\r\nBaby, I'm just gonna {}\r\n{}, {}\r\n\"\"\".format('play, ' * 5, \r\n           'hate, ' * 5, \r\n           'shake, ' * 5, \r\n           shk, shk, \r\n           'break, ' * 5, \r\n           'fake, ' * 5, \r\n           'shake, ' * 5, \r\n           shk, shk)\r\n\r\n#print( chorusReconstructed)\r\n\r\n### Test for success\r\nchorus == chorusReconstructed<\/pre>\n<p>The new chorus is identical content, typed in about half as many characters. That means that, in some sense, about half the chorus of &#8220;Shake it off&#8221; is redundant.<\/p>\n<p>How about the post-chorus? We&#8217;ve already defined placeholder variable shk, which it looks like we&#8217;ll keep using.<\/p>\n<pre>### Original\r\npostchorus = \"\"\"\r\nShake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\nI, I, I shake it off, I shake it off\r\n\"\"\"\r\n\r\n### Replacement\r\npostchorusReconstructed = \"\"\"\r\nShake it off, {}\r\nI, I, {}, {}\r\nI, I, {}, {}\r\nI, I, {}, {}\r\n\"\"\".format( shk, shk, shk, shk, shk, shk, shk )\r\n\r\n### Better replacement\r\n###    (observe above that most shk's are repeated twice.\r\n###     We can use that to get a bit more compression)\r\nshk2 = shk + ', ' + shk\r\npostchorusReconstructed2 = \"\"\"\r\nShake it off, {}\r\nI, I, {}\r\nI, I, {}\r\nI, I, {}\r\n\"\"\".format( shk, shk2, shk2, shk2 )\r\n\r\n### Even better replacement\r\n###    (observe above that most shk's are preceded by a comma and a space.\r\n###     We can use that too)\r\ncshk = ', ' + shk   \r\nshk2 = cshk + cshk\r\npostchorusReconstructed3 = \"\"\"\r\nShake it off{}\r\nI, I{}\r\nI, I{}\r\nI, I{}\r\n\"\"\".format( cshk, shk2, shk2, shk2 )\r\n\r\n\r\n### Too far?\r\ni = 'I, I'\r\nshk2 = i + cshk + cshk\r\npostchorusReconstructed3 = \"\"\"\r\nShake it off{}\r\n{}\r\n{}\r\n{}\r\n\"\"\".format( cshk, shk2, shk2, shk2 )\r\n\r\n### Test for success\r\npostchorus == postchorusReconstructed3<\/pre>\n<p>We&#8217;ve reduced postchorus to almost a third of the size. Add to that that the final post-chorus of the song repeats postchorus three times, and that&#8217;s a total reduction of about nine times. In other words, from an informational standpoint, the last 20% of the song is 90% redundant.<\/p>\n<pre>### The last 20% (40 seconds) of \"Shake it off\" in one line\r\n###  \"\\n\" is the character representation of the line break\/return key\r\nprint( ( ('Shake it off' + cshk + ('\\n' + shk2) * 3 ) + '\\n' ) * 3 )<\/pre>\n<p>Of course, it&#8217;s really not fair to evaluate music from an informational standpoint. There are other standpoints that make sense for music. Nevertheless this exercise does do something useful for us. Breaking a thing down into parts\u2014&#8221;ana-lysis&#8221;\u2014teaches us about a thing by showing us its natural faultlines, and revealing the formula behind it. And it&#8217;s just that kind of breaking-things-down that programming makes you good at.<\/p>\n<p>If you end up analyzing another song this way, let me know!<\/p>\n<p>FYI, this is an excerpt from a lesson out of <a href=\"http:\/\/0w.uk\/pythoncourse\">my Python course<\/a> at UC Davis and <a href=\"https:\/\/www.coursera.org\/specializations\/python-for-social-science\">on Coursera<\/a>.  <\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content --><!-- AddThis Related Posts generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Here are the complete lyrics of Taylor Swift&#8217;s &#8220;Shake it off&#8221;, in the form of a Python string shakeItOffComplete = &#8220;&#8221;&#8221; I stay out too late Got nothing in my brain That&#8217;s what people say, mm, mm That&#8217;s what people say, mm, mm I go on too many dates But I can&#8217;t make them stay &hellip; <a href=\"https:\/\/enfascination.com\/weblog\/post\/2680\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Understanding Taylor Swift with Python<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><!-- AddThis Related Posts generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":2681,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpupg_custom_link":[],"wpupg_custom_link_behaviour":["default"],"wpupg_custom_image":[],"wpupg_custom_image_id":[],"footnotes":""},"categories":[17,33,15],"tags":[],"_links":{"self":[{"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/posts\/2680"}],"collection":[{"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/comments?post=2680"}],"version-history":[{"count":3,"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/posts\/2680\/revisions"}],"predecessor-version":[{"id":2684,"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/posts\/2680\/revisions\/2684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/media\/2681"}],"wp:attachment":[{"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/media?parent=2680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/categories?post=2680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enfascination.com\/weblog\/wp-json\/wp\/v2\/tags?post=2680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}