# Path: feet/entities/jptools.py
# class JAParser(LoggingMixin):
#     __metaclass__ = SingletonMetaClass
# 
#     @memoized
#     def mecab(self):
#         self.logger.warning('Initialize Mecab')
#         return MeCab.Tagger('-d %s' % settings.mecab.mecabdict)
# 
#     def word_tokenize(self, content, with_separators=True):
#         if content is None:
#             return None
#         output = []
#         node = self.mecab.parseToNode(content)
#         while node:
#             features = node.feature.split(',')
#             self.logger.debug('%s %s %s' % (node.surface, features[0],
#                                             features[1]))
#             if node.surface != '':
#                 if features[0] == F1_SEPARATOR:
#                     if with_separators:
#                         output.append(node.surface)
#                 else:
#                     output.append(node.surface)
#             node = node.next
#         return output
# 
#     def sent_tokenize(self, content):
#         if content is None:
#             return []
#         node = self.mecab.parseToNode(content)
#         separators = []
#         while node:
#             features = node.feature.split(',')
#             self.logger.debug('%s %s %s' % (node.surface, features[0],
#                                             features[1]))
#             if node.surface == 'BOS/EOS' or (features[0] == F1_SEPARATOR and
#                                              features[1] == '句点'):
#                 if node.surface != '':
#                     separators.append(node.surface)
#             node = node.next
#         return filter(None, re.split(r"%s" % ('|'.join(separators)), content))
# 
#     def extract_entities(self, content, with_numbers=False):
#         """
#         Extract good candidates for entities in a text.
#         """
#         output = []
#         node = self.mecab.parseToNode(content)
#         sentence = 0
#         entity = []
#         while node:
#             features = node.feature.split(',')
#             f1 = features[0]
#             f2 = features[1]
#             f3 = features[2]
#             if f1 == F1_NOUN and \
#                     f2 != F2_IND_VERB and f2 != F2_NO_MEANING_NAMES and \
#                     f2 != F2_ADVERBS and \
#                     f2 != F2_PRONOUN and f2 != F2_ADJECTIVE and \
#                     f2 != F2_NUMBER and \
#                     f2 != F2_SUFFIX and \
#                     f3 != F3_ADVERBS and \
#                     node.surface not in STOPLIST:
#                 entity.append(node.surface.lower())
#             else:
#                 if len(entity) > 0:
#                     output.append(''.join(entity))
#                     entity = []
#             if f1 == F1_SEPARATOR and \
#                     (f2 == '句点'):
#                 sentence += 1
#             # move to next morphem
#             node = node.next
#         if (len(entity) > 0):
#             output.append(entity)
#         return output
# 
#     def extract_dates(self, content, with_year=None):
#         """
#         Extract dates from text in Japanese.
#         """
#         if with_year is None:
#             with_year = str(datetime.now().year)
#         # TODO: use regex module instead of re
#         re_set = (
#             re.compile(ur'\d{1,4}年\d{1,2}月\d{1,2}日', re.UNICODE),
#             re.compile(ur'\d{1,2}月\d{1,2}日', re.UNICODE),
#             # TODO: deal with days in text
#             re.compile(ur'\d{1,4}/\d{1}/\d{1}', re.UNICODE),
#             re.compile(ur'\d{1,2}/\d{1,2}', re.UNICODE)
#         )
#         res = []
#         for re_i in re_set:
#             res_i = re_i.findall(content.decode('utf8'))
#             for el in res_i:
#                 parsed_date = parse(el)
#                 if parsed_date is not None:
#                     formated_parsed_date = datetime.strftime(parsed_date,
#                                                              '%Y-%m-%d')
#                     if formated_parsed_date not in res:
#                         res.append(formated_parsed_date)
#         return res


from feet.entities.jptools import JAParser
import unittest
# -*- coding: utf8 -*-
# test_jptools.py
# Test the Japanese NLP tools
#
# Author:   Romary Dupuis <romary.dupuis@altarika.com>
#
# Copyright (C) 2016 Romary Dupuis
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html



class JAParserTests(unittest.TestCase):
    def setUp(self):
        pass

    def test_singleton(self):
        self.assertEqual(JAParser(), JAParser())
        self.assertEqual(JAParser().mecab, JAParser().mecab)

    def test_word_tokenize_text_is_none(self):

# Path: feet/entities/jptools.py
# class JAParser(LoggingMixin):
#     __metaclass__ = SingletonMetaClass
# 
#     @memoized
#     def mecab(self):
#         self.logger.warning('Initialize Mecab')
#         return MeCab.Tagger('-d %s' % settings.mecab.mecabdict)
# 
#     def word_tokenize(self, content, with_separators=True):
#         if content is None:
#             return None
#         output = []
#         node = self.mecab.parseToNode(content)
#         while node:
#             features = node.feature.split(',')
#             self.logger.debug('%s %s %s' % (node.surface, features[0],
#                                             features[1]))
#             if node.surface != '':
#                 if features[0] == F1_SEPARATOR:
#                     if with_separators:
#                         output.append(node.surface)
#                 else:
#                     output.append(node.surface)
#             node = node.next
#         return output
# 
#     def sent_tokenize(self, content):
#         if content is None:
#             return []
#         node = self.mecab.parseToNode(content)
#         separators = []
#         while node:
#             features = node.feature.split(',')
#             self.logger.debug('%s %s %s' % (node.surface, features[0],
#                                             features[1]))
#             if node.surface == 'BOS/EOS' or (features[0] == F1_SEPARATOR and
#                                              features[1] == '句点'):
#                 if node.surface != '':
#                     separators.append(node.surface)
#             node = node.next
#         return filter(None, re.split(r"%s" % ('|'.join(separators)), content))
# 
#     def extract_entities(self, content, with_numbers=False):
#         """
#         Extract good candidates for entities in a text.
#         """
#         output = []
#         node = self.mecab.parseToNode(content)
#         sentence = 0
#         entity = []
#         while node:
#             features = node.feature.split(',')
#             f1 = features[0]
#             f2 = features[1]
#             f3 = features[2]
#             if f1 == F1_NOUN and \
#                     f2 != F2_IND_VERB and f2 != F2_NO_MEANING_NAMES and \
#                     f2 != F2_ADVERBS and \
#                     f2 != F2_PRONOUN and f2 != F2_ADJECTIVE and \
#                     f2 != F2_NUMBER and \
#                     f2 != F2_SUFFIX and \
#                     f3 != F3_ADVERBS and \
#                     node.surface not in STOPLIST:
#                 entity.append(node.surface.lower())
#             else:
#                 if len(entity) > 0:
#                     output.append(''.join(entity))
#                     entity = []
#             if f1 == F1_SEPARATOR and \
#                     (f2 == '句点'):
#                 sentence += 1
#             # move to next morphem
#             node = node.next
#         if (len(entity) > 0):
#             output.append(entity)
#         return output
# 
#     def extract_dates(self, content, with_year=None):
#         """
#         Extract dates from text in Japanese.
#         """
#         if with_year is None:
#             with_year = str(datetime.now().year)
#         # TODO: use regex module instead of re
#         re_set = (
#             re.compile(ur'\d{1,4}年\d{1,2}月\d{1,2}日', re.UNICODE),
#             re.compile(ur'\d{1,2}月\d{1,2}日', re.UNICODE),
#             # TODO: deal with days in text
#             re.compile(ur'\d{1,4}/\d{1}/\d{1}', re.UNICODE),
#             re.compile(ur'\d{1,2}/\d{1,2}', re.UNICODE)
#         )
#         res = []
#         for re_i in re_set:
#             res_i = re_i.findall(content.decode('utf8'))
#             for el in res_i:
#                 parsed_date = parse(el)
#                 if parsed_date is not None:
#                     formated_parsed_date = datetime.strftime(parsed_date,
#                                                              '%Y-%m-%d')
#                     if formated_parsed_date not in res:
#                         res.append(formated_parsed_date)
#         return res


# Path: tests/test_jptools.py
from feet.entities.jptools import JAParser
import unittest

# -*- coding: utf8 -*-
# test_jptools.py
# Test the Japanese NLP tools
#
# Author:   Romary Dupuis <romary.dupuis@altarika.com>
#
# Copyright (C) 2016 Romary Dupuis
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html



class JAParserTests(unittest.TestCase):
    def setUp(self):
        pass

    def test_singleton(self):
        self.assertEqual(JAParser(), JAParser())
        self.assertEqual(JAParser().mecab, JAParser().mecab)

    def test_word_tokenize_text_is_none(self):

        self.assertEqual(JAParser().word_tokenize(None), None)