Python語料預處理操作,並保存中間結果

doc1.txt

原始語料:

在這裏插入圖片描述

doc2.txt

預處理後:

在這裏插入圖片描述

doc3.txt

結果爲:

在這裏插入圖片描述


代碼爲:

import re
def yuchuli():

    filename = './中間結果/doc1.txt'
    f = open(filename, 'r', encoding='utf-8')
    context = f.read()

    pattern = ",|\\.|\\?|!|:|;|~|,|:|。|!|;|?| "
    sentence = [i.replace('\n', '##').strip() for i in re.split(pattern, context)]

    g = open('./中間結果/doc2.txt', 'w', encoding='utf-8')

    for word in sentence:
        a = str(word)
        a = a.replace('##', '\r\n')
        print(a, file=g)

    f.close()
    g.close()

    k = open('./中間結果/doc2.txt', 'r', encoding='utf-8')
    out = open('./中間結果/doc3.txt', 'w', encoding='utf-8')

    for eachline in k.readlines():
        if len(eachline) > 5:
            out.writelines(eachline)
    k.close()
    out.close()
    

yuchuli()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章