Text Pre-Processing for Chinese#

Note

This section, “Working in Languages Beyond English,” is co-authored with Quinn Dombrowski, the Academic Technology Specialist at Stanford University and a leading voice in multilingual digital humanities. I’m grateful to Quinn for helping expand this textbook to serve languages beyond English.

This lesson is for anyone who wants to try the TF-IDF or topic modeling lessons on Chinese texts. Before continuing with those lessons, you need to create a segmented derivative of your original Chinese text, which inserts spaces between the words. If words are not separated by spaces, word count methods won’t work.

Install spaCy#

!pip install -U spacy

Download Language Model#

!python -m spacy download zh_core_web_md

Import Libraries#

import spacy

Load Language Model#

Once the model is downloaded, we need to load it. There are two ways to load a spaCy language model.

  1. We can import the model as a module and then load it from the module.

import zh_core_web_md
nlp = zh_core_web_md.load()
  1. We can load the model by name.

#nlp = spacy.load('zh_core_web_md')

If you just downloaded the model for the first time, it’s advisable to use Option 1. Then you can use the model immediately. Otherwise, you’ll likely need to restart your Jupyter kernel (which you can do by clicking Kernel -> Restart Kernel… in the Jupyter Lab menu).

Process Document#

To create a derivative text file that we can use with TF-IDF, topic modeling, or other word-count based methods, we need to use spaCy to segment the text, artificially inserting spaces between words because most text analysis methods assume that words are separated by spaces.

The example text for Chinese is 敬告中国二万万女同胞 by 秋瑾. (Thanks to Paul Vierthaler for selecting and finding the text.)

Here we open the text and process it with the Chinese spaCy model.

filepath = '../texts/zh.txt'
# Open and read text
text = open(filepath, encoding='utf-8').read()
# Process text with spaCy
document = nlp(text)

Then we loop through each token in the original text, lemmatize each token and insert a space between the tokens, and finally write them to our new segmented derivative text file.

outname = filepath.replace('.txt', '-segmented.txt')

# Create a segmented version of the original text file
with open(outname, 'w', encoding='utf8') as out:
    
    for token in document:
        # Get the lemma for each token
        out.write(token.lemma_.lower())
        # Insert white space between each token
        out.write(' ')

View output#

The code cell below prints the text as a list of individual tokens (words and punctuation), so you can see how successfully it identified word boundaries.

for token in document:
    print(token.lemma_)
秋瑾
《
敬告
中国
二万万
女
同胞
》

 

唉!
世界
上
最
不平
的
事
,
就是
我们
二万万
女
同胞
了
。
从
小生
下来
,
遇
着
好
老子
,
还
说
得
过
;遇
着
脾气
杂冒
、
不
讲
情理
的
,
满嘴连
说
:
“
晦气
,
又
是
一个
没用
的
。
”
恨不得
拿
起来
摔死
。
总
抱
着
“
将来
是
别人家
的
人
”
这
句
话
,
冷一眼
、
白一眼
地
看待
;
没
到
几
岁
,
也
不
问
好歹
,
就
把
一
双
雪白
粉嫩
的
天足脚
,
用
白布
缠
着
,
连
睡觉
的
时候
,
也
不许
放松
一点
,
到
了
后来
肉
也
烂尽
了
,
骨
也
折断
了
,
不过
讨亲戚
、
朋友
、
邻居们
一
声
“
某
人家
姑娘
脚
小
”
罢了
。
这
还
不说
,
到
了
择亲
的
时光
,
只
凭着
两
个
不要
脸媒人
的话
,
只要
男家
有钱有势
,
不
问
身家
清白
,
男人
的
性情
好坏
、
学问
高低
,
就
不知不觉
应
了
。
到
了
过门
的
时候
,
用
一
顶
红红绿绿
的
花轿
,
坐
在
里面
,
连气
也
不
能
出
。
到
了
那边
,
要是
遇
着
男人
虽
不
怎么样
,
却
还
安分
,
这就
算
前生
有
福今
生受
了
。
遇
着
不
好的
,
总
不是
说
“
前生
作
了
孽
”
,
就是说
“
运气
不
好
”
。
要是
说
一二
句
抱怨
的话
,
或是
劝
了
男人
几
句
,
反
了
腔
,
就
打骂
俱下
;
别人
听见
还要
说
:
“
不
贤惠
,
不
晓得
妇道
呢
!
”
诸位
听听
,
这不
是
有
冤
没
处诉
么
?
还有
一
桩
不公
的
事
:
男子
死
了
,
女子
就要
带
三年
孝
,
不许
二嫁
。
女子
死
了
,
男人
只
带
几
根
蓝辫线
,
有
嫌难
看
的
,
连带
也
不
带
;
人
死
还
没
三
天
,
就
出去
偷鸡摸狗
;七
还
未
尽
,
新
娘子
早已
进门
了
。
上
天生人
,
男女
原
没有
分别
。
试问
天下
没有
女人
,
就
生出
这些
人
来
么
?
为什么
这样
不
公道
呢
?
那些
男子
,
天天
说
“
心
是
公的
,
待人
是
要
和平
的
”
,
又
为什么
把
女子
当作
非洲
的
黑奴
一样
看待
。
不公不平
,
直到
这
步
田地
呢
?

  
诸位
,
你
要
知道
天下
事靠人
是
不行
的
,
总
要求
己
为
是
。
当初
那些
腐儒
说
什么
“
男尊女卑
”
、
“
女子无才便是德
”
、
“
夫为
妻纲
”
这些
胡说
,
我们
女子
要是
有
志气
的
,
就
应当
号召
同志
与
他
反对
,
陈后
主兴
了
这
缠足
的
例子
,
我们
要是
有
羞耻
的
,
就
应当
兴师问罪
;即
不然
,
难道
他
捆
着
我
的
腿
?
我
不
会
不
缠
的
么
?
男子
怕
我们
有
知识
、
有
学问
、
爬上
他们
的
头
,
不准
我们
求学
,
我们
难道
不
会
和
他
分辨
,
就
应
了
么
?
这
总是
我们
女子
自己
放弃
责任
,
样样
事体
一
见
男子
做
了
,
自己
就
乐得
偷懒
,
图安乐
。
男子
说
我
没用
,
我
就
没
用;
说
我
不行
,
只要
保着眼
前
舒服
,
就
作
奴隶
也
不
问
了
。
自己
又
看看
无功受禄
,
恐怕
行
不
长久
,
一
听见
男子
喜欢
脚
小
,
就
急急忙忙
把
它
缠
了
,
使
男人
看见
喜欢
,
庶
可以
藉此
吃
白饭
。
至于
不
叫
我们
读书
、
习字
,
这
更
是
求之不得
的
,
有
甚么
不
赞成
呢
?
诸位
想想
,
天下
有
享现
成福
的
么
?
自然
是
有
学问
、
有
见识
、
出力
作事
的
男人
得
了
权利
,
我们
作
他
的
奴隶
了
。
既
作
了
他
的
奴隶
,
怎么
不
受
压制
呢
?
自作自受
,
又
怎么
怨
得
人
呢
?
这些
事情
,
提
起来
,
我
也
觉得
难过
,
诸位
想想
总是
个中
人
,
亦
不必
用
我
细说
。

  
但是
从此以后
,
我
还望
我们
姐妹们
,
把
从前
事情
,
一概
搁开
,
把
以后
事情
,
尽力
作去
,
譬如
从
前死
了
,
现在
又
转世
为人
了
。
老
的
呢
,
不要
说
“
老
而
无用
”
,
遇见
丈夫
好的
要
开学堂
,
不要
阻
他
;
儿子
好的
,
要
出洋
留学
,
不要
阻
他
。
中年
作
媳妇
的
,
总
不要
拖
着
丈夫
的
腿
,
使
他
气短
志颓
,
功
不成
、
名
不
就
;生
了
儿子
,
就要
送
他
进学堂
,
女儿
也
是
如此
,
千万
不要
替
他
缠足
。
幼年
姑娘
的
呢
,
若
能够
进学堂
更
好;
就
不进学堂
,
在
家里
也
要
常
看
书
、
习字
。
有钱
作官
的
呢
,
就要
劝
丈夫
开学堂
、
兴工厂
,
作
那些
与
百姓
有益
的
事情
。
无钱
的
呢
,
就要
帮
着
丈夫
苦作
,
不要
偷懒
吃闲饭
。
这就
是
我
的
望头
了
。
诸位
晓得
国是
要亡
的
了
,
男人
自己
也
不
保
,
我们
还
想
靠
他
么
?
我们
自己
要不
振作
,
到
国亡
的
时候
,
那
就
迟
了
。
诸位
!诸位
!须
不
可以
打断
我
的
念头
才
好
呢
!