当前位置: 首页>>代码示例>>Python>>正文


Python data.SeekableUnicodeStreamReader方法代码示例

本文整理汇总了Python中nltk.data.SeekableUnicodeStreamReader方法的典型用法代码示例。如果您正苦于以下问题:Python data.SeekableUnicodeStreamReader方法的具体用法?Python data.SeekableUnicodeStreamReader怎么用?Python data.SeekableUnicodeStreamReader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在nltk.data的用法示例。


在下文中一共展示了data.SeekableUnicodeStreamReader方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _open

# 需要导入模块: from nltk import data [as 别名]
# 或者: from nltk.data import SeekableUnicodeStreamReader [as 别名]
def _open(self):
        """
        Open the file stream associated with this corpus view.  This
        will be called performed if any value is read from the view
        while its file stream is closed.
        """
        if isinstance(self._fileid, PathPointer):
            self._stream = self._fileid.open(self._encoding)
        elif self._encoding:
            self._stream = SeekableUnicodeStreamReader(
                open(self._fileid, 'rb'), self._encoding)
        else:
            self._stream = open(self._fileid, 'rb') 
开发者ID:Thejas-1,项目名称:Price-Comparator,代码行数:15,代码来源:util.py

示例2: _read_xml_fragment

# 需要导入模块: from nltk import data [as 别名]
# 或者: from nltk.data import SeekableUnicodeStreamReader [as 别名]
def _read_xml_fragment(self, stream):
        """
        Read a string from the given stream that does not contain any
        un-closed tags.  In particular, this function first reads a
        block from the stream of size ``self._BLOCK_SIZE``.  It then
        checks if that block contains an un-closed tag.  If it does,
        then this function either backtracks to the last '<', or reads
        another block.
        """
        fragment = ''

        if isinstance(stream, SeekableUnicodeStreamReader):
            startpos = stream.tell()
        while True:
            # Read a block and add it to the fragment.
            xml_block = stream.read(self._BLOCK_SIZE)
            fragment += xml_block

            # Do we have a well-formed xml fragment?
            if self._VALID_XML_RE.match(fragment):
                return fragment

            # Do we have a fragment that will never be well-formed?
            if re.search('[<>]', fragment).group(0) == '>':
                pos = stream.tell() - (
                    len(fragment)-re.search('[<>]', fragment).end())
                raise ValueError('Unexpected ">" near char %s' % pos)

            # End of file?
            if not xml_block:
                raise ValueError('Unexpected end of file: tag not closed')

            # If not, then we must be in the middle of a <..tag..>.
            # If appropriate, backtrack to the most recent '<'
            # character.
            last_open_bracket = fragment.rfind('<')
            if last_open_bracket > 0:
                if self._VALID_XML_RE.match(fragment[:last_open_bracket]):
                    if isinstance(stream, SeekableUnicodeStreamReader):
                        stream.seek(startpos)
                        stream.char_seek_forward(last_open_bracket)
                    else:
                        stream.seek(-(len(fragment)-last_open_bracket), 1)
                    return fragment[:last_open_bracket]

            # Otherwise, read another block. (i.e., return to the
            # top of the loop.) 
开发者ID:Thejas-1,项目名称:Price-Comparator,代码行数:49,代码来源:xmldocs.py


注:本文中的nltk.data.SeekableUnicodeStreamReader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。