當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。