{"word_count": 1515, "character_count": 10034}
Last edit: June 7th, 2019BNC.2014.query() is an interactive R script that I wrote for a course in computational sociolinguistics last semester. It is designed to run queries over the BNC-2014 (spoken component). It extracts a single word or a complex expression, along with speaker information (gender, age class, and social grade), tabulates the results, computes frequencies, and makes a barplot or an association plot (along with a χ2&#160;test).For this demo, I have used&#160;R version 3.5.1 (2018-07-02) -- "Feather Spray" on RStudio (Version 1.1.463). I cannot guarantee that the script will work on previous versions.&#160;Obtaining the corpusFor the script to run properly,&#160;the BNC-2014 files must be installed on your system. To download the BNC-2014 for free, visit&#160;this page&#160;and&#160;complete the signup form at the bottom of the page.&#160;Once you have downloaded the corpus, unzip it and place it&#160;preferably at the root of your hard drive. The architecture of the bnc2014spoken directory is displayed below.The path to the folder that contains the corpus filesAt some point, the script will need the path to the directory that contains the tagged corpus files. If you have installed the corpus at the root of your hard drive, the path is likely to be the following:C:\\bnc2014spoken\\spoken\\tagged (PC);/bnc2014spoken/spoken/tagged&#160;(Mac).If you want to avoid retyping the path each time you run a query, save it in a text file somewhere to copy/paste it later on.Launching the programDownload, install, and start R.Before you run BNC.2014.query(), I advise you to check your working directory. The working directory is a folder which you want R to read data from and store output into. This is where the script will save your data (4 files). To know what your current working directory is, enter:getwd()To change the working directory, enter its path between quotes as an argument of setwd().&#160;For example, if I want my working directory to be&#160;/Users/guillaumedesagulier, I enter:setwd("/Users/guillaumedesagulier")Type the following lines, and press ENTER:rm(list=ls(all=TRUE))source("https://www.nakala.fr/nakala/data/11280/de1d18d2")BNC.2014.query()The first line clears R's memory. The second line downloads the script. The third line launches the program.Loading the required packagesThe script needs four R packages:gsubfnggplot2plyrstringiIf these packages are installed, the script proceeds to the next step. If they are not, the scripts downloads and installs them.Formulating the queryFirst, the program prompts the user to input the corpus query.Your can search for:a simple lexeme (e.g. "hello");several simple lexemes ("hello" and "hi");a complex expression (e.g. "good morning");several complex expressions (e.g. "good morning", "good afternoon", "good evening").The query protocol is based on R-compatible regular expressions. For each of the above query, here is the corresponding expression:wordwhat to type in R"hello"hello"hello" and "hi"(hello|hi)"good morning"good morning"good morning", "good afternoon", "good evening"good (morning|afternoon|evening)In the above code, | means "or". For more information on character-string processing and regular expressions, see chapters 4 and 5 of Corpus Linguistics and Statistics with R.For the purpose of illustration, we compare the use of "hello" and "hi". The search expression is (hi|hello).Note that if you want to compare a one-word expression and a multiword expression (e.g. "hello" and "good morning"), you are going to have to run two separate queries. (( If you want to plot the results, you can easily merge the output files later on. ))If you investigate a single lexeme or several simple lexemes, you may specify a POS tag. This option is not available for multiword expressions. Given that BNC-2014 was tagged with CLAWS7, read the tagset and enter the correct tag. Both "hello" and "hi" are interjections. The CLAWS7 tag for interjections is UH. If you do not want to bother looking for the tag, if you think the tag is irrelevant, or if the lexemes you investigate belong to different grammatical categories, enter \w+ (this is computer language for 'one or more word characters', i.e. any tag).The script converts your search expression into XML...Entering the path to the directory that contains the corpus filesNext, the scripts prompts you to enter the path to the directory that contains the corpus files. If you work with a PC, don't forget to double each backslash (see above).Running the query over the whole corpus or just a sampleAfter entering the path to the corpus files, the script asks you if you want to run the query over the whole corpus (1251 files) or just a sample (100 files). The latter is much faster than the former and useful if you are not sure about the outcome of your query. Here, we enter 1 to run the query over the whole corpus.The script prints the paths to a random selection of ten corpus files. This is just to check that the path to the directory that contains them has been typed correctly. Press ENTER to launch the query.Now, the script loops over each corpus file. Sit back, relax, and wait. A progress bar indicates how much time you have to drink that coffee while the program is working.On my Mac, the script takes between 5 and 10 minutes to complete when all the corpus files are selected. (( MacBook Pro (13-inch, 2017); Processor: 3.3 GHz Intel Core i5; Memory: 16 GB 2133 MHz LPDDR3. ))Once the script has finished looping over the files, press ENTER to save the results in a text file named &#60;interim.results.BNC.2014.txt&#62;.(( The file is also saved as an R data file named &#60;interim.results.BNC.2014.rds&#62;. )) Here is a snaspshot of the file. There is one line per observation. Each word is described by the ID of the speaker who used it.Next,&#160;press ENTER to save the results in a text file named &#60;data.final.BNC.2014.txt&#62;. (( The file is also saved as an R data file named &#60;data.final.BNC.2014.rds&#62;. ))The first 20 lines of&#160;&#60;data.final.BNC.2014.txt&#62; are displayed. Each observation consists of the speaker ID, the word itself, the age group, the gender, the speaker's city, the speaker's dialect, and the social grade. Read the BNC-2014 documentation to know more about these variables.You may now plot the results. Three options are available:a barplot based on raw frequencies,an association plot,no plot (+ archive working files).The association plot makes sense only when your study consists of at least two words or expressions.For the working files to be archived means that they are time-stamped. This prevents the script from over-writing your working files when you run another query.We choose the association plot. We can investigate three effects:gender,age,social class.We choose age.The script provides the table of counts with the variable(s) in the rows and the age groups in the columns. It also runs a χ2&#160;test. If your table does not meet the test's assumptions, an error message may be issued. This is likely to occur if you choose to work on a portion of the corpus only. (see Corpus Linguistics and Statistics with R, Sect. 8.9)The association plot below can be exported as an image or as pdf from R or RStudio.&#160;Each cell of the table of counts is represented by a tile whose area is proportional to the difference between observed and expected frequencies. The dotted line is the baseline. It represents independence between the variables. If the observed frequency of a cell is greater than its expected frequency, the tile appears above the baseline and is shaded black. If the observed frequency of a cell is smaller than its expected frequency, the tile appears below the baseline and is shaded red.&#160;We see that speakers from age groups 19-29 and, to a much lesser extent 60-69, prefer hi over hello. (( For more details on how to interpret an association plot, see Corpus Linguistics and Statistics with R, pp. 184-185. ))You now have the choice between running another query or exiting the program. If you choose to exit, the script adds a time stamp to your working files, for archiving purposes and later re-use. If my working files are time-stamped as follows:&#60;interim.results.BNC.2014_29Dec2018121603.txt&#62;, and&#160;&#60;data.final.BNC.2014_29Dec2018121603.txt&#62;,this means that they were created on December 29th, 2018 at 12.16pm (and 3 seconds!). Again, as long as you do not run the script from scratch again, your current working files are still intact.Built-in plotting functionsIf you want to make several plots, you can do so by using a built-in function. This will only work for the current session, i.e. as long as the working files have not been over-written. Here is the list of functions:function callwhat it doesmake.barplot()opens the barplot menubarplot.gender()makes a barplot based on genderbarplot.age()makes a barplot based on age groupsbarplot.social.class()makes a barplot based on social gradesmake.assocplot()opens the association plot menuassplot.gender()makes an association plot based on genderassplot.age()makes an association plot based on age groupsassplot.soc.class()makes an association plot based on social gradesThe barplot below is obtained by entering barplot.age(). It is made with ggplot2.Video recapThe video below has been edited. In real life, the script takes slightly longer to run. The processing time depends on your hardware.FeedbackThis program is still in beta. If you experience bugs or think of better or faster functionalities, please leave a comment.Citing the programIf you use the program for your research, please cite it as follows:Desagulier, Guillaume. 2019. BNC.2014.query(), v. 0.3. An R script for a sociolinguistic exploration of the spoken component of the BNC 2014.This program is protected by the following Creative Commons licence:  CC-BY-NC-ND 4.0.[cite]Cite this article as: {author}, "{title}," in {sitename}, {publication_date}, {permalink}.[/cite]Marsaz, Drôme, France - Jan 2019