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/sentence_stemmer.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
32 lines (23 sloc)
861 Bytes
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 | |
import nltk | |
from unittest import TestCase | |
class SentenceStemmer(object): | |
# Example of Python-based string_types. | |
@classmethod | |
def create(cls, param): | |
return cls() | |
def __init__(self): | |
self._stemmer = nltk.stem.PorterStemmer() | |
def extract(self, text): | |
tokens = nltk.wordpunct_tokenize(text) | |
result = [] | |
for t in tokens: | |
result.append((0, 0, self._stemmer.stem(t), 1.0)) | |
return result | |
class SentenceStemmerTest(TestCase): | |
def test_extract(self): | |
ex = SentenceStemmer.create({}) | |
sentence = ' The foxes JUMPED over dogs ' | |
result = ex.extract(sentence) | |
self.assertEqual(['the', 'fox', 'jump', 'over', 'dog'], [x[2] for x in result]) |