今天做一个文本相似度的小任务,利用python的“Levenshtein”包可对比两个文本的相似度。为了消除标点符号的影响,需要去除标点,python的string模块下的punctuation包含所有的英文标点符号。所以用replace()一下就可以去除:
 
Example 1:
 
import string
s 
= 'today is friday, so happy..!!!'
for c 
in string
.punctuation
:
    s 
= s
.replace
(c
,'')
print(s
)
 
Result:
 
today 
is friday so happy
 
        string.punctuation中的标点符号只有英文,如果是中文文本,可以调用zhon包的zhon.hanzi.punctuation函数即可得到中文的标点符号集合。
 
Example 2:
 
from zhon
.hanzi 
import punctuation
a 
= '今天周五,下班了,好开心呀!!'
for i 
in punctuation
:
    a 
= a
.replace
(i
,'')
print(a
)
 
Result:
 
今天周五下班了好开心呀
 
<( ̄︶ ̄)↗[GO!]