【发布时间】:2019-04-07 00:51:57
【问题描述】:
我试图将用印地语编写的段落拆分成句子。问题是该段落中并非所有句子都以“|”结尾所以使用 split() 的想法失败了。任何人都可以推荐任何解决方案吗?
这是一段:
विकिपीडियाविकिपीडियासभीविषयोंपरप्रामाणिकऔर,परिवर्तनपरिवर्तनवपुनर्वितरणकेलिएस्वतन्त्रज्ञानकोशकाएकबहुभाषीयप्रकल्पप्रकल्पज्ञानकोशबनानेकाएकबहुभाषीयप्रकल्पप्रकल्प यहयथासम्भवनिष्पक्षदृष्टिकोणवालीसूचनाप्रसारितकरनेकेलिएकृतसंकल्पहै。 सर्वप्रथमअंग्रेज़ीविकिपीडियाजनवरी2001मेंआरम्भकियागयाथा,औरहिन्दीविकिपीडियाकाशुभारम्भजुलाई2003मेंहुआ。 सहायतापृष्ठपरऔरप्रयोगस्थलप्रयोगकरकेदेखेंकिआपस्वयंलेख
这是我的代码:
import codecs
class Tokenizer():
def __init__(self,text):
self.text=text.decode('utf-8')
self.clean_text()
self.sentences=[]
self.final_list=[]
self.bsentences=[]
self.asentences=[]
self.final_sentences=self.bsentences+self.asentences
def readFromFile(self,filename):
f=codecs.open(filename,encoding='utf-8')
self.text=f.read()
self.clean_text()
def print_sentences(self,sentences):
for i in self.sentences:
print i.encode('utf-8')
def cleanText(self):
text=self.text
text=re.sub(r'(\d+)',r'',text)
text=text.replace(u',','')
text=text.replace(u'"','')
text=text.replace(u'"','')
text=text.replace(u':','')
text=text.replace(u"'",'')
text=text.replace(u"‘‘",'')
text=text.replace(u"’’",'')
text=text.replace(u"''",'')
text=text.replace(u".",'')
self.text=text
def getSentence(self):
text=self.text
self.bsentences=text.split(u"।")
sw=codecs.open("stopwords.txt",encoding='utf-8')
stopwords=[x.strip() for x in sw.readlines()]
sentences=[s for s in sw.readlines() if s in enumerate(stopwords)]
return self.final_sentences
t=Tokenizer('')
t.readFromFile('sample.txt')
t.getSentences()
t.print_sentences()
【问题讨论】:
-
pyhton 3 还是 2.7?还是您需要跨版本代码?请不要标记。-垃圾邮件
-
如果您认为我发送了垃圾邮件,我深表歉意。但对我来说,我对任何版本都很好。
标签: python python-3.x python-unicode