Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Text preprocessing

Text preprocessing

Text preprocessing is a fundamental and critical step in Natural Language Processing (NLP); it transforms raw unstructured text data into a format suitable for machine learning models.

This article systematically introduces the three core stages of text preprocessing: text cleaning, tokenization, and part-of-speech tagging.


Text Cleaning: Purifying Raw Text Data

Text cleaning is the first step of preprocessing, aimed at removing noisy data from text and improving the accuracy of subsequent processing.

Encoding Format Handling

Text from different sources may use different encoding formats (such as UTF-8, GBK, ASCII, etc.); unifying the encoding is the first task:

Example

Encoding Conversion Example

text

=

"sample text"

.

encode

(

'gbk'

)

 

Assume the original encoding is GBK

text

=

text.

decode

(

'gbk'

)

.

encode

(

'utf-8'

)

 

Convert to UTF-8

Solutions to Common Encoding Problems:

Special Character Handling

Different scenarios require handling different types of special characters:

Character type Handling Methods Application scenarios
HTML tags Remove with regular expressions Web-crawled text
Emojis Remove or convert to text descriptions Social media analysis
Control characters Filter out All text processing
Special punctuation Standardization Text Normalization

Example

import

re

Example of Removing HTML Tags

text

=

"

This is aHTMLText

"

clean_text

=

re

.

sub

(

r

'<[^>]+>'

,

''

,

text

)

print

(

clean_text

)

 

Output: This is a piece of HTML text

Noise Data Removal

Depending on specific task requirements, you may need to:

  1. Remove irrelevant information (ads, copyright notices, etc.)
  2. Handle spelling errors (using spell-check libraries)
  3. Standardize numeric representation (e.g., unify "1000" as "1,000")
  4. Unify date formats ("2023-01-01" vs "01/01/2023")

Tokenization: Breaking Text into Basic Units

Tokenization is the process of splitting continuous text into meaningful language units (tokens); different languages require different tokenization methods.

English Tokenization Methods

English tokenization is relatively simple, mainly based on spaces and punctuation splitting:

Example

English tokenization using NLTK

from

nltk.

tokenize

import

word_tokenize

text

=

"Natural Language Processing is fascinating!"

tokens

=

word_tokenize

(

text

)

print

(

tokens

)

 

['Natural', 'Language', 'Processing', 'is', 'fascinating', '!']

Notes on English Tokenization:

Chinese Tokenization Techniques

Chinese has no obvious word boundaries, making tokenization more complex. Main methods include:

  1. Dictionary-based Tokenization: Maximum matching method, shortest path method
  2. Statistical-based Tokenization: Sequence labeling methods such as HMM and CRF
  3. Deep learning-based Tokenization: Models such as BiLSTM-CRF and BERT

Example

Chinese tokenization using jieba

import

jieba

text

=

"Natural language processing is very interesting"

tokens

=

jieba.

lcut

(

text

)

print

(

tokens

)

 

['natural language', 'processing', 'very', 'interesting']

Subword Tokenization (Subword Tokenization)

Solves rare word and vocabulary expansion problems; common methods:

Example

Example using HuggingFace's tokenizer

from

transformers

import

BertTokenizer

tokenizer

=

BertTokenizer.

from_pretrained

(

'bert-base-chinese'

)

tokens

=

tokenizer.

tokenize

(

"Natural language processing"

)

print

(

tokens

)

 

['Zi' (a Chinese character), 'Ran' (a Chinese character), 'Yu' (a Chinese character), 'Yan' (a Chinese character), 'Chu' (a Chinese character), 'Li' (a Chinese character)]

Comparison of Common Tokenization Tools

Tool name Supported Languages Feature Applicable scenarios
NLTK Mainly English Comprehensive features, average speed Teaching, research
spaCy Multilingual Industrial-grade, fast Production environment
jieba Chinese Simple to use, extensible dictionary Chinese processing
Stanford CoreNLP Multilingual High accuracy, high resource consumption Academic research
HuggingFace Tokenizers Multilingual Supports subword tokenization Deep learning

Part-of-Speech Tagging: Understanding the Grammatical Roles of Words

Part-of-Speech Tagging (Part-of-Speech Tagging) is the process of labeling each word in the tokenization results with its part-of-speech category.

The Concept of Part-of-Speech Tagging

Part-of-speech tagging helps to:

Common Part-of-Speech Tag Sets

Different languages and tools use different part-of-speech tagging systems:

Common English Penn Treebank tag set (partial):

Common Chinese ICTCLAS tag set (partial):

Automatic Part-of-Speech Tagging Methods

  1. Rule-based methods: Tagging using hand-written rules
  2. Statistical methods: Models such as HMM and MaxEnt
  3. Deep learning based methods: Neural networks such as RNN and Transformer

Example

Part-of-speech tagging using spaCy

import

spacy

nlp

=

spacy.

load

(

"en_core_web_sm"

)

doc

=

nlp

(

"Natural Language Processing is fascinating!"

)

for

token

in

doc:

   

print

(

token

.

text

,

token

.

pos_

)

 

Output each word and its part-of-speech tag

Evaluation Metrics for Part-of-Speech Tagging:


Practical Suggestions

  1. Preprocessing Pipeline Order: Encoding handling → text cleaning → tokenization → part-of-speech tagging
  2. Principles for Tool Selection: Choose appropriate tools based on language, task requirements, and performance needs
  3. Custom Processing: Specific domains may require custom dictionaries or rules
  4. Performance optimization: For large-scale text, consider parallel processing or efficient tools