Welcome back from lunch. We wanted to make sure that we think some of the people who have been supporting us at six Oxford,
which is Russell Sage Foundation, Alfred P. Sloan Foundation,
Oxford Van Hooten Fund, Social Science Division of Teaching Development Awards and Nuffield College,
and of course, the sociology department here that we're in. That's just like a short list of a number of groups that help bring this together.
So thank you. Today, we're going to talk about computational text analysis.
My name is Taylor Brown.
I'm a Ph.D. candidate at Duke and a visiting scholar at NYU, and I know that there's a lot of people in this room in particular who have interest.
We learnt from the flash talks and computational text analysis, but also a lot of experience.
And this is not meant to be just me talking at you because of that level of interest and expertise.
I hope that you ask questions. You help answer questions.
If I don't answer them properly, you correct me. If I do it wrong, we can debug my code together.
Whatever. Let's just make sure that this is interactive, so feel free to interrupt.
And if you don't, I'll probably pause and ask you to. I don't actually consider myself to primarily be a computational text analyst,
but I have used the methods previously and can someone let certain things and.
And I think one thing that's really neat about these methods is it's pretty easy to go from beginner to the cutting edge in a short amount of time.
When we look at the timeline of how computational text analysis has has developed,
a lot of the progress has been made just in the past couple of years.
And I didn't know I didn't know how to code when I started my Ph.D., but I certainly didn't know a lot of these methods.
And now I help on a project that that package we'll see later on on on tech's networks.
That's kind of something that's never been done before, but isn't, but also isn't that advanced and complex.
So there's a lot of opportunity to contribute, especially in the social sciences.
And the last thing I'll mention before we jump in is that as I was preparing this,
I decided to start like a bibliography of foundational and really relevant computational text analysis citations,
and I started a slack channel called Text Citations.
You can join, and if you have anything to contribute, you just put it there and I'll add it to the bibliography.
And at the end of six, we're just we'll print it out and you'll have what could be the start of a good syllabus or reading list or whatever.
So and all the references that I have in this presentation will be there as well.
But starting out. What is computational text analysis on the face of it, it's pretty straight forward and in a lot of ways it is.
But let's break it down. First of all, computational having to do with computers or using computers.
The the point being, amongst other things, that we have just a massive amount of data now and not enough time to to analyse all of this text.
And we use computers to help us do stuff much faster that we probably could do if we would live forever or something like that.
But but it can also be. Computers also help us to do very more complex things we might have difficulty doing at all.
Text this again seems kind of straightforward, but this is the definition I came up with because of reading other things.
Any object that can be read. And most of the time we think of this in terms of written or spoken language and in social cultural scholars have shown.
And I think our intuition is that this sort of language it contains within it a structure that
reflects things like our as as groups of society or morality or hierarchical structures of status.
What's important to us, all of these sorts of things. So that's one reason we focus on those types of texts.
We also just have a lot of those types of texts because it's our primary mode of communication.
But in my work, for example, I look at artworks as texts, as objects, cultural objects that have meaning,
and that meaning is kind of derived from things like colour, content, texture.
And I look at these vectors that are just numeric, but I'm thinking them of them in a very similar way to the way we think about texts.
And so I would just encourage you as we go through this, maybe or not, a more linguistic text analyst.
But maybe your data could be analysed with some of these methods.
I was thinking this morning, I don't I don't know of any case,
but using a topic model on something that's not language text, and I don't know what would come out of that,
but topic modelling was kind of discovered simultaneously in like population ecology or something non textual and with David Blaine,
who's the one who's been cited a bazillion times with their latent, variously allocation.
So originally it was thought of as possibly not applying the texts, linguistic texts and then analysis.
Hopefully, if if you don't know what analysis is, we have like a whole other lecture, but there's probably a lot of definitions for this as well.
I came with a systematic examination of the structure mechanisms of something.
So if we put all of this together,
computational text analysis would be systematic computer-assisted examination of the structure, mechanisms of readable content.
And that's great. But it also makes it sound a little bit boring.
And in particular, social scientists, we want to think about how what analysis means to us.
So Hopkins and King in their article on textual on text analysis,
say this Policy-Makers or computer scientists may be interested in finding the needle in the haystack,
such as a potential terrorist threat or the right webpage to display from a search.
But social scientists are more commonly interested in characterising the haystack.
So we don't necessarily, for instance, with topic modelling, focus on getting the correct classification of some sort of document,
but rather are interested in the distribution of documents over a corpus.
So that just keeping that in mind, as social scientists, again, computational social science,
how we're using these methods because computer scientists or policymakers might use them in a very different way.
Turning now to just the history of will do this really quickly.
The same article the history of computational text analysis that same Hopkins and King article note
that the Catholic Church tracked the proportion of non-religious printed texts in the sixteen hundreds,
and they kind of mentioned this is one of the first examples we have of of like word count or some sort of content analysis.
My intuition is there's probably a precursor to this in other parts of the world, whether the Middle East or Asia Africa.
But this is like a western delta and King and Hopkins were going in that direction.
So. So that's kind of it started a long time ago content analysis.
And but we usually talk about the modern era, starting with Laswell doing keyword counts.
There's a lot of quotes for that from him, for the intuition of content,
analysis of text, for studying sociometric measures or measures of social dynamics.
And those same methods of keyword count started to be used by social scientists in the nineteen forties.
And then, of course, Turing in the nineteen fifties in the context of World War Two applied AI to study, text, try and decipher foreign transmissions.
And then as we moved along, we have the first textbooks on content analysis.
We start applying mainframe computers to them, start doing an open event coding and then we get these dictionary based methods like week for
studying texts and that that brings us up until the 1990s when we have the first topic models.
And then slowly, throughout the nineties, these topic models get infrastructure and get started used and other methods like network
methods of text that we'll discuss later start to get use in there a little bit.
Previous in like the 70s, we have the first embedding models not necessarily word embedding, but embedding models.
And then we in 2010, King and Hopkins and others start really bringing topic modelling.
This is kind of the main method that social scientists tend to use as their entree into computational text analysis and topic modelling.
And that was just in 2010, which is a disturbingly long time ago, but also not that long ago going to, it seems in my mind to be not that long ago.
And in 2014, Majura or Molly Roberts and her colleagues developed a structural topic modelling, which will do a tutorial on later.
But one thing that's interesting about that is that it really was a content text analysis
design for social scientists and sociologists interested in demographic characteristics.
And then we have today, which I believe is June 19, and it's all of us doing whatever we're doing with text analysis.
And like I said, it's pretty easy to jump to the cutting edge. Building off of everything that's come before us.
So. Getting the data really talked about this quite a bit yesterday, so I will not do too much on it,
but where do we get this sort of data that we use in in these with these methods, these computational methods that will.
We'll study later. Largely lately,
one of the things that has really pushed the development of text analysis forward is the fact that we have tons of content from the internet,
including social media. So here there's some examples.
As really mentioned, Twitter has been very good at helping us get its data.
And so a lot of studies come out of Twitter data power.
Pablo Barbara, who we'll hear from later, is this birds of the same feather tweet together,
just looking at the network structure of Twitter and trying to predict ideology
of political leaders and then kind of comparing that to what they actually say?
Monger This great title tweet meant effects on the tweeted experiment reducing racist harassment was looking at.
Similarly on Twitter, if you could use bots to kind of censor people who were harassing others.
We have studies on the effect of wordage in terms of how popular a message becomes.
Reddit is another source of data that's really interesting.
This study looked at there was a ban in 2015 for hate speech on certain subreddits, and they just wanted to see,
did it actually diminish hate speech or did these people and the hate speech just kind of bleed into other subreddits?
Similarly, analysis and Facebook Kickstarter. What makes for a successful Kickstarter campaign based on the language?
We talked about this one at lunch, some of US self-disclosure and perceived trustworthiness on Airbnb.
So as a host on Airbnb, if I disclose more about myself,
is it possible that I that the people looking for housing will trust me more based on text analysis?
And then this one by a king at all? They they use a lot of different social media platforms to look at Chinese censorship.
Outside of social media, there is open ended surveys, historical archives,
this study by Beerman and Stovell used historical interviews with former Nazis.
This one was looking at text from the Qing Dynasty from seventeen twenty two to nineteen eleven.
I just came across one by Mark Anthony Hofmann from Columbia, but he's moving somewhere else.
But he looked at the Bible and how different in the US kind of revival era,
how different pastors in their sermons cited different Bible quotes or passages and Enron emails,
political documents, including the State of the Union in the US, which is a speech given by the president once a year.
And, of course, newspapers and beyond these more specifics, we've got sorry,
we've got these massive corpora like Google and which is millions of books from fifteen hundred until twenty eight.
Maybe you've been more recent now English corpora is an interesting one that I don't think a ton of people know about for some reason,
maybe because you have to pay for it and we don't like doing that. They have some great corpora that are historical.
They're subdivided it by type and then some that are very like updated daily and they're quite large.
The manifesto project has like political stances by something, what I put it.
By over a thousand parties from nineteen forty five until today in 50 countries on five continents,
spinner Internet Archive is another really interesting one. If you're ever wanting to save a website and the text on that website as it is today,
but you don't have time to scrape it or don't know how or whatever you can just go to Internet Archive,
place the URL and it will store that it's literally an archive of the internet.
So for instance, when there's maybe political transitions as we had, you know, in the US a few years ago,
people were worried about certain political government departments like the EPA.
And so there going to be changes when there's a change in presidency to these these websites sometimes.
So people went and just archived all of those web pages.
So you had a static version of what it was like before and after the transition, but this has just tons of of resources for texts.
Any questions, comments, other resources that are so.
Yeah. There's no one else. I mean, it's a clear line between public cloud, OK?
Of the date preservation project, OK, the data preservation project OK.
Cool. Yeah. So anyways, there's tons out there.
And how do we get at it? We talked about this a little bit yesterday, but open source or API is obviously ideal, especially if the open source,
as we talk about, isn't from a hacker who just dropped it there for you, but is actually open source.
Someone has said you can have this or an API where they give you a structured way of getting it from them.
A private agreement. My dissertation data is a private agreement with a company.
Those aren't always easy to come by, but they can be quite nice purchased.
Like I said, the English corpora is one that you can purchase, but you can purchase mass amounts of Twitter data with the firehose as well.
And then, of course, scraped, which we've talked about in terms of use on that one.
Or as we said, maybe you don't care, but I would say check the terms of use.
And then once you have your data, you need to do some preparation before you can analyse it.
So I wanted to take a poll of those of you who do text analysis, how many of you use Python to kind of clean or prepare and analyse your data?
OK. Maybe five. And how many of you are oh, OK.
A little bit more, but not a ton more. If you use our do you use the tidy, like tidy text packages for cleaning your data?
Yeah, the most part. OK.
So in the Princeton Core six on the computational text analysis slides, Maya and Aden's advisor Chris Bell is teaching maths and he will.
He has tons of examples of tidy text, and so we talked about it at lunch.
I think that is. I love Tidey. I love the title universe, especially for text analysis.
Anything but I had we work on seems trustworthy, but I thought I would introduce us to a different approach.
And so when we do our tutorial, we're going to use the quantitative package. Does anyone use that?
Which is it? It's different. Very, very similar, but different.
And I think it's just nice to know what tools you have at your disposal beyond the tidy package.
But OK, before we get to that, let's say we have we now have a corpus of data,
a corpus of text that we want to analyse and we need to pre process them.
So maybe it looks like this. This is actually a piece of scraped data. I have to admit I didn't look at the terms of use, but it's just a small bit.
And as you can see, there's not just text. This is some sort of art contemporary art criticism document.
There's not just text, there's, you know, there's Unicode non ASCII characters, there's age HTML, and we don't really want to analyse those, probably.
And in the context of AR, you're largely what you're going to use to get those out are regular expressions.
Using grep commands, which stands for global globally, search a regular expression in print and we similarly over break,
we're talking about how we all kind of hate regular expressions.
They're they're cumbersome and there's a ton of packages and ah, that you can use that have them built in, so you don't really have to think about it.
But I do encourage you if you want to do text analysis to just get,
especially if you're doing it in order to get familiar with the regular expressions.
And I'll share this later.
There's there's some sort of app online where there's crossword puzzles that you can fill out using regular expressions to get better at it.
But they can't be. They can be a pain. But this is kind of what it would look like in the code.
You might you would. You would define a variable as you know, whatever your text is.
And then this is one of the ones that's the methods that's in base are so you don't have to load a package, but just g sub.
And what we're trying to do is we're saying, find this pattern.
The tab, the tab object and voice touching and replace it with a blank in this variable text.
And it comes out and you haven't removed the page similar to the the Unicode, but you did remove that.
So this is kind of the intuition of all the regular expressions for cleaning, obviously, like Unicode, for example,
as a whole system of of codes that that code special characters and text and you're
not going to want to do each one of those is a regular expression yourself.
And that's where some of these built in packages that have already coded that out for you can be useful, but sometimes they just don't work.
And so it's really good to get to know your regular expressions. This is another one for taking out the HTML.
So you're just submitting anything in these brackets, which is how each HTML code is often written, is written and replacing it with nothing.
And then you have this is defining it as a function, so you could just feed your text to that function and it would remove all of the HTML.
But here's a cheat sheet that you can go to that has a whole bunch of regular expressions
and you can start to learn and I will try and find the crossword puzzle in case you.
Are interested in that, but after cleaning all of that out, this is ideally what your text looks like before you before you start to analyse it.
There's a few more steps in processing that are optional and more substantive decisions, I think.
And we're going to discuss these. But what you love is this just this clean text where you don't have other code, you have extra white spaces.
Yeah. Any questions or comments on that so far?
OK. So as you go about pre processing a little bit further, we have things like stop word removal,
which I'm pretty sure everyone in here is familiar with what stop words are,
but they're basically just very common words that aren't really substantive in terms of if you're looking at the topic or content of your text.
So things like at the and et cetera, but it might be corpus specific to you,
maybe when you're looking at, I don't know, some sort of religious text you really don't care about.
A certain religious object. You just want that removed because you don't want to analyse that over and over and over again.
Or specific names that you want removed. You can add those to your dictionary of stop words and remove them.
And when we do our tutorial a little bit later, we'll see how to programmatically remove stop words.
Then there's the options of stemming or limited rising your your tax stemming removes
the endings of conjugated verbs and plural nouns returning only the stem of the word.
So in this case, running would become run and the verb saw like I saw something would remain sore.
But if you limit, you actually get to the base type of the base of the word.
And so the noun saw like sawing a piece of wood remain would remain sore, but the verb saw would become C because that's it's based on.
So then seeing saw and see what all become see in your text.
And if you think about it, I mean, these can be very substantive and there have has been research that has shown that
you will get different effects in certain contexts if you advertise or if you don't,
or if you remove certain stop words and not others.
And so you want to think about that in this, I think comes back to what we were talking about before that in this kind of wild west.
In some regards of computational social science, we don't necessarily have standards of reporting.
But whenever I review papers that do things with computational text analysis,
I do ask the author to provide, if not their code, so we can just see what words did you remove.
Then the code and robustness checks for what happens if you if you just change some of those parameters around, ideally nothing would change.
But sometimes it does.
Other options in pre-processing are to create an in your text so just based tokenization, meaning that token is just like an individual unit of text.
You can treat them as you anagrams where it's just each individual word.
So something like New York City would be separate New York and City. And if we.
If we do something like a topic model or an embedding model like we did later,
those are probably likely to show up in the same cluster or topic because they're technically the same entity.
You could do it as by and just get New York or New York City. And there are certain and graham kind of processes that will help you predict
which engrams are actual and Graham's just based on how commonly they show up.
And so York City probably would get kicked out because that doesn't happen as much, but New York would stay in.
And then you have New York City, which is the tiger, the tiger. And you can get longer and longer if you like.
You can identify parts of speech,
so this is kind of a common output of a part of speech tagging if you want to identify which of your words fall into which part of speech.
So we have like a singular noun and a plural noun and a verb.
And maybe you want to do that, so then you remove all adjectives or you remove all nouns.
There was a professor in our department, somebody mentioned moral found multiple people mention moral foundations theory.
Previously, he was interested in doing topic modelling where you removed.
The mounds I can't remember now, but basically looking at trying to get the moral foundations by clustering only adjectives.
So once you have them attached to their nouns, then you just look at then you get rid of the nouns and you only look at the adjectives
to see how the clusters of morality around words are shaped in different corpora.
And then identifying named entities,
which is a subtasks of information execution that seeks to locate and classify named entity mentions in unstructured text into predefined categories.
And I will show I have a little tutorial that we're going to go through using the Google named Entity API.
There's others, but it's kind of neat because it will return, you send it the text,
it returns the text and it picks out things that identifies those named entities.
If there's a Wikipedia page associated with it, then they'll give you the link to the Wikipedia page,
and it gives you some like whether it's a noun and a person,
a place, an institution so you can start to to get at what what other entities are spoken about in your texts.
And so, yeah, that's the point.
Now that we're going to move on to the tutorial for pre processing some of the things we just discussed, we're going to show them.
And that I think I have a slide for it. Oh, but before right before we do that, when we come back, we'll talk about analysing the texts.
I just wanted to make sure that you all have access. Did you all get access to the materials?
I sent the link on Slack. Raise your hand. If you don't have the materials yet, you don't.
On Slack, there's a link. If that's all Flash six, Oxford.
And all of the materials, the tutorials for data and the data should be there.
So I'll come to you right afterwards.
But yeah, we'll clean the Texan when we come back after the break, we're going to analyse the text and I thought I'd just give a precursor to that.
In terms of in the computational analysis world, we've all heard these term supervised,
unsupervised and semi supervised the different methods that you can use in like machine learning and these sorts of things.
And I just wanted to make sure we're all on the same page as to where we fit within it and where these methods that we'll learn fit within it.
So unsupervised text you, you give your algorithm or whatever you're working with computationally a set of labelled data,
meaning that maybe you have multiple cases and you're saying, this is a woman, this is a man, this is a woman, this is a man.
And the algorithm will train on that set and then help you predict a set where you don't have any of that data with supervised data,
you don't have any labels to begin with. You don't have. What topic does this word belong to?
You don't have. Is this a male or a female candidate? Are they at high risk or not?
But it just kind of. Uses without really supervision trains and starts to predict what those categories might be.
And with semi supervised, it's a combination of these two things.
Usually it's something like you have some labelled data, but you have a ton of unlabelled data.
So one way you would do that is you would take your labelled data and train an algorithm, predict the on some section of the unlabelled data.
Now they have labels and you bring them back in and relay train the analysis, then you train more.
And it's this kind of cyclical process.
The things that we're going to learn, topic modelling, word embedding and network analysis are largely unsupervised,
classified as unsupervised because a lot of times we're just feeding it data and it's giving it something out.
But in the in the context of social scientific research, it's I put it a little bit more in the semi supervised,
not in the sense that we're adding labels and then retraining algorithms,
but that we are very intentionally informing the algorithm by our own substantive knowledge, which we'll talk about further.
That these sort of methods for us are not things as Chris talked about or not in their introduction to computational social science.
There was maybe hope. I think it wasn't as broad spread as we sometimes like to think, but there was hope at the beginning that with tons of data,
we would need theory anymore, and we've learnt that that's really not the case. And that's true with these methods, too.
Like topic modelling doesn't mean you don't have to read your text.
In fact, it's very important that you're familiar with the text in the corpora that you're working with.
So it's kind of like unsupervised ished, just because the definition of semi supervised is a little bit different.
But yeah, when we come back, these are the three methods we're going to work with. But before that, we're going to clean our data.
Yeah, that's the difference in my own experiences.
Yeah. Where can you get them, really? Well. Thank you.
I just want to ask for a pre-processing steps. There were some things I ran into at some point because they helped me.
First of all, if you have. Well, first of all, if you have text in different languages, you can easily use a translate API.
Oh yeah. But then also it can help you to actually get out spelling mistakes.
So if you translate to language and I'm back against the English,
how clever actually fixes and general you have also proposing steps that I do edits critical spelling errors that take a long time.
Text globe. I think four four are OK.
And then last thing is that you can use fuzzy messaging if your data has a lot of names of companies, that kind of stuff.
They're all mentioned in a different way or with or without and ink and etc.
Yeah, it's actually get them together as a two steps at will be helpful, too.
Yeah, those are both really great points. I think the fuzzy matching is often used when you have, yeah, things, things like Disney or Disney.
It's like the same thing. The named entity stuff can sometimes help with that as well because they'll have the same URL attached to them or something,
but those are both really great points. Anyone else experiences we could go on forever, I'm sure, with horror stories of data.
But what about anything you've learnt or? OK.
Maybe they'll come out during the tutorial, which is what we're going to switch to now.
So the the script that we're going to start with, it's the HTML file for the quantita.
I think it's just called pre processing. And now we're going to switch to the HDMI.
I'm turning myself off. Oh, yeah, anyone on the livestream. Yeah.
The link to these materials are in the HTML files on the BETWEE link that you should see on your screen as soon as we turn off the slides.
We're back just a reminder anyone on the live stream there should be like a bitterly wink that you can follow.
If not, it's bit dot lie slash six Oxford and the file that we're looking at is the HTML on pre-processing text.
And as I said, we're going to work mostly with the Quantita package, but also some things from the tidy purse.
And I'll just walk through it and anyone can ask questions. Obviously, anyone with experience in our python, but particularly there's no R or Python.
There's a million ways to do any one thing that you want to do. So I'm just showing one example.
But if you know of a more efficient way or anything like that, feel free to interrupt.
So first, we're just going to load the packages and then load the texts, which, as I mentioned,
is just a set of of abstracts from sociology between, I think twenty eight and 2000 and like half way through 2012.
And you see here with the printout, just kind of a preview of what the data looks like in the data frame kind of looks like what the source,
which journal the article came from, who the first author is.
I didn't extract all of the authors, only the first author and the year,
and then that's just kind of a thing so you can track back to which file it came from.
Other ways to read in your data, obviously, your data can come in a ton of different formats and this read text I like read text a lot.
There's also a reader read our function. That's pretty great.
But but for quantita, this read text is pretty great.
There's one for just tweets that reads in Twitter data.
Again, that's just represent representative of how prominent Twitter data compared to other things are that we have unique reading package for that,
Jason. Text files multiple text files say you have a I often have this.
You have a file on your computer with a ton of text files and you don't want to load each one individually if you just why it doesn't.
OK, I'll stay over here.
If you just write your path and then start text or whatever the outcome of the format of your text is, it will read in all of those.
And then within quantita for the analysis, you also have what they call dark variables for the file names,
and you can pre specify those and we'll talk a little bit more about them in this case on that might.
Five. It was reading in the State of the Union address, which again is a US speech by the president.
And so it was saying which president and which year XML file CSP.
One thing that's really neat about read text is that when you read in text, you specify,
you declare this parameter text field as to what the column or field within your original file,
where the text is, how it's identified and it reads it in and it names it text so that if you say
you had a document where you had a bunch of different CSV files in the text.
Call them in these different case files was named slightly differently when you use this,
it will rename all of them text so you don't have to do that yourself.
This is me, I don't know how with the Quantita package, how to duplicate the text there might be.
So say you have. Maybe when you were scraping you,
you save you accidentally saved or the same web page has two different URLs and you captured both of them, so it's the exact same text.
But some of the other metadata is different, and so just doing like a dupe on the data frame at large wouldn't work.
It's only duping the text. I use deployer for that.
So this is basically just taking our sociology data frame and then grouping it by the text side.
Anyways, it's duping that if you if we want to explain it further, I'm happy to.
But this is one way that you could remove multiple texts.
If the other metadata about that text was different might be risky, though, because maybe that metadata is really where the important difference lies.
But let's say let's run that.
I don't think for this particular corpus there were any duplicates, but I definitely have duplicates and in contexts where I.
Scraped data.
And then in quantita, similar in some respects to other packages, you create what's called the corpus, which is designed to be, as it says,
a library of original documents that have been converted to plain UTF eight encoded
text and stored along with the metadata at the corpus level and document level.
They have this special document level metadata called doc variables, which are variables associated with the document.
So in the case of our sociology abstracts doc, an instance of the doc variable would be What journal does it come from and what year was it?
Was it produced? The corpus in quantita, as it says here is is not really designed to be where you conduct your analysis.
It's like so original data kind of when you were first learning Stata or SPSS or something,
it's like, don't change the original data, don't touch that right?
It's similar to the the corpus. It's where if you want to do multiple different analyses or robustness checks, that's where the original data lives.
So we're going to create a corpus out of our right now, our.
DataFrame, it's just like it's just a data frame, a traditional data frame that with the text,
the and whatnot, and now we're going to create this special object called a corpus and take a look at that.
It looks pretty much the same. But the formatting, the metadata of a corpus is slightly different and allows for certain content quantita analysis.
If we want to add document level variables that weren't originally in the important of the data,
so again, a dark variable would be like the journalist or the year. So we wanted to create one on the fly.
So in this case, just because it was what I thought of, first,
we're going to identify two of the prominent American sociology journals American Journal of Sociology and American Sociological Review,
which a lot of people would love to have a solo authored or an authored paper and one of these.
So I thought those are like kind of the paradigms in US sociology of like a prestigious journal.
So when you use that as a heuristic for like Tip Top Journal publication, and I'm just going to make a binary coding of that.
So here we only get we're only getting the first five examples, but you see, there's this new variable a.j.'s,
as are none of these first five as you can see there in the sociology, religion or symbolic interaction.
All of those are not in that journal, so they have a value of false. But we've just created a new document variable.
Is this document from a serious R? There's many other ways, as I mentioned, Rag X would be your friend for creating new doc variables.
But this is just one example. Any questions? All makes sense.
So if we wanted to add a corpus level variable, which is not about individual documents,
but the corpus as a whole, we would do that with Matt Murdock.
And I just added the date yesterday, which was June 18th,
and that's kind of just a meta information about the corpus you could add, like who collected it and who created the corpus.
If you plan to make this open source in the future or anything, that that could be a really useful place for you to store metadata about the corpus.
And to take a look at an example, we just want to say, look at the text of the first or this, I'm looking at the second abstract here.
This is an abstract from I believe it was the sociology sociology of religion, right?
This is what the text looks like. Luckily, it's pretty clean in terms of, like we said, there's no non ASCII characters or Unicode or something.
So we can. We can be happy about that, because that's a luxury.
If we wanted to summarise a bit about the corpus, I'm just summarising here the number of abstracts by year.
I don't know why it didn't come up. We go.
So, yeah, like I said, it cuts off partially through 2012, but it looks like there's approximately fifteen hundred articles for each year.
We wanted to see which text is the longest. It looks like there's five hundred and one tokens and one abstracts.
It's kind of like they want one word over what is often our limit of 500 word abstract.
If you happen to have more than one corpus and you wanted to combine them, pretty simple, just plus Adam together.
If you wanted to subset your corpus here, we say we only wanted to look at two thousand and ten.
This is how we would would do it and move this over. So it's not cramping our style.
And then we can begin to explore the actual text itself.
So if we wanted to see, for instance, I study art, I want to see which abstracts mention the word art and in what context.
So here are some it tells me which abstract it's from and then the context of it.
So I argue there is art exertion. Who knows what due to?
I don't know what that one is.
Average consumers of art and culture can maybe then start to maybe you look specifically for things that are relevant to my own research.
The practise of art as prayer, or maybe this kind of exploration starts to give you ideas of what sort of topics
there might be if you're going to do a topic modelling or something like that.
So you can start to explore your data, and there are evidently a lot of hurt.
OK. You can expect inspect the document variables that you defined or the metacarpals variables that you defined,
those we've already kind of talked about.
Then when you want to perform your analysis, as is often the case, you're going to create some sort of term document or term tote like token matrix.
There's often the idea of term frequency document and frequency matrix that you use and topic modelling in quantity.
At first, it's literally, I believe, just a yeah document feature matrix where the features are the columns and the features of the words.
And then you have the documents. And there's a lot of different ways to tokenise.
As I mentioned before, a token in the context of text analysis is like a unit of analysis, and that might be a word.
It might be a whole sentence could be by grams or grams. It could be only punctuation.
It's whatever you want. So I'm just going to show within the context of quantita all the different ways you could very easily tokenising.
And we're starting with these three sentences that I came up with that if anybody can identify which sources they're from,
buy you a drink or coffee later.
So in this case, these are the the texts, and we're just tokenising the basic tokens tax is just going to put it into words.
It's not looking at diagrams or telegrams or sentences, it's just words.
So it's split if you're happy and a dream, does that count into if you're happy and etc., right?
But there's all of these parameters that you can set in the tokens for function like remove numbers, remove punctuation,
remove symbols, remove separators, remove Twitter things like hashtags or at science, remove hyphens or remove URLs.
And I'm not going to go through these. But if you run these different functions,
you'll see how doing the same thing results in a different result or results in different tokens from those sentences.
And as we talked about before with engrams, the token can also do Ingrams.
You just specify Ingram. So here with engrams one through two, I'm saying get MoneyGram's and diagrams.
If you did two to three, you would only get by grams and try grams. You did one two three.
You'd get, et cetera, you anagrams by and try grams. So here that same sentence.
Let's look at the shortest one. If you're happy and a dream, does that count also has if your your happy, happy in in a sutra, right?
You can also this definition character, so instead of words,
I want characters I don't know why you do this, but which is why I said, why would you ever do this?
But it would split it into letters. If you're thinking maybe you would do it to count, to count the number of characters, there's an easier way.
But. There it's possible, or you can tokenise by sentence, but each of these is only one sentence long.
Oh no, this is this one, OK? So. This is not known, this is bowling, there are rules.
Similarly, if you can tell me where that's from, you get a drink or chocolate or something, but it'll just put it into sentences.
Now sentences are your unit of analysis. They're your tokens. Cool.
Super cool. OK, now constructing the document frequency matrix is similarly quite easy in quantity package.
It's just this DFM. In the tokens, it didn't do anything unless you told it to write, it didn't lowercase,
it didn't remove punctuation, it didn't do any of that unless you specified that that's what you wanted with DFM.
It will do a lot of that. You can override it, but it will do a lot of that on on its own.
And so you won't have. It's not showing me.
You won't have to specify, and you won't have things like if it appears at the top in the beginning of a sentence,
you won't have it capitalised and so it's counted as a different token.
It'll be the same token if it's the same word. You can also remove stop words in STEM, which is what we talked about in the in the tutorials.
It's really easy just defining you want to improve English, stop words and use that stem to true and you want to remove punctuation.
Looking at what types of Stoppard's are in this English dictionary, it's I mean, my et cetera, but you can add you can add to it yourself if you want,
like I said, if you wanted to remove, maybe maybe you're analysing text messages or something and they are all yours.
And so your name is there a million times and you're like, I don't want me to be there.
You could add your name as a as a stop word.
Sorry, guys. That'd be very interesting to analyse your text messages. I've never done that.
So this is an example of that. Adding the word will to the dictionary stop words to remove.
Then this top features function, oh, I have to define that.
We'll just show you the top features, the top words that are occurring in your corpus.
So in this case, unsurprisingly, maybe social is is top data study research article using health women results amongst religious also.
It's kind of like a sentence. So, yeah, these are some of the top tokens or words used in sociological abstracts.
You can also make a word cloud, which you kind of a love hate word clouds, and this takes a while to run.
It's in your HTML, you can see it. Some like ungodly palette that was selected to go along with it, but.
I shouldn't have clicked on that. You guys know what the word cloud looks like.
All right. There you go. Who? It's very tropical.
You can also group document, sorry, you had to decide yourself that the full size to to the word size.
Yeah, yeah, yeah, yeah, the font size of the words.
Yeah, it's how common that word is in the corpus. Yeah. You can start to analyse by groups of tags, so in this case,
you want to see if maybe you want to create a corpus or a term document for each frequency matrix by year to analyse them separately.
And so it would do that. So here now we're able to see like say that we normalise the number of tokens across years.
You could see how they like, go up and down.
This is just rock counts, but it looks like health was really more popular in 2010 and went down for whatever reason in 2011.
And they have nearly the same number of documents, if I remember from the histogram at the beginning.
So that's one thing you can do. We're not going to.
Well, maybe we. Yeah. And what did you have the parameters?
Is it more advisable to travel documents when they chase down?
In this case, the those were some of the year's strikes and the Collins.
Well, it's still like the same matrix of just the documents and the features or the tokens.
And then the analysis just does that by year and gives you this table.
So it's sort of the list is is every single piece of information sold in the list and because just by typing.
Yes. And I don't know what the commanders on, I think produces that headed for you or yeah, it will.
It produces the header for you there. It's The Matrix is just a matrix, which is a bunch of lists, you know, in the columns.
But then separate is the corpus where the metadata resides, and it's just drawing on that to create this table for you.
Yeah, because of. But I'm interested in, for example, president a government to make sure it's fair and.
Would send an answer to. OK.
Yeah. And so that wasn't really. So I'm just wondering that's gotten some space.
Yeah. I would probably create a matrix.
By sort of one category of improvement in the this year.
Yeah, I'm trying to think off hand because you want like all of the different combinations of your two features.
I don't think there's a straightforward way to do it with quantity,
but using to there might be you could look in the documentation, but it's definitely doable.
Yeah. Sometimes we have, as it says,
some prior intuition of words that are particularly important inside of our text and we might want to know how those relate.
This shouldn't be women, probably incorrect. I was looking at men and women, but then I switched it to culture and structure, which are too common.
We have previously thought of them as either oppositions or ends of the spectrum within social sociology.
There's obviously a lot of theory there that would argue against it, but just in terms of how we use it in abstract.
Maybe we're interested in how those would be used separately. And so we could just get a count of which ones winning.
Do we talk more about culture? Do we talk more about structure? And according to the first five, we talk more about culture.
But culture is structure to some people. So here you can use external dictionaries.
So maybe you want to do the same sort of thing, but you want to look at like Leiweke Dictionary terms.
You have to pay for those which I didn't want to do last night.
So I just gave you the code for how you would import those terms and then you could do the same analysis.
Looking at the frequency over time, you can look at the similarity of texts.
By running this code, I don't know if this will show up, actually.
Based on my cosine similarity of the the term document matrix might take a little bit.
So this is just giving you an example of the the cosine similarity of the first document with with other documents.
You could then use those and as weights and a network analysis or something, I don't I'm not sure you can do the same with specific words.
So if you wanted to look at how race the cosine similarity of race with other with other tokens in your corpus,
it looks like it has a very high similarity with the word racial, which maybe isn't surprising.
If you go down to the gender list, you're kind of intuitive as well.
It's you. So with gender, women is the most similar term associated with, but then men equal differ male.
Remember, we stemmed these so equal to be equality legislation.
So yeah, it's a cosine similarity from the document term frequency matrix.
Yeah. And then.
Oh, we're not going to do this because we have a whole tutorial on structural topic modelling,
but within the quartzite quantita package, you can you can do topic modelling and it's pretty straightforward.
So here you're just identifying how many topics you want and you're saying I'm going to use latent like allocation like.
And then you get the terms and the topic distributions. So if you want, you can start doing topic modelling that way.
Topic models that way. Yeah. OK, it's 3:15.
Should we take a break or no, because I have two other scripts on preprocessing that I've provided,
but you can also just play with those at your leisure. One would be a bit complicated for us to go through,
but it's working with that Google API for detecting named entities, and that is, I think it's just called.
And LP, it's like the MLP tutorial. And there's another one on sentiment analysis, so in the core six at Princeton,
they do more work on sentiment analysis is pretty straightforward, though, and getting the sentiment just give it a dictionary.
And it looks at the words and tells you what the sentiment of those words are by and large like.
That's the most simple form of sentiment analysis. So it's not even a tutorial.
I just provided you a script that you could use if you wanted to look at in case
any of you want to do that in your group project and you want a base to run on. But I'm happy to go through the Google named Entity Thing.
Often there's credential, it's an API, right? So there could be credentialing problems or we can just take a break.
Take a break. OK, I'm going to do that.
Unless it no questions, right? So it's supposed to run the polls on the stock price, and it's like, oh, yeah.
So a lot of what the text analysis stuff. Not necessarily processing but models we run, a lot of them take a long time.
So we're not going to do a lot of it. So but they each HTML and then I'll give you the marked down file later.
If you want it, you have it. You can copy paste later at your leisure.
OK. Yeah. Yeah. Like you said, at that point, it was like they knew the news outlet.
So we treat the text from. Next, Major.
The public's fascination with newspapers and things.
There are there are a lot. Yeah, I'm sure plenty of people in this room might know of more.
I know, for instance, that English corporate dawg that I told you about one of their larger I think it's called Coca S.O.S. a larger corpora.
Has they subdivide the corpus into like magazines, blogs, newspapers and within newspapers?
There's multiple different newspapers, but like New York Times, is kind of epically good of being able to get from their API text.
I have a corpus if you want to just toy with it of New York Times, I think their op ed.