| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
<title>Unicode: Emoji, accents, and international text</title> |
| 16 |
|
| 17 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 18 |
// be compatible with the behavior of Pandoc < 2.8). |
| 19 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 20 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 21 |
var i, h, a; |
| 22 |
for (i = 0; i < hs.length; i++) { |
| 23 |
h = hs[i]; |
| 24 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 25 |
a = h.attributes; |
| 26 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 27 |
} |
| 28 |
}); |
| 29 |
</script> |
| 30 |
|
| 31 |
<style type="text/css"> |
| 32 |
code{white-space: pre-wrap;} |
| 33 |
span.smallcaps{font-variant: small-caps;} |
| 34 |
span.underline{text-decoration: underline;} |
| 35 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 36 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 37 |
ul.task-list{list-style: none;} |
| 38 |
</style> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
<style type="text/css"> |
| 43 |
code { |
| 44 |
white-space: pre; |
| 45 |
} |
| 46 |
.sourceCode { |
| 47 |
overflow: visible; |
| 48 |
} |
| 49 |
</style> |
| 50 |
<style type="text/css" data-origin="pandoc"> |
| 51 |
html { -webkit-text-size-adjust: 100%; } |
| 52 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 53 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 54 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 55 |
.sourceCode { overflow: visible; } |
| 56 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 57 |
div.sourceCode { margin: 1em 0; } |
| 58 |
pre.sourceCode { margin: 0; } |
| 59 |
@media screen { |
| 60 |
div.sourceCode { overflow: auto; } |
| 61 |
} |
| 62 |
@media print { |
| 63 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 64 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 65 |
} |
| 66 |
pre.numberSource code |
| 67 |
{ counter-reset: source-line 0; } |
| 68 |
pre.numberSource code > span |
| 69 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 70 |
pre.numberSource code > span > a:first-child::before |
| 71 |
{ content: counter(source-line); |
| 72 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 73 |
border: none; display: inline-block; |
| 74 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 75 |
-khtml-user-select: none; -moz-user-select: none; |
| 76 |
-ms-user-select: none; user-select: none; |
| 77 |
padding: 0 4px; width: 4em; |
| 78 |
color: #aaaaaa; |
| 79 |
} |
| 80 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 81 |
div.sourceCode |
| 82 |
{ } |
| 83 |
@media screen { |
| 84 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 85 |
} |
| 86 |
code span.al { color: #ff0000; font-weight: bold; } |
| 87 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 88 |
code span.at { color: #7d9029; } |
| 89 |
code span.bn { color: #40a070; } |
| 90 |
code span.bu { color: #008000; } |
| 91 |
code span.cf { color: #007020; font-weight: bold; } |
| 92 |
code span.ch { color: #4070a0; } |
| 93 |
code span.cn { color: #880000; } |
| 94 |
code span.co { color: #60a0b0; font-style: italic; } |
| 95 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 96 |
code span.do { color: #ba2121; font-style: italic; } |
| 97 |
code span.dt { color: #902000; } |
| 98 |
code span.dv { color: #40a070; } |
| 99 |
code span.er { color: #ff0000; font-weight: bold; } |
| 100 |
code span.ex { } |
| 101 |
code span.fl { color: #40a070; } |
| 102 |
code span.fu { color: #06287e; } |
| 103 |
code span.im { color: #008000; font-weight: bold; } |
| 104 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 105 |
code span.kw { color: #007020; font-weight: bold; } |
| 106 |
code span.op { color: #666666; } |
| 107 |
code span.ot { color: #007020; } |
| 108 |
code span.pp { color: #bc7a00; } |
| 109 |
code span.sc { color: #4070a0; } |
| 110 |
code span.ss { color: #bb6688; } |
| 111 |
code span.st { color: #4070a0; } |
| 112 |
code span.va { color: #19177c; } |
| 113 |
code span.vs { color: #4070a0; } |
| 114 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 115 |
</style> |
| 116 |
<script> |
| 117 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 118 |
(function() { |
| 119 |
var sheets = document.styleSheets; |
| 120 |
for (var i = 0; i < sheets.length; i++) { |
| 121 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 122 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 123 |
var j = 0; |
| 124 |
while (j < rules.length) { |
| 125 |
var rule = rules[j]; |
| 126 |
// check if there is a div.sourceCode rule |
| 127 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 128 |
j++; |
| 129 |
continue; |
| 130 |
} |
| 131 |
var style = rule.style.cssText; |
| 132 |
// check if color or background-color is set |
| 133 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 134 |
j++; |
| 135 |
continue; |
| 136 |
} |
| 137 |
// replace div.sourceCode by a pre.sourceCode rule |
| 138 |
sheets[i].deleteRule(j); |
| 139 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 140 |
} |
| 141 |
} |
| 142 |
})(); |
| 143 |
</script> |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
<style type="text/css">body { |
| 149 |
background-color: #fff; |
| 150 |
margin: 1em auto; |
| 151 |
max-width: 700px; |
| 152 |
overflow: visible; |
| 153 |
padding-left: 2em; |
| 154 |
padding-right: 2em; |
| 155 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 156 |
font-size: 14px; |
| 157 |
line-height: 1.35; |
| 158 |
} |
| 159 |
#TOC { |
| 160 |
clear: both; |
| 161 |
margin: 0 0 10px 10px; |
| 162 |
padding: 4px; |
| 163 |
width: 400px; |
| 164 |
border: 1px solid #CCCCCC; |
| 165 |
border-radius: 5px; |
| 166 |
background-color: #f6f6f6; |
| 167 |
font-size: 13px; |
| 168 |
line-height: 1.3; |
| 169 |
} |
| 170 |
#TOC .toctitle { |
| 171 |
font-weight: bold; |
| 172 |
font-size: 15px; |
| 173 |
margin-left: 5px; |
| 174 |
} |
| 175 |
#TOC ul { |
| 176 |
padding-left: 40px; |
| 177 |
margin-left: -1.5em; |
| 178 |
margin-top: 5px; |
| 179 |
margin-bottom: 5px; |
| 180 |
} |
| 181 |
#TOC ul ul { |
| 182 |
margin-left: -2em; |
| 183 |
} |
| 184 |
#TOC li { |
| 185 |
line-height: 16px; |
| 186 |
} |
| 187 |
table { |
| 188 |
margin: 1em auto; |
| 189 |
border-width: 1px; |
| 190 |
border-color: #DDDDDD; |
| 191 |
border-style: outset; |
| 192 |
border-collapse: collapse; |
| 193 |
} |
| 194 |
table th { |
| 195 |
border-width: 2px; |
| 196 |
padding: 5px; |
| 197 |
border-style: inset; |
| 198 |
} |
| 199 |
table td { |
| 200 |
border-width: 1px; |
| 201 |
border-style: inset; |
| 202 |
line-height: 18px; |
| 203 |
padding: 5px 5px; |
| 204 |
} |
| 205 |
table, table th, table td { |
| 206 |
border-left-style: none; |
| 207 |
border-right-style: none; |
| 208 |
} |
| 209 |
table thead, table tr.even { |
| 210 |
background-color: #f7f7f7; |
| 211 |
} |
| 212 |
p { |
| 213 |
margin: 0.5em 0; |
| 214 |
} |
| 215 |
blockquote { |
| 216 |
background-color: #f6f6f6; |
| 217 |
padding: 0.25em 0.75em; |
| 218 |
} |
| 219 |
hr { |
| 220 |
border-style: solid; |
| 221 |
border: none; |
| 222 |
border-top: 1px solid #777; |
| 223 |
margin: 28px 0; |
| 224 |
} |
| 225 |
dl { |
| 226 |
margin-left: 0; |
| 227 |
} |
| 228 |
dl dd { |
| 229 |
margin-bottom: 13px; |
| 230 |
margin-left: 13px; |
| 231 |
} |
| 232 |
dl dt { |
| 233 |
font-weight: bold; |
| 234 |
} |
| 235 |
ul { |
| 236 |
margin-top: 0; |
| 237 |
} |
| 238 |
ul li { |
| 239 |
list-style: circle outside; |
| 240 |
} |
| 241 |
ul ul { |
| 242 |
margin-bottom: 0; |
| 243 |
} |
| 244 |
pre, code { |
| 245 |
background-color: #f7f7f7; |
| 246 |
border-radius: 3px; |
| 247 |
color: #333; |
| 248 |
white-space: pre-wrap; |
| 249 |
} |
| 250 |
pre { |
| 251 |
border-radius: 3px; |
| 252 |
margin: 5px 0px 10px 0px; |
| 253 |
padding: 10px; |
| 254 |
} |
| 255 |
pre:not([class]) { |
| 256 |
background-color: #f7f7f7; |
| 257 |
} |
| 258 |
code { |
| 259 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 260 |
font-size: 85%; |
| 261 |
} |
| 262 |
p > code, li > code { |
| 263 |
padding: 2px 0px; |
| 264 |
} |
| 265 |
div.figure { |
| 266 |
text-align: center; |
| 267 |
} |
| 268 |
img { |
| 269 |
background-color: #FFFFFF; |
| 270 |
padding: 2px; |
| 271 |
border: 1px solid #DDDDDD; |
| 272 |
border-radius: 3px; |
| 273 |
border: 1px solid #CCCCCC; |
| 274 |
margin: 0 5px; |
| 275 |
} |
| 276 |
h1 { |
| 277 |
margin-top: 0; |
| 278 |
font-size: 35px; |
| 279 |
line-height: 40px; |
| 280 |
} |
| 281 |
h2 { |
| 282 |
border-bottom: 4px solid #f7f7f7; |
| 283 |
padding-top: 10px; |
| 284 |
padding-bottom: 2px; |
| 285 |
font-size: 145%; |
| 286 |
} |
| 287 |
h3 { |
| 288 |
border-bottom: 2px solid #f7f7f7; |
| 289 |
padding-top: 10px; |
| 290 |
font-size: 120%; |
| 291 |
} |
| 292 |
h4 { |
| 293 |
border-bottom: 1px solid #f7f7f7; |
| 294 |
margin-left: 8px; |
| 295 |
font-size: 105%; |
| 296 |
} |
| 297 |
h5, h6 { |
| 298 |
border-bottom: 1px solid #ccc; |
| 299 |
font-size: 105%; |
| 300 |
} |
| 301 |
a { |
| 302 |
color: #0033dd; |
| 303 |
text-decoration: none; |
| 304 |
} |
| 305 |
a:hover { |
| 306 |
color: #6666ff; } |
| 307 |
a:visited { |
| 308 |
color: #800080; } |
| 309 |
a:visited:hover { |
| 310 |
color: #BB00BB; } |
| 311 |
a[href^="http:"] { |
| 312 |
text-decoration: underline; } |
| 313 |
a[href^="https:"] { |
| 314 |
text-decoration: underline; } |
| 315 |
|
| 316 |
code > span.kw { color: #555; font-weight: bold; } |
| 317 |
code > span.dt { color: #902000; } |
| 318 |
code > span.dv { color: #40a070; } |
| 319 |
code > span.bn { color: #d14; } |
| 320 |
code > span.fl { color: #d14; } |
| 321 |
code > span.ch { color: #d14; } |
| 322 |
code > span.st { color: #d14; } |
| 323 |
code > span.co { color: #888888; font-style: italic; } |
| 324 |
code > span.ot { color: #007020; } |
| 325 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 326 |
code > span.fu { color: #900; font-weight: bold; } |
| 327 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 328 |
</style> |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
</head> |
| 334 |
|
| 335 |
<body> |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
<h1 class="title toc-ignore">Unicode: Emoji, accents, and international |
| 341 |
text</h1> |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
<div id="character-encoding" class="section level2"> |
| 346 |
<h2>Character encoding</h2> |
| 347 |
<p>Before we can analyze a text in R, we first need to get its digital |
| 348 |
representation, a sequence of ones and zeros. In practice this works by |
| 349 |
first choosing an <em>encoding</em> for the text that assigns each |
| 350 |
character a numerical value, and then translating the sequence of |
| 351 |
characters in the text to the corresponding sequence of numbers |
| 352 |
specified by the encoding. Today, most new text is encoded according to |
| 353 |
the <a href="http://unicode.org/charts/">Unicode standard</a>, |
| 354 |
specifically the 8-bit block Unicode Transfer Format, <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. Joel Spolsky gives |
| 355 |
a good overview of the situation in an <a href="https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/">essay |
| 356 |
from 2003</a>.</p> |
| 357 |
<p>The software community has mostly moved to UTF-8 as a standard for |
| 358 |
text storage and interchange, but there is still a large volume of text |
| 359 |
in other encodings. Whenever you read a text file into R, you need to |
| 360 |
specify the encoding. If you don’t, R will try to guess the encoding, |
| 361 |
and if it guesses incorrectly, it will wrongly interpret the sequence of |
| 362 |
ones and zeros.</p> |
| 363 |
<p>We will demonstrate the difficulties of encodings with the text of |
| 364 |
Jane Austen’s novel, <em>Mansfield Park</em> provided by <a href="http://www.gutenberg.org">Project Gutenberg</a>. We will download |
| 365 |
the text, then read in the lines of the novel.</p> |
| 366 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co"># download the zipped text from a Project Gutenberg mirror</span></span> |
| 367 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a>url <span class="ot"><-</span> <span class="st">"http://mirror.csclub.uwaterloo.ca/gutenberg/1/4/141/141.zip"</span></span> |
| 368 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a>tmp <span class="ot"><-</span> <span class="fu">tempfile</span>()</span> |
| 369 |
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">download.file</span>(url, tmp)</span> |
| 370 |
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a></span> |
| 371 |
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="co"># read the text from the zip file</span></span> |
| 372 |
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a>con <span class="ot"><-</span> <span class="fu">unz</span>(tmp, <span class="st">"141.txt"</span>, <span class="at">encoding =</span> <span class="st">"UTF-8"</span>)</span> |
| 373 |
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a>lines <span class="ot"><-</span> <span class="fu">readLines</span>(con)</span> |
| 374 |
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="fu">close</span>(con)</span></code></pre></div> |
| 375 |
<p>The <code>unz</code> function and other similar file connection |
| 376 |
functions have <code>encoding</code> arguments which, if left |
| 377 |
unspecified default to assuming that text is encoded in your operating |
| 378 |
system’s native encoding. To ensure consistent behavior across all |
| 379 |
platforms (Mac, Windows, and Linux), you should set this option |
| 380 |
explicitly. Here, we set <code>encoding = "UTF-8"</code>. This is a |
| 381 |
reasonable default, but it is not always appropriate. In general, you |
| 382 |
should determine the appropriate <code>encoding</code> value by looking |
| 383 |
at the file. Unfortunately, the file extension <code>".txt"</code> is |
| 384 |
not informative, and could correspond to any encoding. However, if we |
| 385 |
read the first few lines of the file, we see the following:</p> |
| 386 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>lines[<span class="dv">11</span><span class="sc">:</span><span class="dv">20</span>]</span></code></pre></div> |
| 387 |
<pre><code> [1] "Author: Jane Austen" |
| 388 |
[2] "" |
| 389 |
[3] "Release Date: June, 1994 [Etext #141]" |
| 390 |
[4] "Posting Date: February 11, 2015" |
| 391 |
[5] "" |
| 392 |
[6] "Language: English" |
| 393 |
[7] "" |
| 394 |
[8] "Character set encoding: ASCII" |
| 395 |
[9] "" |
| 396 |
[10] "*** START OF THIS PROJECT GUTENBERG EBOOK MANSFIELD PARK ***"</code></pre> |
| 397 |
<p>The character set encoding is reported as ASCII, which is a subset of |
| 398 |
UTF-8. So, we should be in good shape.</p> |
| 399 |
<p>Unfortunately, we run into trouble as soon as we try to process the |
| 400 |
text:</p> |
| 401 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>corpus<span class="sc">::</span><span class="fu">term_stats</span>(lines) <span class="co"># produces an error</span></span></code></pre></div> |
| 402 |
<pre><code>Error in corpus::term_stats(lines): argument entry 15252 is incorrectly marked as "UTF-8": invalid leading byte (0xA3) at position 36</code></pre> |
| 403 |
<p>The error message tells us that line 15252 contains an invalid |
| 404 |
byte.</p> |
| 405 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>lines[<span class="dv">15252</span>]</span></code></pre></div> |
| 406 |
<pre><code>[1] "the command of her beauty, and her \xa320,000, any one who could satisfy the"</code></pre> |
| 407 |
<p>We might wonder if there are other lines with invalid data. We can |
| 408 |
find all such lines using the <code>utf8_valid</code> function:</p> |
| 409 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>lines[<span class="sc">!</span><span class="fu">utf8_valid</span>(lines)]</span></code></pre></div> |
| 410 |
<pre><code>[1] "the command of her beauty, and her \xa320,000, any one who could satisfy the"</code></pre> |
| 411 |
<p>So, there are no other invalid lines.</p> |
| 412 |
<p>The offending byte in line 15252 is displayed as <code>\xa3</code>, |
| 413 |
an escape code for hexadecimal value 0xa3, decimal value 163. To |
| 414 |
understand why this is invalid, we need to learn more about UTF-8 |
| 415 |
encoding.</p> |
| 416 |
</div> |
| 417 |
<div id="utf-8" class="section level2"> |
| 418 |
<h2>UTF-8</h2> |
| 419 |
<div id="ascii" class="section level3"> |
| 420 |
<h3>ASCII</h3> |
| 421 |
<p>The smallest unit of data transfer on modern computers is the byte, a |
| 422 |
sequence of eight ones and zeros that can encode a number between 0 and |
| 423 |
255 (hexadecimal 0x00 and 0xff). In the earliest character encodings, |
| 424 |
the numbers from 0 to 127 (hexadecimal 0x00 to 0x7f) were standardized |
| 425 |
in an encoding known as ASCII, the American Standard Code for |
| 426 |
Information Interchange. Here are the characters corresponding to these |
| 427 |
codes:</p> |
| 428 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>codes <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">127</span>, <span class="dv">8</span>, <span class="dv">16</span>, <span class="at">byrow =</span> <span class="cn">TRUE</span>,</span> |
| 429 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a> <span class="at">dimnames =</span> <span class="fu">list</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">7</span>, <span class="fu">c</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">9</span>, letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>])))</span> |
| 430 |
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a>ascii <span class="ot"><-</span> <span class="fu">apply</span>(codes, <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>), intToUtf8)</span> |
| 431 |
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a></span> |
| 432 |
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co"># replace control codes with ""</span></span> |
| 433 |
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a>ascii[<span class="st">"0"</span>, <span class="fu">c</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">6</span>, <span class="st">"e"</span>, <span class="st">"f"</span>)] <span class="ot"><-</span> <span class="st">""</span></span> |
| 434 |
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a>ascii[<span class="st">"1"</span>,] <span class="ot"><-</span> <span class="st">""</span></span> |
| 435 |
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a>ascii[<span class="st">"7"</span>, <span class="st">"f"</span>] <span class="ot"><-</span> <span class="st">""</span></span> |
| 436 |
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a></span> |
| 437 |
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="fu">utf8_print</span>(ascii, <span class="at">quote =</span> <span class="cn">FALSE</span>)</span></code></pre></div> |
| 438 |
<pre><code> 0 1 2 3 4 5 6 7 8 9 a b c d e f |
| 439 |
0 \a \b \t \n \v \f \r |
| 440 |
1 |
| 441 |
2 ! " # $ % & ' ( ) * + , - . / |
| 442 |
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| 443 |
4 @ A B C D E F G H I J K L M N O |
| 444 |
5 P Q R S T U V W X Y Z [ \\ ] ^ _ |
| 445 |
6 ` a b c d e f g h i j k l m n o |
| 446 |
7 p q r s t u v w x y z { | } ~ </code></pre> |
| 447 |
<p>The first 32 codes (the first two rows of the table) are special |
| 448 |
control codes, the most common of which, <code>0x0a</code> denotes a new |
| 449 |
line (<code>\n</code>). The special code <code>0x00</code> often denotes |
| 450 |
the end of the input, and R does not allow this value in character |
| 451 |
strings. Code <code>0x7f</code> corresponds to a “delete” control.</p> |
| 452 |
<p>When you call <code>utf8_print</code>, it uses the low level |
| 453 |
<code>utf8_encode</code> subroutine format control codes; they format as |
| 454 |
<code>\uXXXX</code> for four hexadecimal digits <code>XXXX</code> or as |
| 455 |
<code>\UXXXXYYYY</code> for eight hexadecimal digits |
| 456 |
<code>XXXXYYYY</code>:</p> |
| 457 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="fu">utf8_print</span>(<span class="fu">intToUtf8</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">0x0f</span>), <span class="at">quote =</span> <span class="cn">FALSE</span>)</span></code></pre></div> |
| 458 |
<pre><code>[1] \u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n\v\f\r\u000e\u000f</code></pre> |
| 459 |
<p>Compare <code>utf8_print</code> output with the output with the base |
| 460 |
R print function:</p> |
| 461 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">print</span>(<span class="fu">intToUtf8</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">0x0f</span>), <span class="at">quote =</span> <span class="cn">FALSE</span>)</span></code></pre></div> |
| 462 |
<pre><code>[1] \001\002\003\004\005\006\a\b\t\n\v\f\r\016\017</code></pre> |
| 463 |
<p>Base R format control codes below 128 using octal escapes. There are |
| 464 |
some other differences between the function which we will highlight |
| 465 |
below.</p> |
| 466 |
</div> |
| 467 |
<div id="latin-1" class="section level3"> |
| 468 |
<h3>Latin-1</h3> |
| 469 |
<p>ASCII works fine for most text in English, but not for other |
| 470 |
languages. The Latin-1 encoding extends ASCII to Latin languages by |
| 471 |
assigning the numbers 128 to 255 (hexadecimal 0x80 to 0xff) to other |
| 472 |
common characters in Latin languages. We can see these characters |
| 473 |
below.</p> |
| 474 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>codes <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">128</span><span class="sc">:</span><span class="dv">255</span>, <span class="dv">8</span>, <span class="dv">16</span>, <span class="at">byrow =</span> <span class="cn">TRUE</span>,</span> |
| 475 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> <span class="at">dimnames =</span> <span class="fu">list</span>(<span class="fu">c</span>(<span class="dv">8</span><span class="sc">:</span><span class="dv">9</span>, letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>]), <span class="fu">c</span>(<span class="dv">0</span><span class="sc">:</span><span class="dv">9</span>, letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>])))</span> |
| 476 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a>latin1 <span class="ot"><-</span> <span class="fu">apply</span>(codes, <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>), intToUtf8)</span> |
| 477 |
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a></span> |
| 478 |
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="co"># replace control codes with ""</span></span> |
| 479 |
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a>latin1[<span class="fu">c</span>(<span class="st">"8"</span>, <span class="st">"9"</span>),] <span class="ot"><-</span> <span class="st">""</span></span> |
| 480 |
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a></span> |
| 481 |
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="fu">utf8_print</span>(latin1, <span class="at">quote =</span> <span class="cn">FALSE</span>)</span></code></pre></div> |
| 482 |
<pre><code> 0 1 2 3 4 5 6 7 8 9 a b c d e f |
| 483 |
8 |
| 484 |
9 |
| 485 |
a ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ® ¯ |
| 486 |
b ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ |
| 487 |
c À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï |
| 488 |
d Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß |
| 489 |
e à á â ã ä å æ ç è é ê ë ì í î ï |
| 490 |
f ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ</code></pre> |
| 491 |
<p>As with ASCII, the first 32 numbers are control codes. The others are |
| 492 |
characters common in Latin languages. Note that <code>0xa3</code>, the |
| 493 |
invalid byte from <em>Mansfield Park</em>, corresponds to a pound sign |
| 494 |
in the Latin-1 encoding. Given the context of the byte:</p> |
| 495 |
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a>lines[<span class="dv">15252</span>]</span></code></pre></div> |
| 496 |
<pre><code>[1] "the command of her beauty, and her \xa320,000, any one who could satisfy the"</code></pre> |
| 497 |
<p>this is probably the right symbol. The text is probably encoded in |
| 498 |
Latin-1, not UTF-8 or ASCII as claimed in the file.</p> |
| 499 |
<p>If you run into an error while reading text that claims to be ASCII, |
| 500 |
it is probably encoded as Latin-1. Note, however, that this is not the |
| 501 |
only possibility, and there are many other encodings. The |
| 502 |
<code>iconvlist</code> function will list the ones that R knows how to |
| 503 |
process:</p> |
| 504 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a><span class="fu">head</span>(<span class="fu">iconvlist</span>(), <span class="at">n =</span> <span class="dv">20</span>)</span></code></pre></div> |
| 505 |
<pre><code> [1] "437" "850" "852" "855" |
| 506 |
[5] "857" "860" "861" "862" |
| 507 |
[9] "863" "865" "866" "869" |
| 508 |
[13] "ANSI_X3.4-1968" "ANSI_X3.4-1986" "ARABIC" "ARMSCII-8" |
| 509 |
[17] "ASCII" "ASMO-708" "ATARI" "ATARIST" </code></pre> |
| 510 |
</div> |
| 511 |
<div id="utf-8-1" class="section level3"> |
| 512 |
<h3>UTF-8</h3> |
| 513 |
<p>With only 256 unique values, a single byte is not enough to encode |
| 514 |
every character. Multi-byte encodings allow for encoding more. UTF-8 |
| 515 |
encodes characters using between 1 and 4 bytes each and allows for up to |
| 516 |
1,112,064 character codes. Most of these codes are currently unassigned, |
| 517 |
but every year the Unicode consortium meets and adds new characters. You |
| 518 |
can find a list of all of the characters in the <a href="http://www.unicode.org/Public/10.0.0/ucd/UnicodeData.txt">Unicode |
| 519 |
Character Database</a>. A listing of the Emoji characters is <a href="http://www.unicode.org/Public/emoji/5.0/emoji-data.txt">available |
| 520 |
separately</a>.</p> |
| 521 |
<p>Say you want to input the Unicode character with hexadecimal code |
| 522 |
0x2603. You can do so in one of three ways:</p> |
| 523 |
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a><span class="st">"\u2603"</span> <span class="co"># with \u + 4 hex digits</span></span></code></pre></div> |
| 524 |
<pre><code>[1] "☃"</code></pre> |
| 525 |
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a><span class="st">"\U00002603"</span> <span class="co"># with \U + 8 hex digits</span></span></code></pre></div> |
| 526 |
<pre><code>[1] "☃"</code></pre> |
| 527 |
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a><span class="fu">intToUtf8</span>(<span class="dv">0x2603</span>) <span class="co"># from an integer</span></span></code></pre></div> |
| 528 |
<pre><code>[1] "☃"</code></pre> |
| 529 |
<p>For characters above <code>0xffff</code>, the first method won’t |
| 530 |
work. On Windows, a bug in the current version of R (fixed in R-devel) |
| 531 |
prevents using the second method.</p> |
| 532 |
<p>When you try to print Unicode in R, the system will first try to |
| 533 |
determine whether the code is printable or not. Non-printable codes |
| 534 |
include control codes and unassigned codes. On Mac OS, R uses an |
| 535 |
outdated function to make this determination, so it is unable to print |
| 536 |
most emoji. The <code>utf8_print</code> function uses the most recent |
| 537 |
version (10.0.0) of the Unicode standard, and will print all Unicode |
| 538 |
characters supported by your system:</p> |
| 539 |
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a><span class="fu">print</span>(<span class="fu">intToUtf8</span>(<span class="dv">0x1f600</span> <span class="sc">+</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">79</span>)) <span class="co"># base R</span></span></code></pre></div> |
| 540 |
<pre><code>[1] "\U0001f600\U0001f601\U0001f602\U0001f603\U0001f604\U0001f605\U0001f606\U0001f607\U0001f608\U0001f609\U0001f60a\U0001f60b\U0001f60c\U0001f60d\U0001f60e\U0001f60f\U0001f610\U0001f611\U0001f612\U0001f613\U0001f614\U0001f615\U0001f616\U0001f617\U0001f618\U0001f619\U0001f61a\U0001f61b\U0001f61c\U0001f61d\U0001f61e\U0001f61f\U0001f620\U0001f621\U0001f622\U0001f623\U0001f624\U0001f625\U0001f626\U0001f627\U0001f628\U0001f629\U0001f62a\U0001f62b\U0001f62c\U0001f62d\U0001f62e\U0001f62f\U0001f630\U0001f631\U0001f632\U0001f633\U0001f634\U0001f635\U0001f636\U0001f637\U0001f638\U0001f639\U0001f63a\U0001f63b\U0001f63c\U0001f63d\U0001f63e\U0001f63f\U0001f640\U0001f641\U0001f642\U0001f643\U0001f644\U0001f645\U0001f646\U0001f647\U0001f648\U0001f649\U0001f64a\U0001f64b\U0001f64c\U0001f64d\U0001f64e\U0001f64f"</code></pre> |
| 541 |
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a><span class="fu">utf8_print</span>(<span class="fu">intToUtf8</span>(<span class="dv">0x1f600</span> <span class="sc">+</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">79</span>)) <span class="co"># truncates to line width</span></span></code></pre></div> |
| 542 |
<pre><code>[1] "😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣…"</code></pre> |
| 543 |
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a><span class="fu">utf8_print</span>(<span class="fu">intToUtf8</span>(<span class="dv">0x1f600</span> <span class="sc">+</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">79</span>), <span class="at">chars =</span> <span class="dv">500</span>) <span class="co"># increase character limit</span></span></code></pre></div> |
| 544 |
<pre><code>[1] "😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿🙀🙁🙂🙃🙄🙅🙆🙇🙈🙉🙊🙋🙌🙍🙎🙏"</code></pre> |
| 545 |
<p>(Characters with codes above 0xffff, including most emoji, are not |
| 546 |
supported on Windows.)</p> |
| 547 |
<p>The <em>utf8</em> package provides the following utilities for |
| 548 |
validating, formatting, and printing UTF-8 characters:</p> |
| 549 |
<ul> |
| 550 |
<li><p><code>as_utf8()</code> attempts to convert character data to |
| 551 |
UTF-8, throwing an error if the data is invalid;</p></li> |
| 552 |
<li><p><code>utf8_valid()</code> tests whether character data is valid |
| 553 |
according to its declared encoding;</p></li> |
| 554 |
<li><p><code>utf8_normalize()</code> converts text to Unicode composed |
| 555 |
normal form (NFC), optionally applying case-folding and compatibility |
| 556 |
maps;</p></li> |
| 557 |
<li><p><code>utf8_encode()</code> encodes a character string, escaping |
| 558 |
all control characters, so that it can be safely printed to the |
| 559 |
screen;</p></li> |
| 560 |
<li><p><code>utf8_format()</code> formats a character vector by |
| 561 |
truncating to a specified character width limit or by left, right, or |
| 562 |
center justifying;</p></li> |
| 563 |
<li><p><code>utf8_print()</code> prints UTF-8 character data to the |
| 564 |
screen;</p></li> |
| 565 |
<li><p><code>utf8_width()</code> measures the display with of UTF-8 |
| 566 |
character strings (many emoji and East Asian characters are twice as |
| 567 |
wide as other characters).</p></li> |
| 568 |
</ul> |
| 569 |
<p>The package does not provide a method to translate from another |
| 570 |
encoding to UTF-8 as the <code>iconv()</code> function from base R |
| 571 |
already serves this purpose.</p> |
| 572 |
</div> |
| 573 |
</div> |
| 574 |
<div id="translating-to-utf-8" class="section level2"> |
| 575 |
<h2>Translating to UTF-8</h2> |
| 576 |
<p>Back to our original problem: getting the text of <em>Mansfield |
| 577 |
Park</em> into R. Our first attempt failed:</p> |
| 578 |
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" tabindex="-1"></a>corpus<span class="sc">::</span><span class="fu">term_stats</span>(lines)</span></code></pre></div> |
| 579 |
<pre><code>Error in corpus::term_stats(lines): argument entry 15252 is incorrectly marked as "UTF-8": invalid leading byte (0xA3) at position 36</code></pre> |
| 580 |
<p>We discovered a problem on line 15252:</p> |
| 581 |
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" tabindex="-1"></a>lines[<span class="dv">15252</span>]</span></code></pre></div> |
| 582 |
<pre><code>[1] "the command of her beauty, and her \xa320,000, any one who could satisfy the"</code></pre> |
| 583 |
<p>The text is likely encoded in Latin-1, not UTF-8 (or ASCII) as we had |
| 584 |
originally thought. We can test this by attempting to convert from |
| 585 |
Latin-1 to UTF-8 with the <code>iconv()</code> function and inspecting |
| 586 |
the output:</p> |
| 587 |
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" tabindex="-1"></a>lines2 <span class="ot"><-</span> <span class="fu">iconv</span>(lines, <span class="st">"latin1"</span>, <span class="st">"UTF-8"</span>)</span> |
| 588 |
<span id="cb38-2"><a href="#cb38-2" tabindex="-1"></a>lines2[<span class="dv">15252</span>]</span></code></pre></div> |
| 589 |
<pre><code>[1] "the command of her beauty, and her £20,000, any one who could satisfy the"</code></pre> |
| 590 |
<p>It worked! Now we can analyze our text.</p> |
| 591 |
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" tabindex="-1"></a>f <span class="ot"><-</span> corpus<span class="sc">::</span><span class="fu">text_filter</span>(<span class="at">drop_punct =</span> <span class="cn">TRUE</span>, <span class="at">drop =</span> corpus<span class="sc">::</span>stopwords_en)</span> |
| 592 |
<span id="cb40-2"><a href="#cb40-2" tabindex="-1"></a>corpus<span class="sc">::</span><span class="fu">term_stats</span>(lines2, f)</span></code></pre></div> |
| 593 |
<pre><code> term count support |
| 594 |
1 fanny 816 806 |
| 595 |
2 must 508 492 |
| 596 |
3 crawford 493 488 |
| 597 |
4 mr 482 466 |
| 598 |
5 much 459 450 |
| 599 |
6 miss 432 419 |
| 600 |
7 said 406 400 |
| 601 |
8 mrs 408 399 |
| 602 |
9 sir 372 366 |
| 603 |
10 edmund 364 364 |
| 604 |
11 one 370 358 |
| 605 |
12 think 349 346 |
| 606 |
13 now 333 331 |
| 607 |
14 might 324 320 |
| 608 |
15 time 310 307 |
| 609 |
16 little 309 300 |
| 610 |
17 nothing 301 291 |
| 611 |
18 well 299 286 |
| 612 |
19 thomas 288 285 |
| 613 |
20 good 280 275 |
| 614 |
⋮ (8450 rows total)</code></pre> |
| 615 |
</div> |
| 616 |
<div id="the-readtext-package" class="section level2"> |
| 617 |
<h2>The <em>readtext</em> package</h2> |
| 618 |
<p>If you need more than reading in a single text file, the <a href="https://github.com/quanteda/readtext">readtext</a> package |
| 619 |
supports reading in text in a variety of file formats and encodings. |
| 620 |
Beyond just plain text, that package can read in PDFs, Word documents, |
| 621 |
RTF, and many other formats. (Unfortunately, that package currently |
| 622 |
fails when trying to read in <em>Mansfield Park</em>; the authors are |
| 623 |
aware of the issue and are working on a fix.)</p> |
| 624 |
</div> |
| 625 |
<div id="summary" class="section level2"> |
| 626 |
<h2>Summary</h2> |
| 627 |
<p>Text comes in a variety of encodings, and you cannot analyze a text |
| 628 |
without first knowing its encoding. Many functions for reading in text |
| 629 |
assume that it is encoded in UTF-8, but this assumption sometimes fails |
| 630 |
to hold. If you get an error message reporting that your UTF-8 text is |
| 631 |
invalid, use <code>utf8_valid</code> to find the offending texts. Try |
| 632 |
printing the data to the console before and after using |
| 633 |
<code>iconv</code> to convert between character encodings. You can use |
| 634 |
<code>utf8_print</code> to print UTF-8 characters that R refuses to |
| 635 |
display, including emoji characters. For reading in exotic file formats |
| 636 |
like PDF or Word, try the <a href="https://github.com/quanteda/readtext">readtext</a> package.</p> |
| 637 |
</div> |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
<script> |
| 646 |
(function () { |
| 647 |
var script = document.createElement("script"); |
| 648 |
script.type = "text/javascript"; |
| 649 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 650 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 651 |
})(); |
| 652 |
</script> |
| 653 |
|
| 654 |
</body> |
| 655 |
</html> |