{ "cells": [ { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline \n", "\n", "import re\n", "import pymongo\n", "\n", "import collections" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "# Open a connection to the Mongo server\n", "client = pymongo.MongoClient('mongodb://localhost:27017/')" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [], "source": [ "# Create a database and a collections within it.\n", "songs_db = client.songs\n", "albums = songs_db.albums\n", "tracks = songs_db.tracks\n", "genius_tracks = songs_db.gtracks\n", "sentiments = songs_db.sentiments" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"i'm going to tell you how it's going to be you're going to give your love to me i'm going to love you night and day well love is love and not fade away well love is love and not fade away my love is bigger than a cadillac i try to show it and you're driving me back your love for me has got to be real for you to know just how i feel love is real and not fade away well love is real and not fade away i'm going to tell you how it's going to be you're going to give your love to me i'm going to love you night and day well love is love and not fade away well love is love and not fade away\"" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_lyrics = tracks.find_one({'lyrics': {'$exists': True}})['lyrics']\n", "test_lyrics" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[\"i'm\",\n", " 'going',\n", " 'to',\n", " 'tell',\n", " 'you',\n", " 'how',\n", " \"it's\",\n", " 'going',\n", " 'to',\n", " 'be',\n", " \"you're\",\n", " 'going',\n", " 'to',\n", " 'give',\n", " 'your',\n", " 'love',\n", " 'to',\n", " 'me',\n", " \"i'm\",\n", " 'going',\n", " 'to',\n", " 'love',\n", " 'you',\n", " 'night',\n", " 'and',\n", " 'day',\n", " 'well',\n", " 'love',\n", " 'is',\n", " 'love',\n", " 'and',\n", " 'not',\n", " 'fade',\n", " 'away',\n", " 'well',\n", " 'love',\n", " 'is',\n", " 'love',\n", " 'and',\n", " 'not',\n", " 'fade',\n", " 'away',\n", " 'my',\n", " 'love',\n", " 'is',\n", " 'bigger',\n", " 'than',\n", " 'a',\n", " 'cadillac',\n", " 'i',\n", " 'try',\n", " 'to',\n", " 'show',\n", " 'it',\n", " 'and',\n", " \"you're\",\n", " 'driving',\n", " 'me',\n", " 'back',\n", " 'your',\n", " 'love',\n", " 'for',\n", " 'me',\n", " 'has',\n", " 'got',\n", " 'to',\n", " 'be',\n", " 'real',\n", " 'for',\n", " 'you',\n", " 'to',\n", " 'know',\n", " 'just',\n", " 'how',\n", " 'i',\n", " 'feel',\n", " 'love',\n", " 'is',\n", " 'real',\n", " 'and',\n", " 'not',\n", " 'fade',\n", " 'away',\n", " 'well',\n", " 'love',\n", " 'is',\n", " 'real',\n", " 'and',\n", " 'not',\n", " 'fade',\n", " 'away',\n", " \"i'm\",\n", " 'going',\n", " 'to',\n", " 'tell',\n", " 'you',\n", " 'how',\n", " \"it's\",\n", " 'going',\n", " 'to',\n", " 'be',\n", " \"you're\",\n", " 'going',\n", " 'to',\n", " 'give',\n", " 'your',\n", " 'love',\n", " 'to',\n", " 'me',\n", " \"i'm\",\n", " 'going',\n", " 'to',\n", " 'love',\n", " 'you',\n", " 'night',\n", " 'and',\n", " 'day',\n", " 'well',\n", " 'love',\n", " 'is',\n", " 'love',\n", " 'and',\n", " 'not',\n", " 'fade',\n", " 'away',\n", " 'well',\n", " 'love',\n", " 'is',\n", " 'love',\n", " 'and',\n", " 'not',\n", " 'fade',\n", " 'away']" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_lyrics.split()" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i'm []\n", "going []\n", "to []\n", "tell []\n", "you []\n", "how []\n", "it's []\n", "going []\n", "to []\n", "be []\n", "you're []\n", "going []\n", "to []\n", "give []\n", "your []\n", "love ['joy', 'positive']\n", "to []\n", "me []\n", "i'm []\n", "going []\n", "to []\n", "love ['joy', 'positive']\n", "you []\n", "night []\n", "and []\n", "day []\n", "well []\n", "love ['joy', 'positive']\n", "is []\n", "love ['joy', 'positive']\n", "and []\n", "not []\n", "fade ['negative']\n", "away []\n", "well []\n", "love ['joy', 'positive']\n", "is []\n", "love ['joy', 'positive']\n", "and []\n", "not []\n", "fade ['negative']\n", "away []\n", "my []\n", "love ['joy', 'positive']\n", "is []\n", "bigger []\n", "than []\n", "a []\n", "cadillac []\n", "i []\n", "try []\n", "to []\n", "show ['trust']\n", "it []\n", "and []\n", "you're []\n", "driving []\n", "me []\n", "back []\n", "your []\n", "love ['joy', 'positive']\n", "for []\n", "me []\n", "has []\n", "got []\n", "to []\n", "be []\n", "real ['positive', 'trust']\n", "for []\n", "you []\n", "to []\n", "know []\n", "just []\n", "how []\n", "i []\n", "feel []\n", "love ['joy', 'positive']\n", "is []\n", "real ['positive', 'trust']\n", "and []\n", "not []\n", "fade ['negative']\n", "away []\n", "well []\n", "love ['joy', 'positive']\n", "is []\n", "real ['positive', 'trust']\n", "and []\n", "not []\n", "fade ['negative']\n", "away []\n", "i'm []\n", "going []\n", "to []\n", "tell []\n", "you []\n", "how []\n", "it's []\n", "going []\n", "to []\n", "be []\n", "you're []\n", "going []\n", "to []\n", "give []\n", "your []\n", "love ['joy', 'positive']\n", "to []\n", "me []\n", "i'm []\n", "going []\n", "to []\n", "love ['joy', 'positive']\n", "you []\n", "night []\n", "and []\n", "day []\n", "well []\n", "love ['joy', 'positive']\n", "is []\n", "love ['joy', 'positive']\n", "and []\n", "not []\n", "fade ['negative']\n", "away []\n", "well []\n", "love ['joy', 'positive']\n", "is []\n", "love ['joy', 'positive']\n", "and []\n", "not []\n", "fade ['negative']\n", "away []\n" ] } ], "source": [ "for w in test_lyrics.split():\n", " ss = [s for sd in sentiments.find({'word': w}) for s in sd['sentiments']]\n", "# sd = sentiments.find_one({'word': w})\n", "# if sd:\n", "# ss = sd['sentiments']\n", "# else:\n", "# ss = []\n", " print(w, ss)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'joy': 16, 'negative': 6, 'positive': 19, 'trust': 4}" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(collections.Counter(s for w in test_lyrics.split()\n", " for sd in sentiments.find({'word': w}) \n", " for s in sd['sentiments']))" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2038" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "genius_tracks.find({'original_lyrics': {'$exists': True}}).count()" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'_id': 1497768,\n", " 'annotation_count': 1,\n", " 'api_path': '/songs/1497768',\n", " 'ctitle': 'all together on the wireless machine',\n", " 'full_title': 'All Together on the Wireless Machine by\\xa0The\\xa0Beatles',\n", " 'header_image_thumbnail_url': 'https://images.genius.com/ad1f59e8a03be4eb521e88015d15d6e8.200x200x1.jpg',\n", " 'header_image_url': 'https://images.genius.com/ad1f59e8a03be4eb521e88015d15d6e8.200x200x1.jpg',\n", " 'id': 1497768,\n", " 'lyrics': 'when i was sitting on my piano one day a magical thought came my way to write a number for the bbc kenny everett mccartney all together on the wireless machine kenny everett mccartney all together on the wireless machine kenny everett mccartney all together on the wireless machine',\n", " 'lyrics_owner_id': 1549345,\n", " 'original_lyrics': '\\n\\nWhen I was sitting on my piano one day\\nA magical thought came my way\\nTo write a number for the BBC\\nKenny Everett McCartney\\nAll together on the wireless machine\\nKenny Everett McCartney\\nAll together on the wireless machine\\nKenny Everett McCartney\\nAll together on the wireless machine\\n\\n',\n", " 'path': '/The-beatles-all-together-on-the-wireless-machine-lyrics',\n", " 'primary_artist': {'api_path': '/artists/586',\n", " 'header_image_url': 'https://images.genius.com/b82dbb78926a812abfa10886ac84c1a8.1000x523x1.jpg',\n", " 'id': 586,\n", " 'image_url': 'https://images.genius.com/ad1f59e8a03be4eb521e88015d15d6e8.200x200x1.jpg',\n", " 'is_meme_verified': False,\n", " 'is_verified': False,\n", " 'name': 'The Beatles',\n", " 'url': 'https://genius.com/artists/The-beatles'},\n", " 'pyongs_count': None,\n", " 'song_art_image_thumbnail_url': 'https://images.genius.com/ad1f59e8a03be4eb521e88015d15d6e8.200x200x1.jpg',\n", " 'stats': {'hot': False, 'unreviewed_annotations': 0},\n", " 'title': 'All Together on the Wireless Machine',\n", " 'url': 'https://genius.com/The-beatles-all-together-on-the-wireless-machine-lyrics'}" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "genius_tracks.find_one({'original_lyrics': {'$exists': True}})" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [], "source": [ "# for gsong in genius_tracks.find({'original_lyrics': {'$exists': True}}):\n", "# l2 = re.sub('\\[[^\\]]*\\]', '', gsong['original_lyrics'])\n", "# l3 = re.sub('\\[|\\]', '', l2)\n", "# l4 = re.sub('(\\s)+', ' ', l3)\n", "# l5 = re.sub('[,.!?;:]', '', l4)\n", "# genius_tracks.update_one({'_id': gsong['_id']}, \n", "# {'$set': {'lyrics': l5.strip().lower()}})" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [], "source": [ "# for song in tracks.find({'original_lyrics': {'$exists': True}}):\n", "# l2 = re.sub('\\[[^\\]]*\\]', '', song['original_lyrics'])\n", "# l3 = re.sub('\\[|\\]', '', l2)\n", "# l4 = re.sub('(\\s)+', ' ', l3)\n", "# l5 = re.sub('[,.!?;:]', '', l4)\n", "# tracks.update_one({'_id': song['_id']}, \n", "# {'$set': {'lyrics': l5.strip().lower()}})" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [], "source": [ "for song in tracks.find({'lyrics': {'$exists': True}}):\n", " ss = dict(collections.Counter(s for w in song['lyrics'].split()\n", " for sd in sentiments.find({'word': w})\n", " for s in sd['sentiments']))\n", " if ss:\n", " mv = max(ss.values())\n", " nss = {k: v / mv for k, v in ss.items()}\n", " else:\n", " nss = {}\n", " tracks.update_one({'_id': song['_id']}, {'$set': {'nrc_sentiment': ss,\n", " 'nnrc_sentiment': nss}})" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'_id': '1jgefM2ZP7RnPVShhy1eUM',\n", " 'lyrics': \"i'm going to tell you how it's going to be you're going to give your love to me i'm going to love you night and day well love is love and not fade away well love is love and not fade away my love is bigger than a cadillac i try to show it and you're driving me back your love for me has got to be real for you to know just how i feel love is real and not fade away well love is real and not fade away i'm going to tell you how it's going to be you're going to give your love to me i'm going to love you night and day well love is love and not fade away well love is love and not fade away\",\n", " 'name': 'Not Fade Away - Live',\n", " 'nnrc_sentiment': {'joy': 0.8421052631578947,\n", " 'negative': 0.3157894736842105,\n", " 'positive': 1.0,\n", " 'trust': 0.21052631578947367},\n", " 'nrc_sentiment': {'joy': 16, 'negative': 6, 'positive': 19, 'trust': 4}},\n", " {'_id': '3doVdGo0NrKWDuiAUiWyCY',\n", " 'lyrics': \"you and me we are destined you'll agree to spend the rest of our lives with each other the rest of our days like two lovers for ever yeah for ever my bijou\",\n", " 'name': 'Bijou',\n", " 'nnrc_sentiment': {'anticipation': 0.3333333333333333, 'positive': 1.0},\n", " 'nrc_sentiment': {'anticipation': 1, 'positive': 3}},\n", " {'_id': '2ucFulEWapRAmTn7l6f5Q7',\n", " 'lyrics': 'honey pie honey pie i love you honey pie',\n", " 'name': 'Wild Honey Pie - Remastered',\n", " 'nnrc_sentiment': {'joy': 0.25, 'positive': 1.0},\n", " 'nrc_sentiment': {'joy': 1, 'positive': 4}},\n", " {'_id': '4zN811GoD9cpxxvEc0dXyn',\n", " 'lyrics': 'this track is instrumental no lyrics',\n", " 'name': '2120 South Michigan Avenue - Rhythm And Blues / BBC World Service / 1964',\n", " 'nnrc_sentiment': {'anticipation': 1.0, 'positive': 1.0},\n", " 'nrc_sentiment': {'anticipation': 1, 'positive': 1}},\n", " {'_id': '30a68RyhjUjCxLf6JehuYR',\n", " 'lyrics': 'instrumental ahhh ahhh ahhh',\n", " 'name': 'Arrival',\n", " 'nnrc_sentiment': {'positive': 1.0},\n", " 'nrc_sentiment': {'positive': 1}},\n", " {'_id': '12Gx5uHEmfW7JaUDfAFXrp',\n", " 'lyrics': 'instrumental',\n", " 'name': 'Intermezzo No.1',\n", " 'nnrc_sentiment': {'positive': 1.0},\n", " 'nrc_sentiment': {'positive': 1}},\n", " {'_id': '0CJuzA63oAQb8dJDY5IKhZ',\n", " 'lyrics': '1 2 3 4',\n", " 'name': 'Ballad Of The Beaconsfield Miners',\n", " 'nnrc_sentiment': {},\n", " 'nrc_sentiment': {}},\n", " {'_id': '53bUZHJf4W6mkP5zEXxuzi',\n", " 'lyrics': '(instrumental)',\n", " 'name': 'LA Drone - Live',\n", " 'nnrc_sentiment': {},\n", " 'nrc_sentiment': {}},\n", " {'_id': '3S16cvoEfCj7ZC82cLjXrd',\n", " 'lyrics': 'instrumental',\n", " 'name': 'Moby Dick - Live',\n", " 'nnrc_sentiment': {'positive': 1.0},\n", " 'nrc_sentiment': {'positive': 1}},\n", " {'_id': '7n8FOouaabpFxOo2dvr6Nx',\n", " 'lyrics': 'instrumental',\n", " 'name': \"Bonzo's Montreux\",\n", " 'nnrc_sentiment': {'positive': 1.0},\n", " 'nrc_sentiment': {'positive': 1}},\n", " {'_id': '5yiMPpkzl5hTeh0EV3IJlb',\n", " 'lyrics': 'instrumental',\n", " 'name': \"Bonzo's Montreux\",\n", " 'nnrc_sentiment': {'positive': 1.0},\n", " 'nrc_sentiment': {'positive': 1}},\n", " {'_id': '2GdYdQEEH09MaEJKqB3iyk',\n", " 'lyrics': 'instrumental',\n", " 'name': \"Bonzo's Montreux\",\n", " 'nnrc_sentiment': {'positive': 1.0},\n", " 'nrc_sentiment': {'positive': 1}},\n", " {'_id': '3wcbVq8PGuu0sevHG6hDzI',\n", " 'lyrics': 'instrumental',\n", " 'name': 'Moby Dick - 2007 Remastered Version Live Version From The Song Remains The Same',\n", " 'nnrc_sentiment': {'positive': 1.0},\n", " 'nrc_sentiment': {'positive': 1}},\n", " {'_id': '7Jsud7J2MoQM7Y14V2WmQv',\n", " 'lyrics': '(instrumental)',\n", " 'name': 'Drum And Tympani Solo - Live At The Montreal Forum / November 1981',\n", " 'nnrc_sentiment': {},\n", " 'nrc_sentiment': {}},\n", " {'_id': '65YeIE3Y4YBNLnXcpVZz1P',\n", " 'lyrics': 'yeah',\n", " 'name': 'Yeah - Remastered 2011',\n", " 'nnrc_sentiment': {},\n", " 'nrc_sentiment': {}},\n", " {'_id': '50s50lFVFxZcw7h9fTG9fE',\n", " 'lyrics': 'yeah',\n", " 'name': 'Yeah - Remastered 2011',\n", " 'nnrc_sentiment': {},\n", " 'nrc_sentiment': {}},\n", " {'_id': '6ymSCvhKCttKROfCvBBOsr',\n", " 'lyrics': 'people keep on talking bogeying around take you to me party sitting on the town yeah hey',\n", " 'name': \"Gimme Some Lovin' - Live At Wembley Stadium / July 1986\",\n", " 'nnrc_sentiment': {},\n", " 'nrc_sentiment': {}},\n", " {'_id': '4edArG2VehvJdwOZfYOxtK',\n", " 'lyrics': 'well shake it up baby now (shake it up baby) twist and shout (twist and shout) c’mon c’mon c’mon c’mon baby now (c’mon baby) c’mon and work it on out (work it on out) work it on out (work it on out) you know you look so good (look so good) you know you got me going now (got me going) just like i knew you would (like i knew you would) well shake it up baby now (shake it up baby) twist and shout (twist and shout) c’mon c’mon c’mon c’mon baby now (c’mon baby) c’mon and work it on out (work it on out) you know you twist your little girl (twist little girl) you know you twist so fine (twist so fine) c’mon on twist a little closer now (twist a little closer) let me know that your mine (let me know your mine) ah ah… ah ah baby now shake it up baby now twist and shout (twist and shout) c’mon c’mon c’mon c’mon baby now (c’mon baby) c’mon and work it on out (work it on out) you know you twist your little girl (twist little girl) you know you twist so fine (twist so fine) c’mon on twist a little closer now (twist a little closer) let me know that you’re mine (know you’re mine) well shake it shake it shake it baby now (shake it up baby) well shake it shake it shake it baby now (shake it up baby) well shake it shake it shake it baby now (shake it up baby) ah ah ah ah',\n", " 'name': 'Twist And Shout - Live / Remastered',\n", " 'nnrc_sentiment': {'anger': 0.2727272727272727,\n", " 'anticipation': 0.09090909090909091,\n", " 'joy': 1.0,\n", " 'positive': 1.0,\n", " 'surprise': 0.36363636363636365,\n", " 'trust': 0.09090909090909091},\n", " 'nrc_sentiment': {'anger': 3,\n", " 'anticipation': 1,\n", " 'joy': 11,\n", " 'positive': 11,\n", " 'surprise': 4,\n", " 'trust': 1}},\n", " {'_id': '150EAeMGWJRubuH8zyx7h8',\n", " 'lyrics': \"my love don't give me presents i know that she's no peasant only ever has to give me love forever and forever my love don't give me presents turn me on when i get lonely people tell me that she's only fooling i know she isn't she don't give the boys the eye she hates to see me cry she is happy just to hear me say that i will never leave her she don't give the boys the eye she will never make me jealous gives me all her time as well as loving don't ask me why she's a woman who understands she's a woman who loves her man\",\n", " 'name': \"She's A Woman - Live / Remastered\",\n", " 'nnrc_sentiment': {'anger': 0.4,\n", " 'anticipation': 0.4,\n", " 'disgust': 0.4,\n", " 'fear': 0.2,\n", " 'joy': 1.0,\n", " 'negative': 0.8,\n", " 'positive': 1.0,\n", " 'sadness': 0.6,\n", " 'surprise': 0.2,\n", " 'trust': 0.4},\n", " 'nrc_sentiment': {'anger': 2,\n", " 'anticipation': 2,\n", " 'disgust': 2,\n", " 'fear': 1,\n", " 'joy': 5,\n", " 'negative': 4,\n", " 'positive': 5,\n", " 'sadness': 3,\n", " 'surprise': 1,\n", " 'trust': 2}},\n", " {'_id': '1fVeHYkyMxrjbjRAD9uWsZ',\n", " 'lyrics': \"{intro} you make me dizzy miss lizzy the way you rock and roll you make me dizzy miss lizzy when we do the stroll come on miss lizzy love me before i grow too old come on give me fever put your little hand in mine you make me dizzy dizzy lizzy oh girl you look so fine just a-rocking and a-rolling girl i said i wish you were mine aw {bridge} aw you make me dizzy miss lizzy when you call my name oooooh baby say you're driving me insane c'mon c'mon c'mon c'mon baby i want to be your loving man {bridge} run and tell your mama i want you to be my bride run and tell your brother baby don't run and hide you make me dizzy miss lizzy girl i want to marry you come on give me fever put your little hand in mine girl you make me dizzy dizzy lizzy girl you look so fine you're just a-rocking and a-rolling oh i said i wish you were mine {outro}\",\n", " 'name': 'Dizzy Miss Lizzy - Live / Remastered',\n", " 'nnrc_sentiment': {'anger': 0.1,\n", " 'anticipation': 0.3,\n", " 'fear': 0.5,\n", " 'joy': 0.8,\n", " 'negative': 0.9,\n", " 'positive': 1.0,\n", " 'surprise': 0.1,\n", " 'trust': 0.5},\n", " 'nrc_sentiment': {'anger': 1,\n", " 'anticipation': 3,\n", " 'fear': 5,\n", " 'joy': 8,\n", " 'negative': 9,\n", " 'positive': 10,\n", " 'surprise': 1,\n", " 'trust': 5}}]" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(tracks.find({'lyrics': {'$exists': True}}, ['name', 'lyrics', 'nrc_sentiment', 'nnrc_sentiment'], limit=20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }