【发布时间】:2022-01-23 12:35:58
【问题描述】:
我想从我的文本文件中删除标点符号,这是一个英语-波斯语句子对数据。
我已经尝试了以下代码:
import string
import re
from numpy import array, argmax, random, take
import pandas as pd
# function to read raw text file
def read_text(filename):
# open the file
file = open(filename, mode='rt', encoding='utf-8')
# read all text
text = file.read()
file.close()
return text
# split a text into sentences
def to_lines(text):
sents = text.strip().split('\n')
sents = [i.split('\t') for i in sents]
return sents
data = read_text("pes.txt")
pes_eng = to_lines(data)
pes_eng = array(pes_eng)
# Remove punctuation
pes_eng[:,0] = [s.translate(str.maketrans('', '', string.punctuation)) for s
in pes_eng[:,0]]
pes_eng[:,1] = [s.replace("؟!.،,?" ,"") for s in pes_eng]
print(pes_eng)
上面的代码对英语句子有效,但对波斯语句子没有任何作用。
这里的输出是:
Traceback (most recent call last):
File ".\persian_to_english.py", line 29, in <module>
pes_eng[:,1] = [s.replace("؟!.،,?" ,"") for s in pes_eng]
File ".\persian_to_english.py", line 29, in <listcomp>
pes_eng[:,1] = [s.replace("؟!.،,?" ,"") for s in pes_eng]
AttributeError: 'numpy.ndarray' object has no attribute 'replace'
但我想要的是这样的:
['Who' 'چه کسی']
【问题讨论】:
-
看来你现在拥有的和你想要的之间的区别是波斯字符的一个子集。你能解释一下替换应该做什么吗?
标签: python nlp data-cleaning