在Python中清除属于不同语言的文本
我有一个文本集合,其中的句子要么完全是英语的,要么是印地语的,要么是马拉地语的,每个句子都附有ID,分别表示文本语言的0,1,2 不考虑任何语言的文本可能有HTML标记、标点符号等 我可以用下面的代码清理英语句子:在Python中清除属于不同语言的文本,python,python-2.7,unicode,nltk,Python,Python 2.7,Unicode,Nltk,我有一个文本集合,其中的句子要么完全是英语的,要么是印地语的,要么是马拉地语的,每个句子都附有ID,分别表示文本语言的0,1,2 不考虑任何语言的文本可能有HTML标记、标点符号等 我可以用下面的代码清理英语句子: import HTMLParser import re from nltk.corpus import stopwords from collections import Counter import pickle from string import punctuation #c
import HTMLParser
import re
from nltk.corpus import stopwords
from collections import Counter
import pickle
from string import punctuation
#creating html_parser object
html_parser = HTMLParser.HTMLParser()
cachedStopWords = set(stopwords.words("english"))
def cleanText(text,lang_id):
if lang_id == 0:
str1 = ''.join(text).decode('iso-8859-1')
else:
str1 = ''.join(text).encode('utf-8')
str1 = html_parser.unescape(str1)
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', str1)
#print "cleantext before puncts removed : " + cleantext
clean_puncts = re.compile(r'[\s{}]+'.format(re.escape(punctuation)))
cleantext = re.sub(clean_puncts,' ',cleantext)
#print " cleantext after puncts removed : " + cleantext
cleanest = cleantext.lower()
if lang_id == 0:
cleanertext = ' '.join([word for word in cleanest.split() if word not in cachedStopWords])
words = re.findall(r"[\w']+", cleanertext)
words_final = [x.encode('UTF8') for x in words]
else:
words_final = cleanest.split()
return words_final
此外,它会删除所有单词
印地语文本类似于
<p>भारत का इतिहास काफी समृद्ध एवं विस्तृत है। </p>
如何对印地语或马拉地语文本执行相同的操作?如果没有完整的文本文件,我们所能提供的解决方案将只会是一个不确定的结果 首先,检查正在读取到
cleanText()
中的字符串类型,它实际上是一个unicode字符串还是一个字节字符串?看
因此,如果您已经正确地读取了文件并确保所有内容都是unicode,那么在如何管理字符串(在python2或pytho3中)方面应该没有问题。以下示例证实了这一点:
>>> from HTMLParser import HTMLParser
>>> hp = HTMLParser()
>>> text = u"<p>भारत का इतिहास काफी समृद्ध एवं विस्तृत है। </p>"
>>> hp.unescape(text)
u'<p>\u092d\u093e\u0930\u0924 \u0915\u093e \u0907\u0924\u093f\u0939\u093e\u0938 \u0915\u093e\u092b\u0940 \u0938\u092e\u0943\u0926\u094d\u0927 \u090f\u0935\u0902 \u0935\u093f\u0938\u094d\u0924\u0943\u0924 \u0939\u0948\u0964 </p>'
>>> print hp.unescape(text)
<p>भारत का इतिहास काफी समृद्ध एवं विस्तृत है। </p>
>>> hp.unescape(text).split()
[u'<p>\u092d\u093e\u0930\u0924', u'\u0915\u093e', u'\u0907\u0924\u093f\u0939\u093e\u0938', u'\u0915\u093e\u092b\u0940', u'\u0938\u092e\u0943\u0926\u094d\u0927', u'\u090f\u0935\u0902', u'\u0935\u093f\u0938\u094d\u0924\u0943\u0924', u'\u0939\u0948\u0964', u'</p>']
>>> print " ".join(hp.unescape(text).split())
<p>भारत का इतिहास काफी समृद्ध एवं विस्तृत है। </p>
在本次讲座中,了解并遵循最佳实践很可能会解决您的unicode问题。幻灯片
也要注意这一点:在PyCon14上(但仅适用于python2.x
)
像这样打开utf8文件也可以解决您的问题:
import io
with io.open('myfile.txt', 'r', encoding='utf8') as fin:
for line in fin:
clean_text(line)
如果STDIN和STDOUT给您带来问题,请参阅
另见:
unicode
之后str
python2
或python3
?@alvas:python2.7能否显示完整的代码?答案在很大程度上取决于您如何读取文件。
>>> import re
>>> from string import punctuation
>>> p = re.compile(r'[\s{}]+'.format(re.escape(punctuation)))
>>> new_text = " ".join(hp.unescape(text).split())
>>> re.sub(p,' ', new_text)
u' p \u092d\u093e\u0930\u0924 \u0915\u093e \u0907\u0924\u093f\u0939\u093e\u0938 \u0915\u093e\u092b\u0940 \u0938\u092e\u0943\u0926\u094d\u0927 \u090f\u0935\u0902 \u0935\u093f\u0938\u094d\u0924\u0943\u0924 \u0939\u0948\u0964 p '
>>> print re.sub(p,' ', new_text)
p भारत का इतिहास काफी समृद्ध एवं विस्तृत है। p
import io
with io.open('myfile.txt', 'r', encoding='utf8') as fin:
for line in fin:
clean_text(line)