Permalink
Browse files

Added Word len property, fixed Language KeyError

Also wrote tests for the above changes
  • Loading branch information...
jmsv committed Jul 6, 2018
1 parent 5dbba70 commit 086572f49899f918f395bdb8f867ae6a5702b1c8
Showing with 40 additions and 5 deletions.
  1. +6 −0 ety/language.py
  2. +14 −5 ety/word.py
  3. +20 −0 tests.py
@@ -19,3 +19,9 @@ def __repr__(self):

def __str__(self):
return self.name

def __eq__(self, other):
return (
self.iso == other.iso and
self.name == other.name
)
@@ -2,29 +2,35 @@
# -*- coding: utf-8 -*-

import colorful
from six import string_types

from .data import etyms as etymwn_data
from .language import Language
from .tree import EtyTree

from six import string_types


class Word(object):
def __init__(self, word, language='eng', is_source=False):
if not isinstance(word, string_types):
raise ValueError('word must be a string')
self._word = word
self._language = Language(language)
self.is_source = is_source

if isinstance(language, Language):
self._language = language
else:
self._language = Language(language)

self.is_source = bool(is_source)

self._origins = {
'direct': [],
'recursive': []
}
self._id = u"{}:{}".format(self.word, self.language.iso)

def origins(self, recursive=False):
if self.word not in etymwn_data[self.language.iso]:
if (self.language.iso not in etymwn_data or
self.word not in etymwn_data[self.language.iso]):
# There are no roots for this word
return []

@@ -79,3 +85,6 @@ def __repr__(self):
return u'Word({word}, {lang} [{iso}])'.format(
word=self.word, lang=self.language, iso=self.language.iso
)

def __len__(self):
return len(self.word)
@@ -71,11 +71,27 @@ def test_lang(self):
self.assertEqual(ety.Language('enm').name,
'Middle English (1100-1500)')

def test_lang_equal(self):
a = ety.Language('eng')
b = ety.Language('eng')
c = ety.Language('lat')
self.assertEqual(a, b)
self.assertNotEqual(b, c)

def test_word_equal(self):
a = ety.Word('electronic')
b = ety.Word('electronic', language='lat')
c = ety.Word('electrinus')
self.assertNotEqual(a, b)
self.assertNotEqual(a, c)

def test_tree(self):
self.assertGreaterEqual(len(str(
ety.tree('aerodynamically')).split('\n')), 10)
self.assertGreaterEqual(len(str(
ety.tree('fabric')).split('\n')), 4)
self.assertGreaterEqual(len(str(
ety.tree('potato')).split('\n')), 2)

def test_census_words(self):
a = ety.census(['alphabet', 'avocado', 'guitar'])
@@ -98,6 +114,10 @@ def test_census_origins(self):

self.assertEqual(d.origins(recursive=True), e + f)

def test_word_len(self):
word = ety.random_word().word
self.assertEqual(len(ety.Word(word)), len(word))

@stdout_capture
def test_cli_no_args(self, output):
words = ["test"]

0 comments on commit 086572f

Please sign in to comment.