From 44ccf40742fc7e13e910e418ea5c43f3ee72dcb2 Mon Sep 17 00:00:00 2001 From: Rob Speer Date: Mon, 24 Feb 2014 18:03:31 -0500 Subject: [PATCH] A different plan for the top-level word_frequency function. When, before, I was importing wordfreq.query at the top level, this created a dependency loop when installing wordfreq. The new top-level __init__.py provides just a `word_frequency` function, which imports the real function as needed and calls it. This should avoid the dependency loop, at the cost of making `wordfreq.word_frequency` slightly less efficient than `wordfreq.query.word_frequency`. --- tests/test_queries.py | 3 ++- wordfreq/__init__.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/test_queries.py b/tests/test_queries.py index a86f6e8..8b44a23 100644 --- a/tests/test_queries.py +++ b/tests/test_queries.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals from nose.tools import eq_, assert_almost_equal, assert_greater -from wordfreq.query import (word_frequency, average_frequency, wordlist_size, +from wordfreq import word_frequency +from wordfreq.query import (average_frequency, wordlist_size, wordlist_info) diff --git a/wordfreq/__init__.py b/wordfreq/__init__.py index 0768d0d..959be05 100644 --- a/wordfreq/__init__.py +++ b/wordfreq/__init__.py @@ -1,2 +1,16 @@ -# Import some methods from wordfreq.query at the top level. -from wordfreq.query import word_frequency, iter_wordlist, random_words +# Make wordfreq.query available at the top level when needed. + +def word_frequency(word, lang, wordlist='multi', offset=0.): + """ + Get the frequency of `word` in the language with code `lang`, from the + specified `wordlist`. + + The offset gets added to all values, to monotonically account for the + fact that we have not observed all possible words. + + This is a wrapper for the real word_frequency function, so that it can + be imported at the top level instead of from `wordfreq.query`. + """ + from wordfreq.query import word_frequency as _real_word_frequency + return _real_word_frequency(word, lang, wordlist=wordlist, offset=offset) +