Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
jubatus/plugin/src/fv_converter/python_bridge/python/space_splitter.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (35 sloc)
1.18 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import absolute_import, division, print_function, unicode_literals | |
from unittest import TestCase | |
class SpaceSplitter(object): | |
# Example of Python-based string_types. | |
@classmethod | |
def create(cls, param): | |
return cls() | |
def split(self, text): | |
""" | |
Splits the input text with space. | |
The input text is typed as ``str``. | |
This method should return list of boundaries (pair of beginning | |
position and length), e.g., ``[ (0, 1), (3, 4) ]``. | |
""" | |
result = [] | |
begin = 0 | |
length = 0 | |
for (pos, char) in enumerate(text): | |
if char == ' ': | |
if 0 < length: | |
result.append((begin, length)) | |
length = 0 | |
begin = pos + 1 | |
else: | |
length += 1 | |
if length != 0: | |
result.append((begin, length)) | |
return result | |
class SpaceSplitterTest(TestCase): | |
def test_split(self): | |
ex = SpaceSplitter.create({}) | |
sentence = b' hi jubatus !' | |
# pos: 0123456789ABCD | |
self.assertEqual([(1, 2), (5, 7), (13, 1)], ex.split(sentence)) |