今天做一個文本相似度的小任務,利用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!]