Text Preprocessing

  • Problem of text data
    How to encode them into numerical values (feature extraction)

Tokenization

  • Convert a sentence into a sequence of tokens (i.e., words and characters)
    • Example:
      • My name is Yu. My first name is Li.
      • [“My”, “name”, “is”, “Yu”, “My”, “first”, “name”, “is”, “Li”]

Stop words removal

  • Many words are not informative and have a high frequency. Thus, irrelevant for document representation

    • Example: the, and, a, an, is, of, that, may, off, be, by, for, from, it, will, was, with, were
  • Benefits:

    1. Reduce data file size: stop words accounts 20-30% of total word counts
    2. Improve efficiency: they are not useful

Stemming

  • Reducing words to their root form, a document may contain several occurrences of words

    • Example: fish, fishes, fisher, fishing and fishers (share the same word stem and should be represented with its stem fish)
  • Benefits:

    1. Improving effectiveness of matching similar words
    2. Reducing data size: combing words with same roots may reduce data size as much as 40-50%

Normalization

  • Convert the words to equivalence classing of terms
    • Example: {USA, U.S.A.}, {dataset, data set}, {anti-discriminatory, antdiscriminatory}
  • Put the Synonym words within a list
    • Example: {car, automobile}, {cat, kitty}
  • Name entity recognition
    • Example: names of persons, organizations, locations

Build the dictionary

After the above preprocessing,

  • Count the unique tokens from all the documents
  • Index each unique token according to its frequency
  • Remove low-frequency tokens, make them <unk>
  • Reserved tokens

    • Unknown token: <unk>
    • Padding: <pad>
    • Beginning for a sequence: <bos>
    • End of a sequences: <eos>
  • Example: [('<unk>', 0), ('the', 1), ('i', 2), ('and', 3), ('of', 4), ('a', 5), ('to', 6), ('was', 7), ('in', 8), ('that', 9)]

After the standard text preprocessing, the data is clean, smaller and can be linked to some numerical values. Text preprocessing can reduce the data size, improve downstream model performande and reduce the programming running time but won’t lead to overfitting

Text Encoding

Big-of-words

Convert a sentence or a document to a “term” vector

  • For each term (a token in the dictionary) is considered as a component (attribute) of the vector
  • The value of each component is the number of times (occurrence) the corresponding term occurs in the document
  • However, this approch will lose position and order information

A document-term matrix

  • Euclidean distance
    Euclidean distance matrix
    Term frequency problem: different documents can have different lengths

  • Normalize the distance
    Normalized document-term matrix
    Left: before normalization, Right: after normalization
    Importancy problem: some tokens may important (keywords) but some are not.
    For some specific tokens (e.g., image segmentation) are important keywords to specify the document but some tokens (e.g., learning) may not be important to specify

TF-IDF

Term Frequency–Inverse Document Frequency (TF-IDF):

  • Since not all terms are of equal importance, if a term occurs frequently in many documents, it has less discriminatory power
  • Inverse document frequency: if a token appears accross all documents, the term for this token is very small which implies the token is not important
  • $f_{t,d}$: number of times that token $t$ appears in document $d$
  • $N$: total number of documents in the corpus $N = |D|$
  • $|\{d \in D:t \in d\}|$: number of documents that token $t$ appears

Left: TF document-term matrix, Right TF-IDF document-term matrix
Left: TF 1-cosine, Right: TF-IDF 1-cosine

Shortages of bag-of-words: It loses the spatial information and combination information of tokens. Besides, the TF-IDF approach is universal and does not consider the tokens’ representation in the specific data, which means this approach has not learned the data

  • Bag-of-words encoding can convert the document into a numerical vector but omit the local patterns of the text data
  • TF-IDF is a better bag-of-words encoding as it considers the importance of a word across all the documents.