Assignment 5

In this assignment, you'll scrape text from The California Aggie and then analyze the text.

The Aggie is organized by category into article lists. For example, there's a Campus News list, Arts & Culture list, and Sports list. Notice that each list has multiple pages, with a maximum of 15 articles per page.

The goal of exercises 1.1 - 1.3 is to scrape articles from the Aggie for analysis in exercise 1.4.

Exercise 1.1. Write a function that extracts all of the links to articles in an Aggie article list. The function should:

  • Have a parameter url for the URL of the article list.

  • Have a parameter page for the number of pages to fetch links from. The default should be 1.

  • Return a list of aricle URLs (each URL should be a string).

Test your function on 2-3 different categories to make sure it works.

Hints:

  • Be polite to The Aggie and save time by setting up requests_cache before you write your function.

  • Start by getting your function to work for just 1 page. Once that works, have your function call itself to get additional pages.

  • You can use lxml.html or BeautifulSoup to scrape HTML. Choose one and use it throughout the entire assignment.

In [18]:
import requests
#import requests_ftp
import requests_cache
import lxml
from bs4 import BeautifulSoup
from collections import Counter
from matplotlib import pyplot as plt
import pandas as pd
plt.style.use('ggplot')
requests_cache.install_cache('coll_cache')
%matplotlib inline
In [25]:
def articles(url, page = 1):
    result = list()
    for currPage in range(1,page + 1):
        p = 'page/%d/' % currPage
        currUrl = requests.get(url+p)
        clim = BeautifulSoup(currUrl.text, "lxml")
        for article in clim.find_all('article'):
            link = article.find_all('a')
            result.append(link[0].get('href'))
    return result
In [33]:
articles("https://theaggie.org/city/", 4)
Out[33]:
['https://theaggie.org/2017/02/26/police-logs-13/',
 'https://theaggie.org/2017/02/26/news-in-brief-ali-baba-participates-in-nationwide-boycott/',
 'https://theaggie.org/2017/02/23/davis-whole-foods-market-shuts-down/',
 'https://theaggie.org/2017/02/23/protest-against-planned-parenthood-in-woodland-is-met-with-counter-protests/',
 'https://theaggie.org/2017/02/23/daviss-historic-city-hall-building-to-be-put-up-for-sale/',
 'https://theaggie.org/2017/02/21/davis-stands-with-muslim-residents/',
 'https://theaggie.org/2017/02/20/city-of-davis-awarded-funds-for-new-recycling-bins/',
 'https://theaggie.org/2017/02/19/police-logs-12/',
 'https://theaggie.org/2017/02/16/city-of-davis-to-retain-sanctuary-city-status/',
 'https://theaggie.org/2017/02/15/suspect-in-davis-islamic-center-vandalism-arrested/',
 'https://theaggie.org/2017/02/14/project-toto-aims-to-address-questions-regarding-city-finances/',
 'https://theaggie.org/2017/02/13/police-logs-11/',
 'https://theaggie.org/2017/02/12/news-in-brief-a-valentines-day-for-everybody/',
 'https://theaggie.org/2017/02/06/the-musical-train-to-memory-lane/',
 'https://theaggie.org/2017/02/05/police-logs-10/',
 'https://theaggie.org/2017/02/05/davis-owls-face-eviction-at-marriott-residence-inn/',
 'https://theaggie.org/2017/02/02/davis-celebrates-mlk-day/',
 'https://theaggie.org/2017/02/02/local-author-cyclist-rides-2300-miles/',
 'https://theaggie.org/2017/01/31/city-leaders-address-panhandling-issue-in-davis/',
 'https://theaggie.org/2017/01/30/toastmasters-help-members-conquer-fear-of-public-speaking/',
 'https://theaggie.org/2017/01/30/sacramentos-new-public-transportation/',
 'https://theaggie.org/2017/01/29/a-symphony-to-childrens-ears/',
 'https://theaggie.org/2017/01/29/yolo-countys-poverty-rate-higher-than-before-recession/',
 'https://theaggie.org/2017/01/26/healing-the-mind-body-and-soul/',
 'https://theaggie.org/2017/01/26/police-logs-9/',
 'https://theaggie.org/2017/01/26/thousands-gather-at-sacramento-capitol-building-for-womens-rights/',
 'https://theaggie.org/2017/01/25/islamic-center-of-davis-victim-of-hate-crime/',
 'https://theaggie.org/2017/01/24/love-laundry-accommodates-student-schedules-with-extended-hours/',
 'https://theaggie.org/2017/01/23/yolo-county-farm-bureau-to-honor-local-winery/',
 'https://theaggie.org/2017/01/22/chinook-salmon-spawning-in-record-numbers-in-putah-creek/',
 'https://theaggie.org/2017/01/20/helping-the-homeless/',
 'https://theaggie.org/2017/01/20/police-logs-8/',
 'https://theaggie.org/2017/01/17/no-such-thing-as-too-much-thai-food/',
 'https://theaggie.org/2017/01/16/a-dog-named-disney-wins-grant-money-for-rotts-of-friends/',
 'https://theaggie.org/2017/01/15/yolo-county-library-materials-to-be-more-widely-available/',
 'https://theaggie.org/2017/01/12/police-logs-7/',
 'https://theaggie.org/2017/01/12/neighbors-unite/',
 'https://theaggie.org/2016/12/09/sparks-fly-in-light-the-fire/',
 'https://theaggie.org/2016/12/09/bike-campaign-offers-bicycles-to-those-who-cannot-afford-them/',
 'https://theaggie.org/2016/12/08/bail-reform-advocates-gather-at-annual-fundraiser/',
 'https://theaggie.org/2016/12/07/twas-the-night-before-christmas-in-old-sacramento/',
 'https://theaggie.org/2016/12/05/childrens-candlelight-parade-lights-up-downtown-davis/',
 'https://theaggie.org/2016/12/04/police-logs-6/',
 'https://theaggie.org/2016/12/04/the-season-of-giving/',
 'https://theaggie.org/2016/12/02/nodapl-protest-erupts-in-downtown-davis/',
 'https://theaggie.org/2016/12/02/davis-turkey-trot-more-than-just-another-race/',
 'https://theaggie.org/2016/11/30/affordable-clean-green-energy-is-coming-to-yolo-county-davis/',
 'https://theaggie.org/2016/11/29/measure-h-passes-voters-support-davis-schools/',
 'https://theaggie.org/2016/11/29/local-residents-attend-davis-town-hall-meeting/',
 'https://theaggie.org/2016/11/27/police-logs-5/',
 'https://theaggie.org/2016/11/27/public-piano-destroyed-in-act-of-vandalism/',
 'https://theaggie.org/2016/11/27/holding-the-light/',
 'https://theaggie.org/2016/11/22/sun-down-bike-lights-out/',
 'https://theaggie.org/2016/11/22/police-logs-4/',
 'https://theaggie.org/2016/11/21/tune-in-to-watermelon-musics-strings-for-food-drive/',
 'https://theaggie.org/2016/11/20/water-is-sacred-water-is-life/',
 'https://theaggie.org/2016/11/17/the-yolo-food-bank-addresses-food-insecurity/',
 'https://theaggie.org/2016/11/17/nov-8-2016-an-election-day-many-may-never-forget/',
 'https://theaggie.org/2016/11/15/police-logs-3/',
 'https://theaggie.org/2016/11/15/yolo-food-banks-eighth-annual-running-of-the-turkeys-fun-runwalk/']

Exercise 1.2. Write a function that extracts the title, text, and author of an Aggie article. The function should:

  • Have a parameter url for the URL of the article.

  • For the author, extract the "Written By" line that appears at the end of most articles. You don't have to extract the author's name from this line.

  • Return a dictionary with keys "url", "title", "text", and "author". The values for these should be the article url, title, text, and author, respectively.

For example, for this article your function should return something similar to this:

{
    'author': u'Written By: Bianca Antunez \xa0\u2014\xa0city@theaggie.org',
    'text': u'Davis residents create financial model to make city\'s financial state more transparent To increase transparency between the city\'s financial situation and the community, three residents created a model called Project Toto which aims to improve how the city communicates its finances in an easily accessible design. Jeff Miller and Matt Williams, who are members of Davis\' Finance and Budget Commission, joined together with Davis entrepreneur Bob Fung to create the model plan to bring the project to the Finance and Budget Commission in February, according to Kelly Stachowicz, assistant city manager. "City staff appreciate the efforts that have gone into this, and the interest in trying to look at the city\'s potential financial position over the long term," Stachowicz said in an email interview. "We all have a shared goal to plan for a sound fiscal future with few surprises. We believe the Project Toto effort will mesh well with our other efforts as we build the budget for the next fiscal year and beyond." Project Toto complements the city\'s effort to amplify the transparency of city decisions to community members. The aim is to increase the understanding about the city\'s financial situation and make the information more accessible and easier to understand. The project is mostly a tool for public education, but can also make predictions about potential decisions regarding the city\'s financial future. Once completed, the program will allow residents to manipulate variables to see their eventual consequences, such as tax increases or extensions and proposed developments "This really isn\'t a budget, it is a forecast to see the intervention of these decisions," Williams said in an interview with The Davis Enterprise. "What happens if we extend the sales tax? What does it do given the other numbers that are in?" Project Toto enables users, whether it be a curious Davis resident, a concerned community member or a city leader, with the ability to project city finances with differing variables. The online program consists of the 400-page city budget for the 2016-2017 fiscal year, the previous budget, staff reports and consultant analyses. All of the documents are cited and accessible to the public within Project Toto. "It\'s a model that very easily lends itself to visual representation," Mayor Robb Davis said. "You can see the impacts of decisions the council makes on the fiscal health of the city." Complementary to this program, there is also a more advanced version of the model with more in-depth analyses of the city\'s finances. However, for an easy-to-understand, simplistic overview, Project Toto should be enough to help residents comprehend Davis finances. There is still more to do on the project, but its creators are hard at work trying to finalize it before the 2017-2018 fiscal year budget. "It\'s something I have been very much supportive of," Davis said. "Transparency is not just something that I have been supportive of but something we have stated as a city council objective [ ] this fits very well with our attempt to inform the public of our challenges with our fiscal situation." ',
    'title': 'Project Toto aims to address questions regarding city finances',
    'url': 'https://theaggie.org/2017/02/14/project-toto-aims-to-address-questions-regarding-city-finances/'
}

Hints:

  • The author line is always the last line of the last paragraph.

  • Python 2 displays some Unicode characters as \uXXXX. For instance, \u201c is a left-facing quotation mark. You can convert most of these to ASCII characters with the method call (on a string)

    .translate({ 0x2018:0x27, 0x2019:0x27, 0x201C:0x22, 0x201D:0x22, 0x2026:0x20 })

    If you're curious about these characters, you can look them up on this page, or read more about what Unicode is.

In [66]:
def extractor(url):
    climreq = requests.get(url)
    climhtml = climreq.text
    clim = BeautifulSoup(climhtml, "lxml")
    links = clim.find_all('p')
    author = links[-2].getText()
    links = clim.find_all('p')
    l = []
    for i in range(0,len(links)-2):
        l.append(links[i].getText())
    text = unicode.join(u'\n',map(unicode,l))
    #text = map(lambda s: s.strip(), text)
    links = clim.find_all('h1', {'class': ['entry-title']})
    title = links[0].getText()
    contents = {"author": author, 'text':text, 'title':title, 'url':url}
    return contents
extractor("https://theaggie.org/2017/02/24/2017-winter-quarter-election-results/")
Out[66]:
{'author': u'Written by: Alyssa Vandenberg \xa0\u2014 campus@theaggie.org',
 'text': u'Six senators, new executive team elected\nCurrent ASUCD Vice President Abhay Sandhu announced the ASUCD election results on Feb. 24 in the Memorial Union\u2019s Mee room. Six senators were elected: Sam Chiang, Michael Gofman, Khadeja Ibrahim, Rahi Suryawanshi, Marcos Rodriguez and Yajaira Ramirez Sigala. Chiang and Ibrahim ran on the BASED slate, while Suryawanshi, Rodriguez and Ramirez Sigala ran on the Bespoke slate. Gofman ran independently.\nThe new ASUCD president and vice president will be Josh Dalavai and Adilla Jamaludin. Dalavai and Jamaludin ran on the BASED slate. \nThe results will also be posted online at elections.ucdavis.edu.',
 'title': u'2017 Winter Quarter election results',
 'url': 'https://theaggie.org/2017/02/24/2017-winter-quarter-election-results/'}

Exercise 1.3. Use your functions from exercises 1.1 and 1.2 to get a data frame of 60 Campus News articles and a data frame of 60 City News articles. Add a column to each that indicates the category, then combine them into one big data frame.

The "text" column of this data frame will be your corpus for natural language processing in exercise 1.4.

In [34]:
def letsMakeSomeDataFrames(url, category):
    l = articles(url, 4)
    columns = ['url', 'text', 'title', 'author']
    df = pd.DataFrame()
    for i in l:
        x = extractor(i)
        x['category'] = category
        df = df.append(x, ignore_index = True)
    return df
    
In [69]:
X = extractor("https://theaggie.org/2017/02/24/2017-winter-quarter-election-results/")
type(X)
Out[69]:
dict
In [35]:
campus = letsMakeSomeDataFrames("https://theaggie.org/campus/", "campus")
city = letsMakeSomeDataFrames("https://theaggie.org/city/", "city")
biggy = campus.append(city)
In [36]:
biggy = biggy.reset_index()
del biggy['index']
biggy
Out[36]:
author category text title url
0 Written by: Alyssa Vandenberg  — campus@theagg... campus Shaheen’s name to remain on ballot, his votes ... Senate candidate Zaki Shaheen withdraws from race https://theaggie.org/2017/02/22/senate-candida...
1 Written by: Aaron Liss  — campus@theaggie.org campus Students receive email warnings from UC Davis ... UC Davis experiences several recent hate-based... https://theaggie.org/2017/02/21/uc-davis-exper...
2 Written by: Alyssa Vandenberg  — campus@theagg... campus UC Board of Regents to vote on the appointment... UC President selects Gary May as new UC Davis ... https://theaggie.org/2017/02/21/uc-president-s...
3 The UC Davis and UC Office of the President’s ... campus Tighter policies require greater approval of o... Katehi controversy prompts decline of UC admin... https://theaggie.org/2017/02/20/katehi-controv...
4 Written by: Ivan Valenzuela  — campus@theaggie... campus SR #7 asks university to increase capacity for... ASUCD Senate passes resolution submitting comm... https://theaggie.org/2017/02/20/asucd-senate-p...
5 More information regarding the 2016 Annual Rep... campus UC Davis leads in sustainability with largest ... UC releases 2016 Annual Report on Sustainable ... https://theaggie.org/2017/02/20/uc-releases-20...
6 “On a personal note, as in immigrant to this c... campus Speakers, including Interim Chancellor Ralph J... UC Davis Global Affairs holds discussion on Pr... https://theaggie.org/2017/02/19/uc-davis-globa...
7 “While maintaining the security of the nation’... campus Executive order has immediate consequences for... Trump’s immigration ban affects UC Davis commu... https://theaggie.org/2017/02/19/trumps-immigra...
8 “Trump’s corporate colonial cabinet is hard at... campus Student protesters march from MU flagpole to M... UC Davis students participate in UC-wide #NoDA... https://theaggie.org/2017/02/17/uc-davis-stude...
9 Written by: Jayashri Padmanabhan — campus@thea... campus Conference entails full day of speakers, panel... UC Davis holds first mental health conference https://theaggie.org/2017/02/17/uc-davis-holds...
10 The meeting adjourned at 6:59 p.m. \nWritten b... campus Last week in Senate\nThe ASUCD Senate meeting ... Last week in Senate https://theaggie.org/2017/02/16/last-week-in-s...
11 Written by: Alyssa Vandenberg and Emilie DeFaz... campus Executive: Josh Dalavai and Adilla Jamaludin\n... 2017 ASUCD Winter Elections — Meet the Candidates https://theaggie.org/2017/02/16/2017-asucd-win...
12 Written by: Ivan Valenzuela — campus@theaggie.org campus New showcase provides opportunity for students... Shields Library hosts new exhibit for Davis ce... https://theaggie.org/2017/02/14/shields-librar...
13 This is the second time the SCHS has hosted th... campus Students promote fruit and vegetable meals via... Student Health and Counseling Services hosts “... https://theaggie.org/2017/02/14/student-health...
14 “I’d think twice about taking a class,” Stucke... campus New fees to pay for equipment replacement\nTo ... PE classes may charge additional fees https://theaggie.org/2017/02/13/pe-classes-may...
15 Written by: Jayashri Padmanabhan — campus@thea... campus Conference entails full day of speakers, panel... UC Davis holds first mental health conference https://theaggie.org/2017/02/17/uc-davis-holds...
16 The meeting adjourned at 6:59 p.m. \nWritten b... campus Last week in Senate\nThe ASUCD Senate meeting ... Last week in Senate https://theaggie.org/2017/02/16/last-week-in-s...
17 Written by: Alyssa Vandenberg and Emilie DeFaz... campus Executive: Josh Dalavai and Adilla Jamaludin\n... 2017 ASUCD Winter Elections — Meet the Candidates https://theaggie.org/2017/02/16/2017-asucd-win...
18 Written by: Ivan Valenzuela — campus@theaggie.org campus New showcase provides opportunity for students... Shields Library hosts new exhibit for Davis ce... https://theaggie.org/2017/02/14/shields-librar...
19 This is the second time the SCHS has hosted th... campus Students promote fruit and vegetable meals via... Student Health and Counseling Services hosts “... https://theaggie.org/2017/02/14/student-health...
20 “I’d think twice about taking a class,” Stucke... campus New fees to pay for equipment replacement\nTo ... PE classes may charge additional fees https://theaggie.org/2017/02/13/pe-classes-may...
21 A reception is planned for the spring, where f... campus Recipients each rewarded $25,000 for research\... 11 new Chancellor Fellows honored for 2016 https://theaggie.org/2017/02/12/11-new-chancel...
22 Latheef invites and encourages non-Muslims int... campus Muslim Student Association curates five-part D... Muslim students respond to recent political ev... https://theaggie.org/2017/02/12/muslim-student...
23 According to Chloe Tsudama, a fourth-year psyc... campus Events to promote safe sex\nOn Feb. 1, Student... Sexcessful Campaign launched in time for Valen... https://theaggie.org/2017/02/12/sexcessful-cam...
24 campus Chan replaces former senator Sam Park\nMichael... Michael Chan sworn in as interim senator https://theaggie.org/2017/02/10/michael-chan-s...
25 The UC Regents also voted to approve a new pro... campus Regents approve tuition increase in 16-4 vote\... University of California Regents meet, approve... https://theaggie.org/2017/02/09/university-of-...
26 Committee Amendment No. 50 was carried in a vo... campus Last week in Senate\nThe ASUCD Senate meeting ... Last week in Senate https://theaggie.org/2017/02/09/last-week-in-s...
27 AB 2664-funded programs will create opportunit... campus Funding to expand innovation, entrepreneurship... UC Davis receives $2.2 million from Assembly B... https://theaggie.org/2017/02/09/uc-davis-recei...
28 According to Nash, the event provided a space ... campus Davis College Democrats host Dodd for question... Senator Bill Dodd visits UC Davis https://theaggie.org/2017/02/06/senator-bill-d...
29 Apart from not applying retroactively, there a... campus Law to affect students selected to attend Nati... AB 1887 prevents use of state funds, including... https://theaggie.org/2017/02/05/ab-1887-preven...
... ... ... ... ... ...
90 city Grant from Sutter Health supports Davis Pathwa... Helping the homeless https://theaggie.org/2017/01/20/helping-the-ho...
91 city From drunkards to eavesdroppers, Davis residen... Police Logs https://theaggie.org/2017/01/20/police-logs-8/
92 city Downtown Davis offers a wide range of Thai cui... No such thing as too much Thai food https://theaggie.org/2017/01/17/no-such-thing-...
93 With the $10,000 grant, Rotts of Friends plans... city Petco Foundation awards $10,000 to local anima... A dog named Disney wins grant money for Rotts ... https://theaggie.org/2017/01/16/a-dog-named-di...
94 Books by Mail has been made possible by an Ins... city Books by Mail program will deliver materials s... Yolo County Library materials to be more widel... https://theaggie.org/2017/01/15/yolo-county-li...
95 Dec. 29 city Season’s Greetings Edition\nDec. 24\n“Downstai... Police Logs https://theaggie.org/2017/01/12/police-logs-7/
96 Neighbors contributed by donating the initial ... city The Davis Manor Neighborhood hosts first Holid... Neighbors unite https://theaggie.org/2017/01/12/neighbors-unite/
97 “It was a great pleasure to read this book,” s... city Local author Matt Biers-Ariel’s latest novel\n... Sparks fly in Light the Fire https://theaggie.org/2016/12/09/sparks-fly-in-...
98 General information about The Bike Campaign ca... city Volunteer program distributes bicycles, aims t... Bike Campaign offers bicycles to those who can... https://theaggie.org/2016/12/09/bike-campaign-...
99 “We’re […] seeing […] a huge push to change th... city Speakers criticize “unconstitutional” system\n... Bail reform advocates gather at annual fundraiser https://theaggie.org/2016/12/08/bail-reform-ad...
100 Find out more about the Theatre of Lights at t... city Live-action retelling of Christmas poem promis... ‘Twas the Night Before Christmas in Old Sacram... https://theaggie.org/2016/12/07/twas-the-night...
101 city 35th annual tree lighting ceremony kicks off w... Children’s Candlelight Parade lights up downto... https://theaggie.org/2016/12/05/childrens-cand...
102 city Another week of ‘why did people call the polic... Police Logs https://theaggie.org/2016/12/04/police-logs-6/
103 city The Yolo County Children’s Alliance celebrates... The season of giving https://theaggie.org/2016/12/04/the-season-of-...
104 “Now, therefore, be it resolved that the City ... city Students, activists call for solidarity with S... NoDAPL protest erupts in downtown Davis https://theaggie.org/2016/12/02/nodapl-protest...
105 For more information on the Turkey Trot result... city Participants gathered for 5k, 10k, half marath... Davis Turkey Trot: more than just another race https://theaggie.org/2016/12/02/davis-turkey-t...
106 In addition, the program is expected to increa... city Citizens can expect more cost-competitive clea... Affordable, clean, green energy is coming to Y... https://theaggie.org/2016/11/30/affordable-cle...
107 city Davis community renews local parcel tax for K-... Measure H passes, voters support Davis schools https://theaggie.org/2016/11/29/measure-h-pass...
108 city College alcohol use, high-risk drinking discus... Local residents attend Davis town hall meeting https://theaggie.org/2016/11/29/local-resident...
109 city Looks like it’s been an interesting week\nNov.... Police Logs https://theaggie.org/2016/11/27/police-logs-5/
110 city Ornamental piano outside Mishka’s Café vandali... Public piano destroyed in act of vandalism https://theaggie.org/2016/11/27/public-piano-d...
111 city Davis residents light candles, promote sanctua... Holding the Light https://theaggie.org/2016/11/27/holding-the-li...
112 Fewer hours of daylight should not get in the... city Don’t be left in the dark now that Daylight Sa... Sun down, bike lights out https://theaggie.org/2016/11/22/sun-down-bike-...
113 “Fairly large tumbleweed blocking right lane” city Nov. 7\n“Subject stated our pizza is ready and... Police Logs https://theaggie.org/2016/11/22/police-logs-4/
114 city Helping the community through Davis Community ... Tune in to Watermelon Music’s strings-for-food... https://theaggie.org/2016/11/21/tune-in-to-wat...
115 city Protesters gather at State Capitol Building fo... Water is sacred, water is life https://theaggie.org/2016/11/20/water-is-sacre...
116 “We have generous shoppers, and we have the mo... city The Yolo County community comes together for h... The Yolo Food Bank addresses food insecurity https://theaggie.org/2016/11/17/the-yolo-food-...
117 city Election results are in; Davis community conce... Nov. 8 2016: An Election Day many may never fo... https://theaggie.org/2016/11/17/nov-8-2016-an-...
118 Nov. 4 city More turkeys, more tomfoolery, more accidental... Police Logs https://theaggie.org/2016/11/15/police-logs-3/
119 city Participants line up for Thanksgiving 5k befor... Yolo Food Bank’s eighth Annual Running of the ... https://theaggie.org/2016/11/15/yolo-food-bank...

120 rows × 5 columns

Exercise 1.4. Use the Aggie corpus to answer the following questions. Use plots to support your analysis.

  • What topics does the Aggie cover the most? Do city articles typically cover different topics than campus articles?

  • What are the titles of the top 3 pairs of most similar articles? Examine each pair of articles. What words do they have in common?

  • Do you think this corpus is representative of the Aggie? Why or why not? What kinds of inference can this corpus support? Explain your reasoning.

Hints:

  • The nltk book and scikit-learn documentation may be helpful here.

  • You can determine whether city articles are "near" campus articles from the similarity matrix or with k-nearest neighbors.

  • If you want, you can use the wordcloud package to plot a word cloud. To install the package, run

    conda install -c https://conda.anaconda.org/amueller wordcloud

    in a terminal. Word clouds look nice and are easy to read, but are less precise than bar plots.

In [41]:
import nltk
import numpy as np
from wordcloud import WordCloud
from nltk import corpus
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.neighbors import NearestNeighbors
In [49]:
cloud = WordCloud(max_font_size=40).generate(''.join(biggy['text']))
plt.imshow(cloud)
plt.axis("off")
plt.show()

cloud2 = WordCloud(max_font_size=40).generate(''.join(biggy['text'][0:59]))
plt.imshow(cloud2)
plt.axis("off")
plt.show()

cloud3 = WordCloud(max_font_size=40).generate(''.join(biggy['text'][60:119]))
plt.imshow(cloud3)
plt.axis("off")
plt.show()
In [52]:
stemmer = PorterStemmer().stem

tokenize = nltk.word_tokenize

def stem(tokens,stemmer = PorterStemmer().stem):
    return [stemmer(w.lower()) for w in tokens]

def lemmatize(text):
    return stem(tokenize(text))

textd = {} #dictionary from lemmata to document ids containing that lemma
for fid in biggy['text']:
    t = fid
    s = set(lemmatize(t))
    try:
        toks = toks | s
    except NameError:
        toks = s
    for tok in s:
        try:
            textd[tok].append(fid)
        except KeyError:
            textd[tok] = [fid]

docids = {} #dictionary of the document id to an integer id for the document
N = len(biggy['text'])
for i in xrange(N):
    docids[biggy['text'][i]] = i
    
tokids = {} #dictionary of lemma to integer id for the lemma
tok_list = list(toks)
m = len(tok_list)
for j in xrange(m):
    tokids[tok_list[j]] = j
In [53]:
numd = {key:len(set(val)) for key,val in textd.items()}
logN = np.log(len(biggy['text']))
idf_smooth = {key:logN - np.log(1 + val) for key, val in numd.items() if val > 1}
idf_smooth
Out[53]:
{u'1,800': 3.6888794541139358,
 u'hatr': 3.401197381662155,
 u'four': 2.3025850929940455,
 u'protest': 2.0149030205422647,
 u'sleep': 3.6888794541139358,
 u'asian': 3.401197381662155,
 u'president\u2019': 3.6888794541139358,
 u'oldest': 3.6888794541139358,
 u'hate': 2.3025850929940455,
 u'whose': 3.401197381662155,
 u'saylor': 3.6888794541139358,
 u'accur': 3.6888794541139358,
 u'bike': 2.5902671654458262,
 u'certain': 2.4849066497879999,
 u'disney\u2019': 3.6888794541139358,
 u'under': 1.8430527636156055,
 u'worth': 3.6888794541139358,
 u'@': 3.6888794541139358,
 u'point': 2.8415815937267324,
 u'risk': 2.7080502011022101,
 u'compassion': 3.6888794541139358,
 u'blanket': 3.6888794541139358,
 u'rise': 3.1780538303479453,
 u'voic': 2.4849066497879999,
 u'tenni': 3.6888794541139358,
 u'georgia': 3.6888794541139358,
 u'jack': 3.6888794541139358,
 u'unitran': 3.6888794541139358,
 u'govern': 2.0794415416798357,
 u'jacob': 3.6888794541139358,
 u'affect': 2.0794415416798357,
 u'emili': 3.6888794541139358,
 u'school': 1.3535045382968995,
 u'scholar': 3.1780538303479453,
 u'\u2013\u2013': 3.1780538303479453,
 u'today\u2019': 3.6888794541139358,
 u'environmentally-friendli': 3.6888794541139358,
 u'\u201cit': 1.5686159179138452,
 u'plaza': 3.1780538303479453,
 u'charter': 3.6888794541139358,
 u'speci': 3.401197381662155,
 u'miller': 3.6888794541139358,
 u'bacon': 2.9957322735539909,
 u'request': 2.5902671654458262,
 u'histor': 2.4849066497879999,
 u'consequ': 2.5902671654458262,
 u'second': 2.0794415416798357,
 u'diseas': 2.9957322735539909,
 u'\u201creport': 3.6888794541139358,
 u'gorman': 3.6888794541139358,
 u'\u201cin': 2.0149030205422647,
 u'nahabedian': 3.6888794541139358,
 u'illumin': 3.6888794541139358,
 u'even': 1.6519975268528961,
 u'employ': 3.401197381662155,
 u'dialogu': 3.401197381662155,
 u'biolog': 2.5902671654458262,
 u'issues.\u201d': 3.6888794541139358,
 u'neg': 2.7080502011022101,
 u'near': 3.1780538303479453,
 u'\u201cpeopl': 3.401197381662155,
 u'fossil': 2.9957322735539909,
 u'dr.': 3.401197381662155,
 u'new': 0.85566611005772009,
 u'net': 3.6888794541139358,
 u'ever': 2.5902671654458262,
 u'told': 2.9957322735539909,
 u'mee': 3.6888794541139358,
 u'specialist': 2.9957322735539909,
 u'intellectu': 3.6888794541139358,
 u'behavior': 2.9957322735539909,
 u'men': 3.1780538303479453,
 u'here': 1.6964492894237297,
 u'met': 2.8415815937267324,
 u'aftermath': 3.6888794541139358,
 u'100': 2.5902671654458262,
 u'enrol': 2.9957322735539909,
 u'headlin': 3.6888794541139358,
 u'luther': 3.6888794541139358,
 u'daughter': 2.8415815937267324,
 u'forum': 3.1780538303479453,
 u'campus.\u201d': 2.5902671654458262,
 u'student-faculti': 3.6888794541139358,
 u'jame': 3.6888794541139358,
 u'harass': 3.1780538303479453,
 u'mentor': 3.1780538303479453,
 u'studi': 1.6519975268528961,
 u'controversi': 2.7080502011022101,
 u'counti': 1.7429693050586228,
 u'imposs': 3.6888794541139358,
 u'volunt': 2.3895964699836751,
 u'katehi': 3.1780538303479453,
 u'brought': 2.3895964699836751,
 u'attitud': 3.6888794541139358,
 u'scientif': 3.6888794541139358,
 u'total': 2.3025850929940455,
 u'unit': 1.9542783987258296,
 u'highli': 3.1780538303479453,
 u'sarah': 2.9957322735539909,
 u'dna': 3.6888794541139358,
 u'spoke': 2.1484344131667874,
 u'would': 1.0498221244986774,
 u'program': 1.1239300966523995,
 u'music': 2.4849066497879999,
 u'asset': 3.6888794541139358,
 u'armi': 3.6888794541139358,
 u'type': 2.5902671654458262,
 u'until': 2.2225423853205091,
 u'adela': 3.6888794541139358,
 u'relat': 1.5686159179138452,
 u'officio': 3.6888794541139358,
 u'notic': 2.4849066497879999,
 u'hurt': 3.401197381662155,
 u'warn': 3.401197381662155,
 u'phone': 2.7080502011022101,
 u'warm': 3.1780538303479453,
 u'adult': 3.401197381662155,
 u'90': 3.1780538303479453,
 u'hole': 3.6888794541139358,
 u'hold': 2.2225423853205091,
 u'we\u2019ll': 3.6888794541139358,
 u'pantri': 3.401197381662155,
 u'must': 2.0149030205422647,
 u'me': 1.8430527636156055,
 u'irvin': 3.6888794541139358,
 u'word': 2.7080502011022101,
 u'wore': 3.6888794541139358,
 u'1997': 3.6888794541139358,
 u'1996': 3.6888794541139358,
 u'work': 0.70995429887632611,
 u'mu': 3.6888794541139358,
 u'wors': 3.6888794541139358,
 u'endow': 3.6888794541139358,
 u'advocaci': 2.5902671654458262,
 u'climat': 2.0794415416798357,
 u'give': 1.4916548767777167,
 u'household': 3.401197381662155,
 u'services.\u201d': 3.6888794541139358,
 u'indic': 3.401197381662155,
 u'10,000': 3.1780538303479453,
 u'woman': 3.6888794541139358,
 u'want': 1.1499055830556602,
 u'2.2': 3.6888794541139358,
 u'everi': 1.5686159179138452,
 u'david': 3.1780538303479453,
 u'attract': 2.8415815937267324,
 u'motion': 3.1780538303479453,
 u'end': 2.0149030205422647,
 u'community.\u201d': 2.8415815937267324,
 u'polic': 2.2225423853205091,
 u'travel': 2.3025850929940455,
 u'that.\u201d': 2.9957322735539909,
 u'ceremoni': 3.6888794541139358,
 u'how': 1.2321436812926323,
 u'recoveri': 3.6888794541139358,
 u'interview': 2.1484344131667874,
 u'disappoint': 3.401197381662155,
 u'perspect': 2.8415815937267324,
 u'updat': 3.6888794541139358,
 u'grown': 3.6888794541139358,
 u'shaitaj': 2.9957322735539909,
 u'recogn': 2.4849066497879999,
 u'after': 1.0262916270884834,
 u'lab': 3.1780538303479453,
 u'befor': 1.5686159179138452,
 u'beauti': 2.9957322735539909,
 u'law': 2.0794415416798357,
 u'demonstr': 2.5902671654458262,
 u'memori': 2.4849066497879999,
 u'attempt': 2.9957322735539909,
 u'sioux': 3.6888794541139358,
 u'third': 3.6888794541139358,
 u'think': 1.1499055830556602,
 u'greek': 3.6888794541139358,
 u'maintain': 2.3025850929940455,
 u'green': 2.9957322735539909,
 u'south': 3.401197381662155,
 u'incorpor': 3.401197381662155,
 u'enter': 2.7080502011022101,
 u'fan': 3.6888794541139358,
 u'nodapl': 3.6888794541139358,
 u'order': 1.3535045382968995,
 u'wind': 3.6888794541139358,
 u'wine': 3.6888794541139358,
 u'oper': 2.4849066497879999,
 u'\u201cdavi': 2.9957322735539909,
 u'consent': 3.401197381662155,
 u'offici': 2.2225423853205091,
 u'failur': 3.6888794541139358,
 u'becaus': 0.89567144467141935,
 u'incid': 2.3895964699836751,
 u'appar': 3.6888794541139358,
 u'mayor': 1.8430527636156055,
 u'fit': 2.9957322735539909,
 u'fix': 3.6888794541139358,
 u',': 0.077961541469711904,
 u'better': 2.1484344131667874,
 u'offic': 1.4916548767777167,
 u'comprehens': 3.401197381662155,
 u'easier': 3.6888794541139358,
 u'then': 1.3535045382968995,
 u'them': 1.1499055830556602,
 u'anim': 3.1780538303479453,
 u'proce': 3.6888794541139358,
 u'enrich': 3.1780538303479453,
 u'safe': 2.3895964699836751,
 u'break': 2.8415815937267324,
 u'promis': 3.401197381662155,
 u'they': 0.53899650073268646,
 u'battl': 3.6888794541139358,
 u'one': 0.62860865942237432,
 u'silver': 3.401197381662155,
 u'bank': 3.6888794541139358,
 u'prize': 3.6888794541139358,
 u'choic': 2.7080502011022101,
 u'alex': 2.7080502011022101,
 u'meat': 3.401197381662155,
 u'accommod': 3.1780538303479453,
 u'anastasia': 3.6888794541139358,
 u'luca': 3.6888794541139358,
 u'each': 1.5293952047605637,
 u'went': 2.0794415416798357,
 u'side': 2.9957322735539909,
 u'bond': 2.9957322735539909,
 u'healthcar': 3.401197381662155,
 u'resum': 3.6888794541139358,
 u'oppos': 3.1780538303479453,
 u'taught': 2.9957322735539909,
 u'2,500': 3.6888794541139358,
 u'logo': 3.6888794541139358,
 u'commiss': 2.2225423853205091,
 u'saturday': 3.401197381662155,
 u'rp': 3.6888794541139358,
 u'network': 2.5902671654458262,
 u'goe': 2.7080502011022101,
 u'facil': 2.5902671654458262,
 u'crucial': 3.6888794541139358,
 u'resources.\u201d': 3.6888794541139358,
 u'content': 3.1780538303479453,
 u'laid': 3.1780538303479453,
 u'daniel': 3.401197381662155,
 u'adapt': 3.401197381662155,
 u'got': 2.5902671654458262,
 u'forth': 3.6888794541139358,
 u'twice': 3.401197381662155,
 u'written': 3.401197381662155,
 u'u.s.': 1.9542783987258296,
 u'ruttkay': 3.6888794541139358,
 u'situat': 2.5902671654458262,
 u'free': 1.791759469228055,
 u'standard': 2.8415815937267324,
 u'jennif': 3.401197381662155,
 u'disenfranchis': 3.6888794541139358,
 u'atm': 3.6888794541139358,
 u'moment': 3.401197381662155,
 u'renew': 2.9957322735539909,
 u'unabl': 3.6888794541139358,
 u'loud': 3.6888794541139358,
 u'enjoy': 2.1484344131667874,
 u'rang': 2.9957322735539909,
 u'nation\u2019': 3.401197381662155,
 u'grade': 3.401197381662155,
 u'dhaliw': 2.9957322735539909,
 u'independ': 3.401197381662155,
 u'wast': 3.1780538303479453,
 u'rank': 3.6888794541139358,
 u'capac': 3.1780538303479453,
 u'restrict': 3.6888794541139358,
 u'instruct': 2.8415815937267324,
 u'alreadi': 2.2225423853205091,
 u'agre': 2.8415815937267324,
 u'primari': 3.6888794541139358,
 u'katehi\u2019': 3.401197381662155,
 u'instagram': 3.6888794541139358,
 u'nomin': 3.1780538303479453,
 u'their': 0.53899650073268646,
 u'top': 2.8415815937267324,
 u'sometim': 3.1780538303479453,
 u'people\u2019': 3.401197381662155,
 u'timothi': 3.6888794541139358,
 u'necessarili': 2.9957322735539909,
 u'ton': 3.1780538303479453,
 u'too': 2.7080502011022101,
 u'similarli': 3.6888794541139358,
 u'we\u2019v': 2.5902671654458262,
 u'john': 3.401197381662155,
 u'we\u2019r': 2.1484344131667874,
 u'listen': 2.9957322735539909,
 u'growth': 3.401197381662155,
 u'segundo': 3.6888794541139358,
 u'tool': 3.1780538303479453,
 u'took': 2.1484344131667874,
 u'direct': 2.0149030205422647,
 u'legislatur': 3.6888794541139358,
 u'conserv': 2.5902671654458262,
 u'younger': 3.6888794541139358,
 u'white': 2.1484344131667874,
 u'silli': 3.6888794541139358,
 u'target': 2.9957322735539909,
 u'provid': 0.87546873735389985,
 u'tree': 3.6888794541139358,
 u'\u201cif': 2.4849066497879999,
 u'project': 1.5293952047605637,
 u'matter': 2.9957322735539909,
 u'anxieti': 3.6888794541139358,
 u'design': 1.7429693050586228,
 u'minut': 3.1780538303479453,
 u'empow': 2.7080502011022101,
 u'entail': 3.401197381662155,
 u'ran': 3.6888794541139358,
 u'modern': 3.401197381662155,
 u'mind': 3.1780538303479453,
 u'mine': 3.6888794541139358,
 u'raw': 3.6888794541139358,
 u'manner': 3.6888794541139358,
 u'seen': 2.5902671654458262,
 u'seem': 2.4849066497879999,
 u'seek': 2.2225423853205091,
 u'strength': 3.6888794541139358,
 u'recreat': 2.9957322735539909,
 u'\u201d': 0.16251892949777513,
 u'especi': 1.791759469228055,
 u'thoma': 3.6888794541139358,
 u'thoroughli': 3.6888794541139358,
 u'neurobiolog': 3.401197381662155,
 u'rider': 3.6888794541139358,
 u'predominantli': 3.401197381662155,
 u'blue': 3.401197381662155,
 u'plenti': 3.6888794541139358,
 u'though': 2.4849066497879999,
 u'russel': 3.401197381662155,
 u'object': 2.7080502011022101,
 u'what': 1.0739196760777379,
 u'marin': 3.6888794541139358,
 u'regular': 3.6888794541139358,
 u'third-year': 1.8430527636156055,
 u'letter': 2.7080502011022101,
 u'\u201calt-right\u201d': 3.6888794541139358,
 u'drought': 3.401197381662155,
 u'everyth': 2.4849066497879999,
 u'maria': 3.6888794541139358,
 u'tradit': 2.7080502011022101,
 u'don': 3.401197381662155,
 u'professor': 2.0149030205422647,
 u'camp': 3.6888794541139358,
 u'alumni': 3.401197381662155,
 u'dog': 2.9957322735539909,
 u'hospit': 2.7080502011022101,
 u'alumnu': 3.6888794541139358,
 u'declar': 2.9957322735539909,
 u'tech': 3.401197381662155,
 u'oppress': 3.6888794541139358,
 u'came': 2.0149030205422647,
 u'treati': 3.6888794541139358,
 u'hunger': 3.6888794541139358,
 u'opposit': 3.1780538303479453,
 u'regents\u2019': 3.6888794541139358,
 u'advisori': 3.6888794541139358,
 u'openli': 3.6888794541139358,
 u'inaugur': 3.1780538303479453,
 u'issue.\u201d': 3.6888794541139358,
 u'academi': 3.401197381662155,
 u'environ': 2.0794415416798357,
 u'bail': 3.401197381662155,
 u'involv': 1.6964492894237297,
 u'despit': 2.2225423853205091,
 u'acquir': 3.6888794541139358,
 u'explain': 1.791759469228055,
 u'restaur': 3.1780538303479453,
 u'lgbtqia': 3.6888794541139358,
 u'theme': 3.1780538303479453,
 u'busi': 1.5293952047605637,
 u'stephani': 3.401197381662155,
 u'department\u2019': 3.401197381662155,
 u'rice': 3.6888794541139358,
 u'do': 0.76214005204689617,
 u'yfb': 3.6888794541139358,
 u'wide': 3.401197381662155,
 u'de': 3.401197381662155,
 u'stop': 2.3895964699836751,
 u'dc': 3.6888794541139358,
 u'25,000': 3.6888794541139358,
 u'spirit': 2.9957322735539909,
 u'report': 1.6964492894237297,
 u'instructor': 3.6888794541139358,
 u'receiv': 1.3217558399823193,
 u'earn': 3.1780538303479453,
 u'bar': 3.1780538303479453,
 u'spokesperson': 3.401197381662155,
 u'politician': 3.401197381662155,
 u'them.\u201d': 3.401197381662155,
 u'indigen': 3.401197381662155,
 u'bad': 2.8415815937267324,
 u'ban': 2.9957322735539909,
 u'respond': 2.4849066497879999,
 u'human': 2.4849066497879999,
 u'fair': 3.401197381662155,
 u'ongo': 3.6888794541139358,
 u'resist': 2.9957322735539909,
 u'mandatori': 3.6888794541139358,
 u'result': 2.2225423853205091,
 u'respons': 1.7429693050586228,
 u'fail': 2.8415815937267324,
 u'themselv': 2.0149030205422647,
 u'news': 2.0794415416798357,
 u'best': 2.3895964699836751,
 u'juliet': 3.6888794541139358,
 u'awar': 2.1484344131667874,
 u'co2': 3.6888794541139358,
 u'away': 2.3025850929940455,
 u'discoveri': 3.6888794541139358,
 u'figur': 2.8415815937267324,
 u'score': 3.6888794541139358,
 u'drawn': 3.1780538303479453,
 u'approach': 2.1484344131667874,
 u'berkeley': 2.9957322735539909,
 u'attribut': 3.6888794541139358,
 u'accord': 1.0033021088637848,
 u'wa': 0.29885537304990617,
 u'adilla': 3.6888794541139358,
 u'extens': 2.9957322735539909,
 u'drew': 3.6888794541139358,
 u'intox': 3.6888794541139358,
 u'toler': 3.1780538303479453,
 u'kitchen': 3.6888794541139358,
 u'protect': 1.8430527636156055,
 u'accident': 3.6888794541139358,
 u'expos': 3.401197381662155,
 u'ill': 3.1780538303479453,
 u'against': 1.791759469228055,
 u'countri': 1.6964492894237297,
 u'compromis': 3.6888794541139358,
 u'and': 0.051293294387550148,
 u'had': 1.2039728043259359,
 u'inher': 3.401197381662155,
 u'2nd': 3.6888794541139358,
 u'path': 3.1780538303479453,
 u'250': 3.1780538303479453,
 u'guid': 2.9957322735539909,
 u'speak': 2.0149030205422647,
 u'bathroom': 3.6888794541139358,
 u'iraq': 3.6888794541139358,
 u'sam': 2.7080502011022101,
 u'union': 2.7080502011022101,
 u'three': 1.4916548767777167,
 u'been': 0.6931471805599454,
 u'.': 0.10536051565782589,
 u'revenu': 2.9957322735539909,
 u'beer': 3.6888794541139358,
 u'much': 1.8430527636156055,
 u'interest': 1.791759469228055,
 u'basic': 2.5902671654458262,
 u'quickli': 3.401197381662155,
 u'life': 1.8971199848858813,
 u'regul': 3.401197381662155,
 u'worker': 2.8415815937267324,
 u'anywher': 3.1780538303479453,
 u'child': 2.8415815937267324,
 u'165': 3.6888794541139358,
 u'davi': 0.23361485118150505,
 u'sanctuari': 3.1780538303479453,
 u'alleg': 3.401197381662155,
 u'ident': 2.8415815937267324,
 u'affirm': 3.1780538303479453,
 u'servic': 1.3217558399823193,
 u'properti': 2.4849066497879999,
 u'commerci': 3.1780538303479453,
 u'\u201cwhi': 3.6888794541139358,
 u'air': 3.1780538303479453,
 u'aim': 1.8971199848858813,
 u'calcul': 3.6888794541139358,
 u'monse': 3.6888794541139358,
 u'publicli': 3.6888794541139358,
 u'aid': 2.3895964699836751,
 u'seven': 2.3895964699836751,
 u'garag': 3.401197381662155,
 u'mexico': 3.401197381662155,
 u'is': 0.16251892949777513,
 u'it': 0.20252426411147351,
 u'ii': 3.401197381662155,
 u'in': 0.042559614418795633,
 u'\u2019': 2.5902671654458262,
 u'now.\u201d': 3.6888794541139358,
 u'sever': 1.6519975268528961,
 u'if': 1.2039728043259359,
 u'flore': 3.401197381662155,
 u'credit': 3.6888794541139358,
 u'perform': 2.2225423853205091,
 u'suggest': 2.3895964699836751,
 u'make': 0.81719982922992385,
 u'transpar': 3.401197381662155,
 u'wound': 3.6888794541139358,
 u'airport': 3.6888794541139358,
 u'dean': 3.1780538303479453,
 u'complex': 2.9957322735539909,
 u'big': 2.2225423853205091,
 u'jerri': 3.6888794541139358,
 u'complet': 2.3025850929940455,
 u'evid': 3.6888794541139358,
 u'sacramento': 1.6519975268528961,
 u'redevelop': 3.401197381662155,
 u'rain': 3.6888794541139358,
 u'hand': 3.401197381662155,
 u'fairli': 3.6888794541139358,
 u'rais': 2.3895964699836751,
 u'scale': 3.6888794541139358,
 u'\u201cstudent': 3.6888794541139358,
 u'kid': 3.1780538303479453,
 u'kept': 3.6888794541139358,
 u'thu': 3.1780538303479453,
 u'1970': 3.6888794541139358,
 u'contact': 2.4849066497879999,
 u'shortli': 3.6888794541139358,
 u'thi': 0.28768207245178079,
 u'the': 0.042559614418795633,
 u'campu': 0.93734414107198738,
 u'legisl': 2.4849066497879999,
 u'left': 2.4849066497879999,
 u'identifi': 2.8415815937267324,
 u'capitol': 2.9957322735539909,
 u'just': 0.89567144467141935,
 u'photo': 2.9957322735539909,
 u'victim': 3.401197381662155,
 u'yet': 2.7080502011022101,
 u'languag': 3.1780538303479453,
 u'previous': 2.5902671654458262,
 u'campaign': 2.0794415416798357,
 u'easi': 3.6888794541139358,
 u'josh': 3.6888794541139358,
 u'spread': 2.9957322735539909,
 u'board': 2.1484344131667874,
 u'5k': 3.401197381662155,
 u'prison': 3.401197381662155,
 u'els': 2.9957322735539909,
 u'east': 2.9957322735539909,
 u'hat': 3.6888794541139358,
 u'gave': 2.5902671654458262,
 u'applic': 2.4849066497879999,
 u'mayb': 3.401197381662155,
 u'preserv': 2.8415815937267324,
 u'donat': 2.0794415416798357,
 u'\u201cwe': 1.0033021088637848,
 u'background': 2.2225423853205091,
 u'skills.\u201d': 3.6888794541139358,
 u'sunoco': 3.6888794541139358,
 u'readili': 3.6888794541139358,
 u'athlet': 3.1780538303479453,
 u'apart': 2.9957322735539909,
 u'measur': 2.3895964699836751,
 u'gift': 3.6888794541139358,
 u'specif': 2.3895964699836751,
 u'54': 3.1780538303479453,
 u'50': 2.5902671654458262,
 u'panelist': 3.6888794541139358,
 u'remind': 2.5902671654458262,
 u'confid': 3.401197381662155,
 u'night': 2.7080502011022101,
 u'hung': 3.6888794541139358,
 u'flagpol': 3.6888794541139358,
 u'attorney': 2.3895964699836751,
 u'right': 1.3862943611198904,
 u'old': 2.7080502011022101,
 u'crowd': 2.7080502011022101,
 u'percentag': 3.1780538303479453,
 u'sandhu': 3.1780538303479453,
 u'rile': 3.6888794541139358,
 u'elderli': 3.6888794541139358,
 u'transmiss': 3.6888794541139358,
 u'combat': 3.1780538303479453,
 u'for': 0.12405264866997889,
 u'enolog': 3.6888794541139358,
 u'fox': 3.6888794541139358,
 u'p.m.': 2.4849066497879999,
 u'cnn': 3.6888794541139358,
 u'condit': 2.9957322735539909,
 u'underserv': 3.6888794541139358,
 u'body.\u201d': 3.6888794541139358,
 u'can\u2019t': 3.1780538303479453,
 u'core': 3.401197381662155,
 u'plu': 3.6888794541139358,
 u'sensibl': 3.6888794541139358,
 u'tour': 3.401197381662155,
 u'insecur': 3.401197381662155,
 u'pose': 3.401197381662155,
 u'confer': 3.1780538303479453,
 u'administration\u2019': 2.9957322735539909,
 u'colleg': 1.5293952047605637,
 u'promot': 2.1484344131667874,
 u'peer': 3.1780538303479453,
 u'post': 2.8415815937267324,
 u'super': 3.401197381662155,
 u'describ': 2.7080502011022101,
 u'chapter': 2.7080502011022101,
 u'bylaw': 3.6888794541139358,
 u'slightli': 3.401197381662155,
 u'surround': 2.3025850929940455,
 u'contamin': 3.6888794541139358,
 u'festiv': 3.1780538303479453,
 u'dinner': 3.6888794541139358,
 u'afternoon': 3.6888794541139358,
 u'commit': 2.1484344131667874,
 u'areas.\u201d': 3.6888794541139358,
 u'produc': 2.5902671654458262,
 u'him.\u201d': 3.401197381662155,
 u'civil': 2.8415815937267324,
 u'regist': 3.1780538303479453,
 u'profession': 2.5902671654458262,
 u'son': 3.6888794541139358,
 u'down': 1.7429693050586228,
 u'creativ': 2.4849066497879999,
 u'resili': 3.6888794541139358,
 u'formerli': 3.6888794541139358,
 u'opportun': 1.4916548767777167,
 u'manageri': 3.1780538303479453,
 u'\u201cthere\u2019': 3.1780538303479453,
 u'deal': 2.8415815937267324,
 u'frustrat': 3.6888794541139358,
 u'support': 0.93734414107198738,
 u'closur': 3.6888794541139358,
 u'transform': 3.6888794541139358,
 u'fight': 3.6888794541139358,
 u'avail': 2.1484344131667874,
 u'university\u2019': 3.1780538303479453,
 u'editor': 3.401197381662155,
 u'way': 1.3217558399823193,
 u'spring': 2.7080502011022101,
 u'call': 1.2909841813155656,
 u'analysi': 3.1780538303479453,
 u'head': 2.4849066497879999,
 u'creation': 3.6888794541139358,
 u'form': 2.7080502011022101,
 u'solidar': 2.7080502011022101,
 u'forc': 2.5902671654458262,
 u'forb': 3.6888794541139358,
 u'heal': 3.401197381662155,
 u'recommend': 3.401197381662155,
 u'surveil': 3.6888794541139358,
 u'hear': 2.7080502011022101,
 u'solar': 3.401197381662155,
 u'true': 3.401197381662155,
 u'analyst': 3.401197381662155,
 u'fort': 3.6888794541139358,
 u'nov.': 1.8971199848858813,
 u'counsel': 2.7080502011022101,
 u'wineri': 3.401197381662155,
 u'intern': 1.7429693050586228,
 u'what\u2019': 3.401197381662155,
 u'tell': 2.5902671654458262,
 u'vanguard': 3.6888794541139358,
 u'jan.': 0.95885034629295074,
 u'cair-sv': 3.6888794541139358,
 u'retir': 3.1780538303479453,
 u'150': 2.9957322735539909,
 u'featur': 2.1484344131667874,
 u'classic': 3.6888794541139358,
 u'upset': 3.401197381662155,
 u'proven': 3.6888794541139358,
 u'sale': 2.9957322735539909,
 u'exist': 2.3895964699836751,
 u'desir': 3.401197381662155,
 u'ship': 3.6888794541139358,
 u'ramirez': 3.1780538303479453,
 u'check': 2.8415815937267324,
 u'impos': 3.401197381662155,
 u'floor': 3.401197381662155,
 u'holli': 3.6888794541139358,
 u'excel': 2.3895964699836751,
 u'actor': 3.6888794541139358,
 u'flood': 3.6888794541139358,
 u'role': 2.5902671654458262,
 u'test': 3.401197381662155,
 u'tie': 3.6888794541139358,
 u'unwilling': 3.6888794541139358,
 u'roll': 2.9957322735539909,
 u'realiti': 3.401197381662155,
 u'legitim': 3.6888794541139358,
 u'intend': 3.1780538303479453,
 u'benefici': 3.6888794541139358,
 u'\u201cthey': 3.1780538303479453,
 u'outreach': 2.5902671654458262,
 u'fell': 3.6888794541139358,
 u'intent': 3.401197381662155,
 u'award': 2.1484344131667874,
 u'consid': 2.0149030205422647,
 u'easili': 3.1780538303479453,
 u'weekend': 3.401197381662155,
 u'femal': 2.5902671654458262,
 u'chairperson': 3.401197381662155,
 u'longer': 2.7080502011022101,
 u'ignor': 3.6888794541139358,
 u'time': 0.93734414107198738,
 u'push': 2.8415815937267324,
 u'serious': 3.6888794541139358,
 u'daili': 3.1780538303479453,
 u'1st': 3.6888794541139358,
 u'recipi': 3.1780538303479453,
 u'concept': 3.6888794541139358,
 u'consum': 3.401197381662155,
 u'focus': 2.0794415416798357,
 u'drop-off': 3.6888794541139358,
 u'signific': 2.3895964699836751,
 u'supplement': 3.6888794541139358,
 u'milo': 2.7080502011022101,
 u'chair': 2.1484344131667874,
 u'decid': 2.0794415416798357,
 u'middl': 2.9957322735539909,
 u'grape': 3.6888794541139358,
 u'flash': 3.6888794541139358,
 u'father': 3.6888794541139358,
 u'environment': 1.5686159179138452,
 u'certainli': 3.1780538303479453,
 u'decis': 2.2225423853205091,
 u'sociolog': 3.401197381662155,
 u'oversight': 3.6888794541139358,
 u'\u201cbecaus': 3.6888794541139358,
 u'brown': 3.401197381662155,
 u'sourc': 2.9957322735539909,
 u'low-incom': 3.401197381662155,
 u'string': 3.6888794541139358,
 u'\u201cit\u2019': 1.5686159179138452,
 u'convict': 3.6888794541139358,
 u'join': 2.0149030205422647,
 u'exact': 3.6888794541139358,
 u'room': 2.4849066497879999,
 u'\u201cwe\u2019ll': 3.6888794541139358,
 u'valuabl': 3.6888794541139358,
 u'administr': 1.7429693050586228,
 u'level': 2.0794415416798357,
 u'tear': 3.6888794541139358,
 u'die': 3.6888794541139358,
 u'restor': 2.9957322735539909,
 u'democrat': 3.401197381662155,
 u'item': 2.7080502011022101,
 u'team': 2.2225423853205091,
 u'quick': 3.6888794541139358,
 u'nagey': 3.6888794541139358,
 u'guy': 3.1780538303479453,
 u'round': 3.6888794541139358,
 u'prevent': 2.5902671654458262,
 u'pork': 3.401197381662155,
 u'outlin': 3.401197381662155,
 u'trend': 3.1780538303479453,
 u'compens': 3.1780538303479453,
 u'sign': 1.9542783987258296,
 u'cost': 2.3895964699836751,
 u'patient': 2.9957322735539909,
 u'6:10': 3.6888794541139358,
 u'appear': 3.401197381662155,
 u'current': 1.2611312181658842,
 u'\u201cnot': 3.401197381662155,
 u'suspect': 3.401197381662155,
 u'uc-wid': 3.401197381662155,
 u'appeal': 2.7080502011022101,
 u'gener': 1.6519975268528961,
 u'muslim': 2.5902671654458262,
 u'french': 3.6888794541139358,
 u'water': 2.3895964699836751,
 u'entertain': 3.6888794541139358,
 u'address': 1.8430527636156055,
 u'locat': 2.1484344131667874,
 u'along': 2.5902671654458262,
 u'destigmat': 3.6888794541139358,
 u'teacher': 3.6888794541139358,
 u'wait': 2.9957322735539909,
 u'alto': 3.6888794541139358,
 u'male': 2.7080502011022101,
 u'invit': 2.9957322735539909,
 u'my': 1.3535045382968995,
 u'proud': 2.9957322735539909,
 u'healthi': 2.8415815937267324,
 u'extrem': 2.8415815937267324,
 u'bob': 3.1780538303479453,
 u'ourselv': 3.6888794541139358,
 u'orient': 3.401197381662155,
 u'love': 1.8430527636156055,
 u'extra': 3.6888794541139358,
 u'prefer': 3.6888794541139358,
 u'leav': 2.3895964699836751,
 u'\u2019\u201d': 3.6888794541139358,
 u'seattl': 3.6888794541139358,
 u'you.\u201d': 3.6888794541139358,
 u'instal': 3.401197381662155,
 u'forefront': 3.6888794541139358,
 u'should': 1.5686159179138452,
 u'mobil': 3.6888794541139358,
 u'market': 1.8971199848858813,
 u'prove': 3.1780538303479453,
 u'sake': 3.6888794541139358,
 u'univers': 1.2321436812926323,
 u'visit': 2.5902671654458262,
 u'by': 0.26570316573300534,
 u'everybodi': 3.401197381662155,
 u'live': 1.4916548767777167,
 u'scope': 3.6888794541139358,
 u'checkout': 3.6888794541139358,
 u'suicid': 3.6888794541139358,
 u'today': 2.5902671654458262,
 u'capit': 3.401197381662155,
 u'afford': 2.0794415416798357,
 u'peopl': 0.64435701639051324,
 u'curriculum': 3.6888794541139358,
 u'enhanc': 2.9957322735539909,
 u'downtown': 1.791759469228055,
 u'visual': 3.401197381662155,
 u'effort': 1.7429693050586228,
 u'religi': 2.8415815937267324,
 u'slogan': 3.401197381662155,
 u'car': 3.401197381662155,
 u'prepar': 2.4849066497879999,
 u'judg': 2.9957322735539909,
 u'focu': 2.2225423853205091,
 u'imper': 3.6888794541139358,
 u'cat': 3.6888794541139358,
 u'mile': 3.6888794541139358,
 u'whatev': 2.9957322735539909,
 u'purpos': 2.3895964699836751,
 u'preclud': 3.6888794541139358,
 u'social': 1.8430527636156055,
 u'heart': 2.8415815937267324,
 u'complic': 3.6888794541139358,
 u'trump': 1.8430527636156055,
 u'predict': 2.8415815937267324,
 u'curri': 3.6888794541139358,
 u'topic': 2.4849066497879999,
 u'heard': 2.7080502011022101,
 u'critic': 2.1484344131667874,
 u'council': 2.0149030205422647,
 u'recycl': 3.6888794541139358,
 u'agenc': 2.3895964699836751,
 u'unfortun': 3.401197381662155,
 u'stole': 3.6888794541139358,
 u'occur': 2.4849066497879999,
 u'clair': 3.6888794541139358,
 u'occup': 3.6888794541139358,
 u'pink': 3.6888794541139358,
 u'multipl': 2.5902671654458262,
 u'winter': 2.3895964699836751,
 u'assemblymemb': 3.401197381662155,
 u'economi': 3.1780538303479453,
 u'write': 2.9957322735539909,
 u'alway': 2.1484344131667874,
 u'sunday': 3.401197381662155,
 u'vital': 3.1780538303479453,
 u'anyon': 2.5902671654458262,
 u'fourth': 3.6888794541139358,
 u'sworn': 3.6888794541139358,
 u'pathway': 3.401197381662155,
 u'product': 2.3025850929940455,
 u'superintend': 3.6888794541139358,
 u'southern': 3.401197381662155,
 u'bear': 3.6888794541139358,
 u'date': 3.6888794541139358,
 u'such': 1.1239300966523995,
 u'april': 2.9957322735539909,
 u'\u201cfor': 2.5902671654458262,
 u'man': 3.6888794541139358,
 u'classroom': 3.6888794541139358,
 u'stress': 2.7080502011022101,
 u'practic': 2.3895964699836751,
 u'secur': 2.4849066497879999,
 u'favorit': 3.401197381662155,
 u'inform': 1.5293952047605637,
 u'switch': 3.6888794541139358,
 u'so': 0.59783700075562063,
 u'african': 3.6888794541139358,
 u'tall': 3.401197381662155,
 u'riversid': 3.401197381662155,
 u'talk': 2.0794415416798357,
 u'shield': 3.6888794541139358,
 u'anticip': 3.401197381662155,
 u'approv': 2.1484344131667874,
 u'shouldn\u2019t': 3.6888794541139358,
 u'brain': 3.6888794541139358,
 u'equip': 2.9957322735539909,
 u'still': 1.6964492894237297,
 u'mainli': 3.6888794541139358,
 u'\u201c': 1.7429693050586228,
 u'dynam': 3.6888794541139358,
 u'entiti': 3.6888794541139358,
 u'ethic': 2.9957322735539909,
 u'conjunct': 3.6888794541139358,
 u'group': 1.6964492894237297,
 u'thank': 2.4849066497879999,
 u'polici': 1.5686159179138452,
 u'passag': 3.1780538303479453,
 u'jim': 3.6888794541139358,
 u'platform': 2.7080502011022101,
 u'window': 2.5902671654458262,
 u'torr': 3.6888794541139358,
 u'main': 2.0794415416798357,
 u'halt': 3.401197381662155,
 u'financi': 2.3025850929940455,
 u'automobil': 3.401197381662155,
 u'initi': 1.6964492894237297,
 u'nation': 1.6094379124341001,
 u'answer': 2.4849066497879999,
 u'half': 2.8415815937267324,
 u'not': 0.34484048629172914,
 u'now': 1.4916548767777167,
 u'nop': 3.6888794541139358,
 u'hall': 2.5902671654458262,
 u'nor': 3.6888794541139358,
 u'introduct': 3.6888794541139358,
 u'&': 2.8415815937267324,
 u'term': 2.5902671654458262,
 u'year\u2019': 2.3895964699836751,
 u'entrepreneur': 3.6888794541139358,
 u'kids\u2019': 3.6888794541139358,
 u'drop': 2.9957322735539909,
 u'separ': 3.401197381662155,
 u'magazin': 3.1780538303479453,
 u'wong': 3.6888794541139358,
 u'rock': 3.1780538303479453,
 u'januari': 3.1780538303479453,
 u'quarter': 2.4849066497879999,
 u'nick': 2.9957322735539909,
 u'christian': 3.6888794541139358,
 u'muslim-major': 3.6888794541139358,
 u'\u201csinc': 3.6888794541139358,
 u'replac': 2.5902671654458262,
 u'individu': 1.6519975268528961,
 u'continu': 1.0739196760777379,
 u'ensur': 2.0149030205422647,
 u'\u201cuc': 2.4849066497879999,
 u'sponsor': 2.8415815937267324,
 u'year': 0.62860865942237432,
 u'happen': 2.0794415416798357,
 u'baselin': 3.6888794541139358,
 u'canada': 3.401197381662155,
 u'shown': 3.1780538303479453,
 u'accomplish': 2.7080502011022101,
 u'jackson': 3.6888794541139358,
 u'space': 2.0794415416798357,
 u'profit': 3.1780538303479453,
 u'internet': 2.8415815937267324,
 u'sweet': 3.401197381662155,
 u'correct': 3.6888794541139358,
 u'integr': 3.1780538303479453,
 u'e.': 3.6888794541139358,
 u'million': 2.0149030205422647,
 u'seventh': 3.1780538303479453,
 u'argu': 2.8415815937267324,
 u'shemeri': 3.6888794541139358,
 u'california': 1.0262916270884834,
 u'span': 3.6888794541139358,
 u'landfil': 3.6888794541139358,
 u'card': 3.1780538303479453,
 u'care': 1.6964492894237297,
 u'refus': 3.401197381662155,
 u'recov': 3.6888794541139358,
 u'\u201cwell': 3.6888794541139358,
 u'place': 1.2909841813155656,
 u'greenhous': 3.6888794541139358,
 u'nonprofit': 2.2225423853205091,
 u'principl': 3.6888794541139358,
 u'typic': 3.6888794541139358,
 u'childhood': 3.6888794541139358,
 u'frequent': 3.401197381662155,
 u'first': 1.1765738301378215,
 u'origin': 2.7080502011022101,
 u'directli': 2.5902671654458262,
 u'carri': 3.1780538303479453,
 u'onc': 2.2225423853205091,
 u'rachel': 3.6888794541139358,
 u'yourself': 2.7080502011022101,
 u'\u2018whi': 3.6888794541139358,
 u'long': 2.1484344131667874,
 u'spanish': 3.6888794541139358,
 u'vote': 2.1484344131667874,
 u'happi': 2.9957322735539909,
 u'open': 1.7429693050586228,
 u'given': 1.9542783987258296,
 u'boulevard': 3.1780538303479453,
 u'christma': 3.6888794541139358,
 u'district': 2.2225423853205091,
 u'bloom': 3.6888794541139358,
 u'caught': 3.6888794541139358,
 u'breed': 3.401197381662155,
 u'reli': 3.6888794541139358,
 u'plastic': 3.1780538303479453,
 u'citi': 1.0986122886681096,
 u'2': 2.5902671654458262,
 u'draft': 3.6888794541139358,
 u'proposit': 3.401197381662155,
 u'conveni': 3.6888794541139358,
 u'cite': 3.1780538303479453,
 u'friend': 2.3025850929940455,
 u'transgend': 3.6888794541139358,
 u'future.\u201d': 3.6888794541139358,
 u'that': 0.14310084364067333,
 u'season': 2.7080502011022101,
 u'logist': 3.6888794541139358,
 u'mostli': 3.1780538303479453,
 u'quad': 3.1780538303479453,
 u'than': 1.1765738301378215,
 u'boyfriend': 3.6888794541139358,
 u'11': 2.8415815937267324,
 u'10': 1.5293952047605637,
 u'13': 2.8415815937267324,
 u'12': 2.7080502011022101,
 ...}
In [54]:
plt.hist(idf_smooth.values(),bins=20)
plt.show()
In [55]:
dat = [biggy['text'][x] for x in xrange(len(biggy['text']))]
In [56]:
vectorizer = TfidfVectorizer(tokenizer=lemmatize,stop_words="english",smooth_idf=True,norm=None)
tfs = vectorizer.fit_transform(dat)
In [58]:
sim = tfs.dot(tfs.T)
x = list()
for i in range(0,len(biggy)):
    for j in range(0,119):
        if i != j:
            x.append([i, j])
y = list()
for i in x:
    y.append(sim[tuple(i)])
l = list()
for i in range(6):
    m = max(y)
    r = [i for i, j in enumerate(y) if j == m]
    l.append(x[r[1]])
    for i in r:
        y[i] = 0
l
Out[58]:
[[17, 11], [15, 9], [9, 17], [15, 36], [17, 117], [17, 56]]
In [60]:
k = list()
for i in l:
    k.append(sim[tuple(i)])
k
Out[60]:
[69556.33973665371,
 30273.899623357396,
 16323.493824626179,
 15577.449899986905,
 14865.259760042278,
 12850.559077167329]

From the two wordclouds we see that both the text columns have the highest frequency of the word "Davis", "UC", "city." This shows that most articles are about Davis (Which is good since it is the Aggie), some about the school and some about the city. Next we'll check the difference between the ones that are specific to the campus and city.

Both the titles of the city news and campus news have Davis as the largest word. For the campus, the second largest word is "UC" and for the city the second largest word is "city." This makes a lot of sense. The campus words with highest frequency have a lot to do with things happening on campus like winter, holds, participate, etc. This differs from the city which includes words more like Woodland, protest, police, and other words more specific to the city itself.

In [63]:
vectorizer = TfidfVectorizer(tokenizer=lemmatize,stop_words="english",smooth_idf=True,norm=None)
def cosine_sim(text1, text2):
    tfidf = vectorizer.fit_transform([text1, text2])
    return ((tfidf * tfidf.T).A)

Since the values in the similarity matrix are so large we can assume that the articles of campustext and text are not that similar.

In [ ]: