1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/accessibility/annotations/ax_image_stopwords.h"
#include <string>
#include <string_view>
#include <vector>
#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/i18n/case_conversion.h"
#include "base/i18n/char_iterator.h"
#include "base/no_destructor.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/icu/source/common/unicode/uchar.h"
namespace content {
namespace {
// List of image stopwords for all languages. See ax_image_stopwords.h
// for information about how image stopwords are defined and how they're
// used.
//
// The stopwords are encoded here as a single long string delimited by
// newlines. This is much more efficient than an array of strings, which
// in practice takes ~6x more storage in the resulting binary.
//
// Current size as of June 2020:
// 369 unique words
// 2542 bytes uncompressed
// 1127 bytes gzipped
const char kImageStopwordsUtf8[] = {
//
// Language-independent stopwords.
//
"com\n"
"edu\n"
"http\n"
"https\n"
"www\n"
//
// English.
//
// General English stopwords.
"and\n"
"are\n"
"for\n"
"from\n"
"how\n"
"online\n"
"our\n"
"the\n"
"this\n"
"with\n"
"you\n"
"your\n"
// English image-specific stopwords.
"art\n"
"avatar\n"
"background\n"
"backgrounds\n"
"black\n"
"download\n"
"drawing\n"
"drawings\n"
"free\n"
"gif\n"
"icon\n"
"icons\n"
"illustration\n"
"illustrations\n"
"image\n"
"images\n"
"jpeg\n"
"jpg\n"
"logo\n"
"logos\n"
"meme\n"
"memes\n"
"photo\n"
"photos\n"
"picture\n"
"pictures\n"
"png\n"
"stock\n"
"transparent\n"
"vector\n"
"vectors\n"
"video\n"
"videos\n"
"wallpaper\n"
"wallpapers\n"
"white\n"
// Alt text from may images starts with "Image may contain".
"may\n"
"contain\n"
// Many images on the web have the alt text "Highlights info row."
"highlights\n"
"info\n"
"row\n"
// Google Photos images are labeled as "portrait" or "landscape".
"portrait\n"
"landscape\n"
// Reddit says "post image".
"post\n"
// Months and month abbreviations. Often used as part of the date/time
// when a photograph was taken.
"january\n"
"jan\n"
"february\n"
"feb\n"
"march\n"
"mar\n"
"april\n"
"apr\n"
"may\n"
"june\n"
"jun\n"
"july\n"
"jul\n"
"august\n"
"aug\n"
"september\n"
"sep\n"
"october\n"
"oct"
"november"
"nov\n"
"december\n"
"dec\n"
// Days of the week.
"monday\n"
"mon\n"
"tuesday\n"
"tue\n"
"wednesday\n"
"wed\n"
"thursday\n"
"thu\n"
"friday\n"
"fri\n"
"saturday\n"
"sat\n"
"sunday\n"
"sun\n"
//
// French
//
// General French stopwords.
"les\n" // the
"pour\n" // for
"des\n" // of the
"sur\n" // on
"avec\n" // with
"une\n" // one
"dans\n" // in
"est\n" // is
"plus\n" // more
"par\n" // by
"vous\n" // you
"pas\n" // not
"qui\n" // who
"aux\n" // to the
"son\n" // his/her/its
"nous\n" // we
"voir\n" // see
// French Image stopwords.
"noir\n" // black
"blanc\n" // white
"dessin\n" // drawing
"font\n" // background
"peinture\n" // painting
// Months.
"janvier\n"
"janv\n"
"février\n"
"févr\n"
"mars\n"
"avril\n"
"mai\n"
"juin\n"
"juillet\n"
"juil\n"
"août\n"
"septembre\n"
"sept\n"
"octobre\n"
"oct\n"
"novembre\n"
"nov\n"
"décembre\n"
"déc\n"
// Days of the week.
"lundi\n"
"lun\n"
"mardi\n"
"mar\n"
"mercredi\n"
"mer\n"
"jeudi\n"
"jeu\n"
"vendredi\n"
"ven\n"
"samedi\n"
"sam\n"
"dimanche\n"
"dim\n"
//
// Italian
//
"con\n" // with
"per\n" // through, by
"non\n" // not
"come\n" // as
"più\n" // more
"dal\n" // da + il
"dallo\n" // da + lo
"dai\n" // da + i
"dagli\n" // da + gli
"dall\n" // da + l'
"dagl\n" // da + gll'
"dalla\n" // da + la
"dalle\n" // da + le
"del\n" // di + il
"dello\n" // di + lo
"dei\n" // di + i
"degli\n" // di + gli
"dell\n" // di + l'
"degl\n" // di + gl'
"della\n" // di + la
"delle\n" // di + le
"nel\n" // in + el
"nello\n" // in + lo
"nei\n" // in + i
"negli\n" // in + gli
"nell\n" // in + l'
"negl\n" // in + gl'
"nella\n" // in + la
"nelle\n" // in + le
"sul\n" // su + il
"sullo\n" // su + lo
"sui\n" // su + i
"sugli\n" // su + gli
"sull\n" // su + l'
"sugl\n" // su + gl'
"sulla\n" // su + la
"sulle\n" // su + le
// Images
"arte\n"
"immagini\n"
"illustrazione\n"
"fotografia\n"
"icona\n"
"bianca\n" // white
"bianco\n" // white
"nera\n" // black
"nero\n" // black
"contenere\n" // contain (image may contain...)
// Months.
"gennaio\n"
"genn\n"
"febbraio\n"
"febbr\n"
"marzo\n"
"mar\n"
"aprile\n"
"apr\n"
"maggio\n"
"magg\n"
"giugno\n"
"luglio\n"
"agosto\n"
"settembre\n"
"sett\n"
"ottobre\n"
"ott\n"
"novembre\n"
"nov\n"
"dicembre\n"
"dic\n"
// Weekdays.
"lunedì\n"
"lun\n"
"martedì\n"
"mar\n"
"mercoledì\n"
"mer\n"
"giovedì\n"
"gio\n"
"venerdì\n"
"ven\n"
"sabato\n"
"sab\n"
"domenica\n"
"dom\n"
//
// German
//
// General German stopwords.
"und\n" // and
"mit\n" // with
"für\n" // for
"der\n" // the
"die\n" // the
"von\n" // of, from
"auf\n" // on
"das\n" // the
"aus\n" // out of
"ist\n" // is
"ein\n" // one
"eine\n" // one
"sie\n" // they, she
"den\n" // the
"zum\n" // zu + dem
"zur\n" // zu + der
"bei\n" // by
"des\n" // the
"sprüche\n" // claims (to be)
"oder\n" // or
// German Image stopwords.
"bild\n" // picture
"bilder\n" // pictures
"foto\n" // photo
"schwarz\n" // black
"weiß\n" // white
// Months.
"januar\n"
"jan\n"
"jän\n"
"februar\n"
"feb\n"
"märz\n"
"april\n"
"apr\n"
"mai\n"
"juni\n"
"juli\n"
"august\n"
"aug\n"
"september\n"
"sept\n"
"oktober\n"
"okt\n"
"november\n"
"nov\n"
"dezember\n"
"dez\n"
// Weekdays.
"montag\n"
"dienstag\n"
"mittwoch\n"
"donnerstag\n"
"freitag\n"
"samstag\n"
"sonntag\n"
//
// Spanish
//
// General Spanish stopwords.
"con\n" // with
"para\n" // by
"del\n" // of the
"que\n" // that
"los\n" // the
"las\n" // the
"una\n" // one
"por\n" // for
"más\n" // more
"como\n" // how
// Spanish image stopwords.
"dibujos\n" // drawings
"imagen\n" // images
"arte\n" // art
"fondo\n" // background
"fondos\n" // backgrounds
"diseño\n" // design
"ilustración\n" // illustration
"imagenes\n" // images
"blanca\n" // white
"blanco\n" // white
"negra\n" // black
"negro\n" // black
// Months.
"enero\n"
"febrero\n"
"feb\n"
"marzo\n"
"abril\n"
"abr\n"
"mayo\n"
"junio\n"
"jun\n"
"julio\n"
"jul\n"
"agosto\n"
"septiembre\n"
"sept\n"
"set\n"
"octubre\n"
"oct\n"
"noviembre\n"
"nov\n"
"diciembre\n"
"dic\n"
// Weekdays. Weekday abbreviations in Spanish are two-letters which
// don't need to be listed here (anything less than 3 letters is
// considered a stopword already).
"lunes\n"
"martes\n"
"miércoles\n"
"jueves\n"
"viernes\n"
"sábado\n"
"domingo\n"
//
// Hindi
//
// General Hindi stopwords.
"में\n" // in
"लिए\n" // for
"नहीं\n" // no
"एक\n" // one
"साथ\n" // with
"दिया\n" // gave
"किया\n" // did
"रहे\n" // are
"सकता\n" // can
"इस\n" // this
"शामिल\n" // include
"तारीख\n" // the date
// Hindi image stopwords.
"चित्र\n" // picture
"वीडियो\n" // video
// Months
"जनवरी\n"
"फरवरी\n"
"मार्च\n"
"अप्रैल\n"
"मई\n"
"जून\n"
"जुलाई\n"
"अगस्त\n"
"सितंबर\n"
"अक्टूबर\n"
"नवंबर\n"
"दिसंबर\n"
// Weekdays.
"सोमवार\n"
"मंगलवार\n"
"बुधवार\n"
"बृहस्पतिवार\n"
"शुक्रवार\n"
"शनिवार\n"
"रविवार\n"};
} // namespace
// static
AXImageStopwords& AXImageStopwords::GetInstance() {
static base::NoDestructor<AXImageStopwords> instance;
return *instance;
}
AXImageStopwords::AXImageStopwords() {
// Parse the newline-delimited stopwords from kImageStopwordsUtf8 and store
// them as a flat_set of type string_view. This is very memory-efficient
// because it avoids ever needing to copy any of the strings; each
// string_view is just a pointer into kImageStopwordsUtf8 and flat_set
// acts like a set but basically just does a binary search.
std::vector<std::string_view> stopwords =
base::SplitStringPiece(kImageStopwordsUtf8, "\n", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
// It's inefficient to add things to a flat_set one at a time. Copy them
// all over at once.
stopword_set_ = stopwords;
}
AXImageStopwords::~AXImageStopwords() = default;
bool AXImageStopwords::IsImageStopword(const char* word_utf8) const {
std::u16string word_utf16 = base::UTF8ToUTF16(word_utf8);
// It's not really meaningful, but since short words are stopwords, for
// simplicity we define the empty string to be a stopword too.
if (word_utf16.empty())
return true;
// Canonicalize case, this is like "ToLower" for many languages but
// works independently of the current locale.
word_utf16 = base::i18n::FoldCase(word_utf16);
// Count the number of distinct codepoints from a supported unicode block.
int supported_count = 0;
base::i18n::UTF16CharIterator iter(word_utf16);
for (; !iter.end(); iter.Advance()) {
int32_t codepoint = iter.get();
UBlockCode block_code = ublock_getCode(codepoint);
switch (block_code) {
case UBLOCK_BASIC_LATIN:
case UBLOCK_LATIN_1_SUPPLEMENT:
case UBLOCK_LATIN_EXTENDED_A:
case UBLOCK_DEVANAGARI:
supported_count++;
break;
default:
break;
}
}
// Treat any string of 2 or fewer characters in any of these unicode
// blocks as a stopword. Of course, there are rare exceptions, but they're
// acceptable for this heuristic. All that means is that we won't count
// those words when trying to determine if the alt text for an image
// has meaningful text or not. The odds are good that a 1 or 2 character
// word is not meaningful.
//
// Note: this assumption might not be valid for some unicode blocks,
// like Chinese. That's why the heuristic only applies to certain unicode
// blocks where we believe this to be a reasonable assumption.
//
// Note that in Devanagari (the script used for the Hindi language, among
// others) a word sometimes looks like a single character (one glyph) but it's
// actually two or more unicode codepoints (consonants and vowels that are
// joined together), which is why this heuristic still works. Anything with
// two or fewer unicode codepoints is an extremely short word.
if (supported_count == iter.char_offset() && iter.char_offset() <= 2)
return true;
return base::Contains(stopword_set_, base::UTF16ToUTF8(word_utf16));
}
} // namespace content
|