# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re
from .libvoikko import Voikko

v = Voikko(u'fi-x-morphoid')
# Replace all non letter characters with space
RE_WS_REPLACE = re.compile("[^\w]", re.UNICODE)
RE_FIND_COMPOUNDS = re.compile(r'\(([\w\+]+)\)', re.UNICODE)
# .findall(u'+pitkä(pitkä)+tukka(tukka)+inen(+inen)')


def voikko_analyze(text):
    text = RE_WS_REPLACE.sub(' ', text)
    words = text.split(' ')
    # Strip spaces
    words = [x.strip() for x in words]
    # Remove empty items
    words = filter(None, words)
    # Transform to unicode when using python2
    # print words
    # words = [unicode(x, 'utf8') for x in words]

    # Loop all words and analyze them
    analyzed = []
    for word in words:
        aword = v.analyze(word)
        # i = 0
        if aword:
            for f in aword:
                f['found'] = True
                f['original'] = word
                wordbases = RE_FIND_COMPOUNDS.findall(f['WORDBASES'])
                f['wordbase_list'] = [x for x in wordbases if not x.startswith('+')]
                # print(f['WORDBASES'], RE_FIND_COMPOUNDS.findall(f['WORDBASES']))
                analyzed.append(f)
                # i += 1
                # if 'BASEFORM' in f:
                #     base = f['BASEFORM']
                #     print(u'{:<30} {:<30} {} -- {}'.format(base,
                #         word, i, f['CLASS']))
                # base = f['BASEFORM']
                # print(u'{:<30} {:<30} {} -- {}'.format(base,
                #         word, i, f['CLASS']))
                # print(f)
        else:
            analyzed.append({'BASEFORM': word,
                             'found': False, 'original': word})
            # base = word
            # print(base + ' -- NOT FOUND')
    return analyzed


def baseform_text(text):
    analyzed = voikko_analyze(text)
    wordlist = [x['BASEFORM'].lower() for x in analyzed]
    wordlist += [x['original'].lower() for x in analyzed]
    base_text = u' '.join(set(wordlist))
    return base_text


if __name__ == '__main__':
    import sys
    words = [unicode(x, 'utf8') for x in sys.argv[1:]]
    print(baseform_text(u' '.join(words)))
@@ -954,33 +954,37 @@
 POUNDS.findall(f
-%5B
+.get(
 'WORDBASES'%5D)%0A  
@@ -978,17 +978,21 @@
 RDBASES'
-%5D
+, '')
 )%0A      
@@ -2182,8 +2182,9 @@
 words)))
+%0A

Fix missing WORDBASES case