python读北大corpor的一段测试程序

本文介绍了一个简单的Python类,用于读取和处理特定格式的语料库文件。通过实例化该类,可以轻松获取文档标题等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    开始学着python,并为那个相似度开发包做准备.下面是一个小的测试程序:


#corpor file reader
#author: percylee
#time: 2006/08

class CorporFileReader:
    """reader for corpor file, which is labeled just like pku-corpor of
    Renmin Ribao. e.g. 在/p  1998年/t  来临/v  之际/f  ,/w ..."""
   
    def __init__(self,fpath,splitstr = ' '):
        """need file path to init CorporFileReader"""
        self.fpath = fpath
        self.title = ''
        self.document = ''
        self.docwordlist = []
        self.splitstr = splitstr

    def __docTitle(self):
        if self.docwordlist.__len__() <= 2:
             return None
        title = self.docwordlist[1]#for [0] is '199801-.../m'
        for wordno in range(2,self.docwordlist.__len__()):
             if self.docwordlist[wordno].find('/w') >= 0:
                 break
             title = title + self.docwordlist[wordno] + ' '
        return title

    def read(self):
        """read title and document from corpor file"""
        file = open(self.fpath)
        self.document = file.read()
        file.close()
        self.docwordlist = self.document.split(self.splitstr)
        self.title = self.__docTitle()

    def docTitle(self):
        """get document title"""
        return self.title
   
#test class
print '...in test...'
corporader = CorporFileReader('g://pyCode//pkucorpora1.txt')
print 'create one object of ' + corporader.__doc__
corporader.read()
print 'and read one document which/'s title is ' + corporader.docTitle()
 

    输出为:

...in test...
create one object of reader for corpor file, which is labeled just like pku-corpor of
    Renmin Ribao. e.g. 在/p  1998年/t  来临/v  之际/f  ,/w ...
and read one document which's title is 迈向/v  充满/v  希望/n  的/u  新/a  世纪/n 

    非常简单.但python着实有趣;很久以来没有享受到这种学写程序的单纯的快乐了^_^.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值