{"id":1527,"date":"2021-04-21T09:35:26","date_gmt":"2021-04-21T13:35:26","guid":{"rendered":"https:\/\/wwp.northeastern.edu\/blog\/?p=1527"},"modified":"2021-08-10T16:07:56","modified_gmt":"2021-08-10T20:07:56","slug":"tokenization-word-embedding-models","status":"publish","type":"post","link":"https:\/\/wwp.northeastern.edu\/blog\/tokenization-word-embedding-models\/","title":{"rendered":"Experiments in Tokenization for Word Embedding Models"},"content":{"rendered":"\r\n<h3><strong>By <a href=\"https:\/\/cssh.northeastern.edu\/student\/laura-johnson\/\">Juniper Johnson<\/a><\/strong><\/h3>\r\n \r\n\r\nDuring my time as the 2019\u20132020 NULab Coordinator, I extended my previous research experience with the Women Writers Project to build an XSLT script for tokenizing element content for the <a href=\"https:\/\/wwp.northeastern.edu\/lab\/wwvt\/\">Women Writers Vector Toolkit (WWVT)<\/a>. The WWVT is an online laboratory for learning about and experimenting with word embedding models and features over <a href=\"http:\/\/lab.wwp.northeastern.edu\/wwvt\/\">20 models created using the Women Writers Online (WWO) corpus<\/a> and parallel corpora created from the Text Creation Partnership collections. The WWVT models are created in R using the package <a href=\"https:\/\/github.com\/bmschmidt\/wordVectors\">\u201cwordVectors\u201d<\/a> developed by Ben Schmidt and Jian Li and, using a spatial framework, can be used to study the relationships between words in a corpus of texts. That is, word embedding models calculate the spatial proximity and relationality of words across an entire corpus that can be explored through different access points and configurations like clusters and single word queries.\r\n\r\n \r\n\r\nThe research capabilities of word embedding models are rich, as <a href=\"https:\/\/wwp.northeastern.edu\/blog\/credit-history\/\">Sarah Connell demonstrates in her exploration of seventeenth-century historiography<\/a>. However, the previous models developed for the WWVT allow users only to interact with a corpora using single word queries. While useful, this approach does have its limitations. For example, if one is interested in geography in the WWO corpus, there are many different terms of interest: perhaps certain cities like <em>London<\/em> and <em>Philadelphia <\/em>or countries like <em>Scotland<\/em> or <em>France<\/em>. However, each of these examples is limited to a single word, and so querying multi-word locations like <em>New York<\/em> and <em>Tower of London<\/em> is less straightforward. When a word embedding model is made using the wordVectors package, a textual corpus is cleaned of certain punctuation marks like periods, asterisks, hyphens, and em dashes. After this process, the text is tokenized into individual units. Tokenization\u2014a fundamental process for natural language processing\u2014strips a string into individual units. Or, in this case, text into individual words. Therefore, when the word embedding model is created, it forms a relational model between the <em>words<\/em> of a corpus, not phrases or concepts.\r\n\r\n \r\n\r\nAfter the first <a href=\"https:\/\/wwp.northeastern.edu\/outreach\/seminars\/neh_wem.html\">Word Vectors for the Thoughtful Humanist institute<\/a> in 2019, the WWVT team sought to create expanded resources for scholars interested in word embedding models. One part of this project was to create a workflow that could provide opportunities to tokenize multiword strings in the word embedding models. In preparing participants\u2019 corpora for the institute, I also developed a <a href=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Data-Preparation-Checklist.pdf\">data preparation checklist<\/a> with stages to identify \u201cnoise\u201d that might disrupt the word embedding model, steps and tools for modifying and cleaning a corpus, and helpful resources for text regularization.\r\n\r\n \r\n\r\nFor XML corpora, an integral step in this workflow is to transform XML files to plaintext, a process in which we use the <a href=\"https:\/\/github.com\/NEU-DSG\/wwp-public-code-share\/tree\/main\/fulltext\">WWP Full Text XSLT on GitHub<\/a>. In order to \u201ctokenize\u201d the content of specific XML elements, an additional step was needed before the documents became plaintext files. In principle, this process is rather straightforward: replace \u201cnoisy\u201d characters in the element content that would be removed during the cleaning process and add underscores between words that should be treated as a single token. In application, however, this step became a bit challenging. When finding, modifying, or removing repetition in data, regular expressions are used to describe a sequence of characters that you wish to isolate. However, to figure out what you need to remove, you must first survey and identify the content that needs to be removed.\r\n\r\n \r\n\r\nTo tokenize the content of &lt;persName&gt; and &lt;placeName&gt; elements in the WWO Corpus, I\u2014aided by the expertise of DSG XML Applications Programmer Ash Clark\u2014surveyed the element contents for problematic punctuation, including hyphens, asterisks, en dashes, and spaces. After identifying the range of characters we needed to remove, we built an XSLT script to remove these characters and replace them with a single underscore. This XSLT script, <a href=\"https:\/\/github.com\/NEU-DSG\/wwp-public-code-share\/blob\/main\/fulltext\/element-tokenizer.xslt\">the Element Tokenizer<\/a>, is available with a <a href=\"https:\/\/github.com\/NEU-DSG\/wwp-public-code-share\/blob\/main\/fulltext\/Element-Tokenizer-README.md\">detailed README tutorial on the WWP GitHub<\/a>.\r\n\r\n \r\n\r\nThe tokenizer chains these transformations by declaring new variables for additional special characters in sequence. This means that the tokenizer processes all of the special characters before replacing them with underscores. These special characters were chosen specifically for the contents of the &lt;persName&gt; and &lt;placeName&gt; elements of the WWO corpus and do not include other punctuation because that either was not included or is cleaned in the model training process. To support work with other corpora, the Element Tokenizer has been annotated and directions for writing additional lines of code for different characters are included in comments.\r\n\r\n \r\n\r\nUsing the Element Tokenizer, we created three distinct word embedding models: one with\u00a0&lt;persName&gt; tokenized, another with &lt;placeName&gt; tokenized, and one with both &lt;persName&gt; and &lt;placeName&gt; tokenized. These models are available to explore now on the <a href=\"http:\/\/lab.wwp.northeastern.edu\/wwvt\/\">WWVT Lab<\/a>. To demonstrate the effects of the tokenization process for words and concepts, the following examples compare two queries for \u201cElizabeth\u201d and \u201cLondon,\u201d each a single word to represent names of people and places. I queried both of these terms in four different models to compare the results: the Full WWO Corpus (without any regularization or tokenization), WWO with Person Names Tokenized, WWO with Place Names Tokenized, and WWO with Place and Person Names Tokenized.\r\n\r\n<figure id=\"attachment_1542\" aria-describedby=\"caption-attachment-1542\" style=\"width: 1026px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1542 size-full\" src=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.11.07-AM.png\" alt=\"\" width=\"1026\" height=\"1162\" srcset=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.11.07-AM.png 1026w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.11.07-AM-265x300.png 265w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.11.07-AM-768x870.png 768w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.11.07-AM-904x1024.png 904w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.11.07-AM-238x270.png 238w\" sizes=\"auto, (max-width: 1026px) 100vw, 1026px\" \/><figcaption id=\"caption-attachment-1542\" class=\"wp-caption-text\">Figure 1: Query for \u201cElizabeth\u201d with WWO Full Corpus<\/figcaption><\/figure>\r\n\r\n<figure id=\"attachment_1543\" aria-describedby=\"caption-attachment-1543\" style=\"width: 1044px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1543 size-full\" src=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.12.27-AM.png\" alt=\"\" width=\"1044\" height=\"1172\" srcset=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.12.27-AM.png 1044w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.12.27-AM-267x300.png 267w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.12.27-AM-768x862.png 768w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.12.27-AM-912x1024.png 912w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.12.27-AM-241x270.png 241w\" sizes=\"auto, (max-width: 1044px) 100vw, 1044px\" \/><figcaption id=\"caption-attachment-1543\" class=\"wp-caption-text\">Figure 1: WWO Corpus with Person Names Tokenized<\/figcaption><\/figure>\r\n\r\n<span style=\"font-size: 16px;\">For the query term \u201cElizabeth\u201d (Figure 1) the effects of tokenizing the &lt;persName&gt; element is striking; instead of generating a list of names that could be either first names or middle names with Elizabeth (i.e. Mary Elizabeth) or other possible women relations, the query for the tokenized model results in more proper, specific names: Sir William Cavendish, Princess Elizabeth, and Sir Thomas Moore. For a corpus like WWO\u2014which includes many frequent, common names, like Elizabeth\u2014tokenizing &lt;persName&gt; elements points to the possibility of more specificity to identify individuals in word embedding models, as demonstrated by the different vectors for \u201celizabeth\u201d and \u201cprincess_elizabeth.\u201d\u00a0<\/span>\r\n\r\n<figure id=\"attachment_1544\" aria-describedby=\"caption-attachment-1544\" style=\"width: 1022px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1544\" src=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.14.18-AM.png\" alt=\"\" width=\"1022\" height=\"1166\" srcset=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.14.18-AM.png 1022w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.14.18-AM-263x300.png 263w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.14.18-AM-768x876.png 768w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.14.18-AM-898x1024.png 898w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.14.18-AM-237x270.png 237w\" sizes=\"auto, (max-width: 1022px) 100vw, 1022px\" \/><figcaption id=\"caption-attachment-1544\" class=\"wp-caption-text\">Figure 2: Query results for \u201cLondon\u201d with WWO Full Corpus<\/figcaption><\/figure>\r\n\r\n<figure id=\"attachment_1545\" aria-describedby=\"caption-attachment-1545\" style=\"width: 1042px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1545\" src=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.15.23-AM.png\" alt=\"\" width=\"1042\" height=\"1150\" srcset=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.15.23-AM.png 1042w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.15.23-AM-272x300.png 272w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.15.23-AM-768x848.png 768w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.15.23-AM-928x1024.png 928w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.15.23-AM-245x270.png 245w\" sizes=\"auto, (max-width: 1042px) 100vw, 1042px\" \/><figcaption id=\"caption-attachment-1545\" class=\"wp-caption-text\">Figure 2: WWO Corpus with Place Names Tokenized<\/figcaption><\/figure>\r\n\r\nSimilarly, for the query term \u201cLondon\u201d (Figure 2), there is a difference between the non-tokenized corpora results that points to specificity: in the WWO Full Corpus, the term <em>London<\/em> is connected to both possible place and person names, including \u201cBently\u201d and \u201cJ.M.\u201d However, the tokenized model results in specific locations and\/or concepts including St. Paul\u2019s Churchyard, the Strand, and Warwick Lane. For texts where proper names for people and places are instrumental to the concepts that researchers might be interested in using word embedding models for, tokenizing specific element contents is an exciting possibility to add another layer of analysis.\r\n\r\n<figure id=\"attachment_1546\" aria-describedby=\"caption-attachment-1546\" style=\"width: 1026px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1546 size-full\" src=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.16.10-AM.png\" alt=\"\" width=\"1026\" height=\"1174\" srcset=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.16.10-AM.png 1026w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.16.10-AM-262x300.png 262w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.16.10-AM-768x879.png 768w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.16.10-AM-895x1024.png 895w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.16.10-AM-236x270.png 236w\" sizes=\"auto, (max-width: 1026px) 100vw, 1026px\" \/><figcaption id=\"caption-attachment-1546\" class=\"wp-caption-text\">Figure 3: Query results for \u201cLondon\u201d\u00a0with WWO Corpus with Place Name and Person Names Tokenized<\/figcaption><\/figure>\r\n\r\n<figure id=\"attachment_1548\" aria-describedby=\"caption-attachment-1548\" style=\"width: 1022px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1548\" src=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.17.44-AM.png\" alt=\"\" width=\"1022\" height=\"1158\" srcset=\"https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.17.44-AM.png 1022w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.17.44-AM-265x300.png 265w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.17.44-AM-768x870.png 768w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.17.44-AM-904x1024.png 904w, https:\/\/wwp.northeastern.edu\/blog\/wp-content\/uploads\/2021\/04\/Screen-Shot-2021-04-21-at-9.17.44-AM-238x270.png 238w\" sizes=\"auto, (max-width: 1022px) 100vw, 1022px\" \/><figcaption id=\"caption-attachment-1548\" class=\"wp-caption-text\">Figure 3: Query results for \u201cElizabeth\u201d with WWO Corpus with Place Name and Person Names Tokenized<\/figcaption><\/figure>\r\n\r\n \r\n\r\nWhen you tokenize the contents of more than one element for a specific model (Figure 3), there is the possibility of overlap in concepts or topics like, in the above example, people and places. For example, the query term \u201cLondon\u201d in the Tokenized Person and Place Name model results in specific names and places like J.Johnson, and J. Robinson, The Strand, Middle Temple, and Queen Street. Without specific subject expertise or familiarity with the WWP corpora, an initial estimate could be a relationship between authors and locations of printers and print shops (as evidenced by the Strand). These types of names and places largely feature on title pages, so, to determine a more specific relationship, one could look at the location of these names within the original XML corpora for either a title page or other major structural elements. While this is only a speculation, this simple example with just two query terms demonstrates the possibility for specificity in results and spatial relationships when tokenizing element contents in XML corpora when using word embedding models.\r\n\r\n \r\n\r\nWhile the Element Tokenizer XSLT script was developed for a very specific computational workflow for generating word embedding models from XML textual data, it demonstrates the possibility of altering existing workflows with simple tools for increased specificity, exploration, and access for subjects in computational textual analysis. For more reading about specific explorations of participant corpora from the WWP Word Vectors for the Thoughtful Humanist Institute Series, see the following blog post series:\r\n\r\n \r\n\r\nBlog Posts from 2019 Institute Participants\r\nJuniper Johnson: <a href=\"https:\/\/wwp.northeastern.edu\/blog\/archival-lgbt\/\">https:\/\/wwp.northeastern.edu\/blog\/archival-lgbt\/<\/a>\r\nSarah Connell: <a href=\"https:\/\/wwp.northeastern.edu\/blog\/credit-history\/\">https:\/\/wwp.northeastern.edu\/blog\/credit-history\/<\/a>\r\nJames Clawson: <a href=\"https:\/\/jmclawson.net\/blog\/posts\/updates-to-word-vector-utilities\/\">https:\/\/jmclawson.net\/blog\/posts\/updates-to-word-vector-utilities<\/a>\r\n<a href=\"https:\/\/wwp.northeastern.edu\/blog\/word-embedding-model-materialism-spiritualism\/\">https:\/\/wwp.northeastern.edu\/blog\/word-embedding-model-materialism-spiritualism\/<\/a>\r\nBecky Standard: <a href=\"https:\/\/wwp.northeastern.edu\/blog\/a-most-illustrious-and-distinctive-career\/\">https:\/\/wwp.northeastern.edu\/blog\/a-most-illustrious-and-distinctive-career\/<\/a>\r\n\r\n \r\n\r\n<strong>Juniper Johnson<\/strong> is a third-year Ph.D. student in the English Department studying Literature at Northeastern University. They focus on archives, queer and feminist theory, digital humanities, and eighteenth- and nineteenth-century American literature. Their research interests include archives and finding aids as data, writing about the body and sexuality in science and medicine, the history of queer theory, and disability studies.\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>By Juniper Johnson During my time as the 2019\u20132020 NULab Coordinator, I extended my previous research experience with the Women Writers Project to build an XSLT script for tokenizing element content for the Women Writers Vector Toolkit (WWVT). The WWVT is an online laboratory for learning about and experimenting with word embedding models and features over 20 models created using the Women Writers Online (WWO) corpus and parallel corpora created from the Text Creation Partnership collections. The WWVT models are&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/wwp.northeastern.edu\/blog\/tokenization-word-embedding-models\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-1527","post","type-post","status-publish","format-standard","hentry","category-word-vectors"],"_links":{"self":[{"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/posts\/1527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/comments?post=1527"}],"version-history":[{"count":10,"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/posts\/1527\/revisions"}],"predecessor-version":[{"id":1558,"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/posts\/1527\/revisions\/1558"}],"wp:attachment":[{"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/media?parent=1527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/categories?post=1527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wwp.northeastern.edu\/blog\/wp-json\/wp\/v2\/tags?post=1527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}