Skip to content

Commit

Permalink
Modified nltk-internal imports to import values from the modules wher…
Browse files Browse the repository at this point in the history
…e they are defined -- e.g., changed "from nltk import FreqDist" to "from nltk.probability import FreqDist". This helps to avoid circular import problems, and will be helpful if we choose to do any lazy importing to speed up nltk import time.

svn/trunk@8144
  • Loading branch information
Edward Loper committed Jun 1, 2009
1 parent 919d0a1 commit a015b5d
Show file tree
Hide file tree
Showing 55 changed files with 179 additions and 168 deletions.
5 changes: 3 additions & 2 deletions nltk/app/chartparser_app.py
Expand Up @@ -46,8 +46,9 @@
import os.path

from nltk.parse.chart import *
from nltk import tokenize, Tree, Nonterminal, parse_cfg
from nltk import in_idle
from nltk.tree import Tree
from nltk.grammar import Nonterminal, parse_cfg
from nltk.util import in_idle
from nltk.draw.util import *
from nltk.draw.cfg import CFGEditor
from nltk.draw.tree import tree_to_treesegment, TreeSegmentWidget
Expand Down
24 changes: 13 additions & 11 deletions nltk/app/chunkparser_app.py
Expand Up @@ -22,7 +22,9 @@
import re
import random

from nltk import corpus, Tree, chunk, in_idle
import nltk
from nltk.tree import Tree
from nltk.util import in_idle
from nltk.draw.util import *

class RegexpChunkApp(object):
Expand Down Expand Up @@ -268,9 +270,9 @@ def __init__(self, devset_name='conll2000', devset=None,
# Named development sets:
if devset is None:
if devset_name == 'conll2000':
devset = corpus.conll2000.chunked_sents('train.txt')#[:100]
devset = nltk.corpus.conll2000.chunked_sents('train.txt')#[:100]
elif devset == 'treebank':
devset = corpus.treebank_chunk.chunked_sents()#[:100]
devset = nltk.corpus.treebank_chunk.chunked_sents()#[:100]
else:
raise ValueError('Unknown development set %s' % devset_name)

Expand Down Expand Up @@ -318,7 +320,7 @@ def __init__(self, devset_name='conll2000', devset=None,
"""The index of the next sentence in the development set that
should be looked at by the eval demon."""

self._eval_score = chunk.ChunkScore(chunk_node=chunk_node)
self._eval_score = nltk.chunk.ChunkScore(chunk_node=chunk_node)
"""The L{ChunkScore <nltk.chunk.ChunkScore>} object that's used
to keep track of the score of the current grammar on the
development set."""
Expand Down Expand Up @@ -571,7 +573,7 @@ def _eval_demon(self):
self._eval_normalized_grammar = None
return
self._eval_index = 0
self._eval_score = chunk.ChunkScore(chunk_node=
self._eval_score = nltk.chunk.ChunkScore(chunk_node=
self._chunk_node)
self._eval_grammar = self.grammar
self._eval_normalized_grammar = self.normalized_grammar
Expand Down Expand Up @@ -824,7 +826,7 @@ def show_trace(self, *e):
self.devsetbox.insert('end', tagseq+'\n')
self.devsetbox.tag_add('wrapindent','end -2c linestart','end -2c')
# Run a partial parser, and extract gold & test chunks
chunker = chunk.RegexpChunkParser(rules[:i])
chunker = nltk.chunk.RegexpChunkParser(rules[:i])
test_tree = self._chunkparse(gold_tree.leaves())
gold_chunks = self._chunks(gold_tree)
test_chunks = self._chunks(test_tree)
Expand Down Expand Up @@ -896,11 +898,11 @@ def _view_history(self, index):
self.normalized_grammar = self.normalize_grammar(
self._history[index][0])
if self.normalized_grammar:
rules = [chunk.regexp.RegexpChunkRule.parse(line)
rules = [nltk.chunk.regexp.RegexpChunkRule.parse(line)
for line in self.normalized_grammar.split('\n')]
else:
rules = []
self.chunker = chunk.RegexpChunkParser(rules)
self.chunker = nltk.chunk.RegexpChunkParser(rules)
# Show the score.
self._eval_plot()
# Update the devset box
Expand Down Expand Up @@ -1028,7 +1030,7 @@ def _grammarcheck(self, grammar):
line = re.sub(r'((\\.|[^#])*)(#.*)?', r'\1', line)
line = line.strip()
if line:
try: chunk.regexp.RegexpChunkRule.parse(line)
try: nltk.chunk.regexp.RegexpChunkRule.parse(line)
except ValueError, e:
self.grammarbox.tag_add('error', '%s.0' % (lineno+1),
'%s.0 lineend' % (lineno+1))
Expand Down Expand Up @@ -1061,7 +1063,7 @@ def update(self, *event):
try:
# Note: the normalized grammar has no blank lines.
if normalized_grammar:
rules = [chunk.regexp.RegexpChunkRule.parse(line)
rules = [nltk.chunk.regexp.RegexpChunkRule.parse(line)
for line in normalized_grammar.split('\n')]
else:
rules = []
Expand All @@ -1071,7 +1073,7 @@ def update(self, *event):
self.chunker = None
return

self.chunker = chunk.RegexpChunkParser(rules)
self.chunker = nltk.chunk.RegexpChunkParser(rules)
self.grammarbox.tag_remove('error', '1.0', 'end')
self.grammar_changed = time.time()
# Display the results
Expand Down
36 changes: 19 additions & 17 deletions nltk/app/collocations_app.py
Expand Up @@ -8,7 +8,9 @@

import threading

from nltk import corpus, FreqDist, in_idle
import nltk
from nltk.util import in_idle
from nltk.probability import FreqDist
from nltk.text import Text as TextDomain
from nltk.draw.util import *

Expand All @@ -18,37 +20,37 @@
_DEFAULT = 'English: Brown Corpus (Humor)'
_CORPORA = {
'Catalan: CESS-CAT Corpus':
lambda: corpus.cess_cat.words(),
lambda: nltk.corpus.cess_cat.words(),
'English: Brown Corpus':
lambda: corpus.brown.words(),
lambda: nltk.corpus.brown.words(),
'English: Brown Corpus (Press)':
lambda: corpus.brown.words(categories=['news', 'editorial', 'reviews']),
lambda: nltk.corpus.brown.words(categories=['news', 'editorial', 'reviews']),
'English: Brown Corpus (Religion)':
lambda: corpus.brown.words(categories='religion'),
lambda: nltk.corpus.brown.words(categories='religion'),
'English: Brown Corpus (Learned)':
lambda: corpus.brown.words(categories='learned'),
lambda: nltk.corpus.brown.words(categories='learned'),
'English: Brown Corpus (Science Fiction)':
lambda: corpus.brown.words(categories='science_fiction'),
lambda: nltk.corpus.brown.words(categories='science_fiction'),
'English: Brown Corpus (Romance)':
lambda: corpus.brown.words(categories='romance'),
lambda: nltk.corpus.brown.words(categories='romance'),
'English: Brown Corpus (Humor)':
lambda: corpus.brown.words(categories='humor'),
lambda: nltk.corpus.brown.words(categories='humor'),
'English: NPS Chat Corpus':
lambda: corpus.nps_chat.words(),
lambda: nltk.corpus.nps_chat.words(),
'English: Wall Street Journal Corpus':
lambda: corpus.treebank.words(),
lambda: nltk.corpus.treebank.words(),
'Chinese: Sinica Corpus':
lambda: corpus.sinica_treebank.words(),
lambda: nltk.corpus.sinica_treebank.words(),
'Dutch: Alpino Corpus':
lambda: corpus.alpino.words(),
lambda: nltk.corpus.alpino.words(),
'Hindi: Indian Languages Corpus':
lambda: corpus.indian.words(files='hindi.pos'),
lambda: nltk.corpus.indian.words(files='hindi.pos'),
'Portuguese: Floresta Corpus (Portugal)':
lambda: corpus.floresta.words(),
lambda: nltk.corpus.floresta.words(),
'Portuguese: MAC-MORPHO Corpus (Brazil)':
lambda: corpus.mac_morpho.words(),
lambda: nltk.corpus.mac_morpho.words(),
'Spanish: CESS-ESP Corpus':
lambda: corpus.cess_esp.words(),
lambda: nltk.corpus.cess_esp.words(),
}

class CollocationsView:
Expand Down
51 changes: 26 additions & 25 deletions nltk/app/concordance_app.py
Expand Up @@ -10,7 +10,8 @@
import re
import threading

from nltk import corpus, in_idle
import nltk
from nltk.util import in_idle
from nltk.draw.util import *

WORD_OR_TAG = '[^/ ]+'
Expand All @@ -27,53 +28,53 @@
_DEFAULT = 'English: Brown Corpus (Humor, simplified)'
_CORPORA = {
'Catalan: CESS-CAT Corpus (simplified)':
lambda: corpus.cess_cat.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.cess_cat.tagged_sents(simplify_tags=True),
'English: Brown Corpus':
lambda: corpus.brown.tagged_sents(),
lambda: nltk.corpus.brown.tagged_sents(),
'English: Brown Corpus (simplified)':
lambda: corpus.brown.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.brown.tagged_sents(simplify_tags=True),
'English: Brown Corpus (Press, simplified)':
lambda: corpus.brown.tagged_sents(categories=['news', 'editorial', 'reviews'], simplify_tags=True),
lambda: nltk.corpus.brown.tagged_sents(categories=['news', 'editorial', 'reviews'], simplify_tags=True),
'English: Brown Corpus (Religion, simplified)':
lambda: corpus.brown.tagged_sents(categories='religion', simplify_tags=True),
lambda: nltk.corpus.brown.tagged_sents(categories='religion', simplify_tags=True),
'English: Brown Corpus (Learned, simplified)':
lambda: corpus.brown.tagged_sents(categories='learned', simplify_tags=True),
lambda: nltk.corpus.brown.tagged_sents(categories='learned', simplify_tags=True),
'English: Brown Corpus (Science Fiction, simplified)':
lambda: corpus.brown.tagged_sents(categories='science_fiction', simplify_tags=True),
lambda: nltk.corpus.brown.tagged_sents(categories='science_fiction', simplify_tags=True),
'English: Brown Corpus (Romance, simplified)':
lambda: corpus.brown.tagged_sents(categories='romance', simplify_tags=True),
lambda: nltk.corpus.brown.tagged_sents(categories='romance', simplify_tags=True),
'English: Brown Corpus (Humor, simplified)':
lambda: corpus.brown.tagged_sents(categories='humor', simplify_tags=True),
lambda: nltk.corpus.brown.tagged_sents(categories='humor', simplify_tags=True),
'English: NPS Chat Corpus':
lambda: corpus.nps_chat.tagged_posts(),
lambda: nltk.corpus.nps_chat.tagged_posts(),
'English: NPS Chat Corpus (simplified)':
lambda: corpus.nps_chat.tagged_posts(simplify_tags=True),
lambda: nltk.corpus.nps_chat.tagged_posts(simplify_tags=True),
'English: Wall Street Journal Corpus':
lambda: corpus.treebank.tagged_sents(),
lambda: nltk.corpus.treebank.tagged_sents(),
'English: Wall Street Journal Corpus (simplified)':
lambda: corpus.treebank.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.treebank.tagged_sents(simplify_tags=True),
'Chinese: Sinica Corpus':
lambda: corpus.sinica_treebank.tagged_sents(),
lambda: nltk.corpus.sinica_treebank.tagged_sents(),
'Chinese: Sinica Corpus (simplified)':
lambda: corpus.sinica_treebank.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.sinica_treebank.tagged_sents(simplify_tags=True),
'Dutch: Alpino Corpus':
lambda: corpus.alpino.tagged_sents(),
lambda: nltk.corpus.alpino.tagged_sents(),
'Dutch: Alpino Corpus (simplified)':
lambda: corpus.alpino.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.alpino.tagged_sents(simplify_tags=True),
'Hindi: Indian Languages Corpus':
lambda: corpus.indian.tagged_sents(files='hindi.pos'),
lambda: nltk.corpus.indian.tagged_sents(files='hindi.pos'),
'Hindi: Indian Languages Corpus (simplified)':
lambda: corpus.indian.tagged_sents(files='hindi.pos', simplify_tags=True),
lambda: nltk.corpus.indian.tagged_sents(files='hindi.pos', simplify_tags=True),
'Portuguese: Floresta Corpus (Portugal)':
lambda: corpus.floresta.tagged_sents(),
lambda: nltk.corpus.floresta.tagged_sents(),
'Portuguese: Floresta Corpus (Portugal, simplified)':
lambda: corpus.floresta.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.floresta.tagged_sents(simplify_tags=True),
'Portuguese: MAC-MORPHO Corpus (Brazil)':
lambda: corpus.mac_morpho.tagged_sents(),
lambda: nltk.corpus.mac_morpho.tagged_sents(),
'Portuguese: MAC-MORPHO Corpus (Brazil, simplified)':
lambda: corpus.mac_morpho.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.mac_morpho.tagged_sents(simplify_tags=True),
'Spanish: CESS-ESP Corpus (simplified)':
lambda: corpus.cess_esp.tagged_sents(simplify_tags=True),
lambda: nltk.corpus.cess_esp.tagged_sents(simplify_tags=True),
}

class ConcordanceSearchView(object):
Expand Down
9 changes: 5 additions & 4 deletions nltk/app/rdparser_app.py
Expand Up @@ -68,8 +68,9 @@

import string

from nltk import parse, tokenize, Tree, in_idle

import nltk
from nltk.tree import Tree
from nltk.util import in_idle
from nltk.draw.util import *
from nltk.draw.tree import *
from nltk.draw.cfg import *
Expand All @@ -86,7 +87,7 @@ class RecursiveDescentApp(object):
"""
def __init__(self, grammar, sent, trace=0):
self._sent = sent
self._parser = parse.SteppingRecursiveDescentParser(grammar, trace)
self._parser = nltk.parse.SteppingRecursiveDescentParser(grammar, trace)

# Set up the main window.
self._top = Tk()
Expand Down Expand Up @@ -866,7 +867,7 @@ def app():
Create a recursive descent parser demo, using a simple grammar and
text.
"""
from nltk import parse_cfg
from nltk.grammar import parse_cfg
grammar = parse_cfg("""
# Grammatical productions.
S -> NP VP
Expand Down
16 changes: 8 additions & 8 deletions nltk/app/srparser_app.py
Expand Up @@ -77,8 +77,8 @@

import string

from nltk import parse, tokenize, in_idle

import nltk
from nltk.util import in_idle
from nltk.draw.util import *
from nltk.draw.tree import *
from nltk.draw.cfg import CFGEditor
Expand All @@ -92,11 +92,11 @@ class ShiftReduceApp(object):
can shift tokens onto the stack, and can perform reductions on the
top elements of the stack. A "step" button simply steps through
the parsing process, performing the operations that
C{parse.ShiftReduceParser} would use.
C{nltk.parse.ShiftReduceParser} would use.
"""
def __init__(self, grammar, sent, trace=0):
self._sent = sent
self._parser = parse.SteppingShiftReduceParser(grammar, trace)
self._parser = nltk.parse.SteppingShiftReduceParser(grammar, trace)

# Set up the main window.
self._top = Tk()
Expand Down Expand Up @@ -382,7 +382,7 @@ def _redraw(self):
# Draw the stack.
stackx = 5
for tok in self._parser.stack():
if isinstance(tok, parse.Tree):
if isinstance(tok, Tree):
attribs = {'tree_color': '#4080a0', 'tree_width': 2,
'node_font': self._boldfont,
'node_color': '#006060',
Expand Down Expand Up @@ -685,7 +685,7 @@ def _animate_reduce_frame(self, frame, widgets, dy):
for widget in widgets:
self._cframe.remove_widget(widget)
tok = self._parser.stack()[-1]
if not isinstance(tok, parse.Tree): raise ValueError()
if not isinstance(tok, Tree): raise ValueError()
label = TextWidget(self._canvas, str(tok.node), color='#006060',
font=self._boldfont)
widget = TreeSegmentWidget(self._canvas, label, widgets,
Expand All @@ -708,7 +708,7 @@ def _animate_reduce_frame(self, frame, widgets, dy):
#
# # Make a new one.
# tok = self._parser.stack()[-1]
# if isinstance(tok, parse.Tree):
# if isinstance(tok, Tree):
# attribs = {'tree_color': '#4080a0', 'tree_width': 2,
# 'node_font': bold, 'node_color': '#006060',
# 'leaf_color': '#006060', 'leaf_font':self._font}
Expand Down Expand Up @@ -772,7 +772,7 @@ def app():
text.
"""

from nltk import Nonterminal, Production, ContextFreeGrammar
from nltk.grammar import Nonterminal, Production, ContextFreeGrammar
nonterminals = 'S VP NP PP P N Name V Det'
(S, VP, NP, PP, P, N, Name, V, Det) = [Nonterminal(s)
for s in nonterminals.split()]
Expand Down
3 changes: 2 additions & 1 deletion nltk/book.py
Expand Up @@ -3,7 +3,8 @@
from nltk.corpus import gutenberg, genesis, inaugural,\
nps_chat, webtext, treebank, wordnet
from nltk.text import Text
from nltk import FreqDist, bigrams
from nltk.probability import FreqDist
from nltk.util import bigrams
from nltk.misc import babelize_shell

print "*** Introductory Examples for the NLTK Book ***"
Expand Down
7 changes: 3 additions & 4 deletions nltk/chunk/regexp.py
Expand Up @@ -9,7 +9,7 @@
import re
import types

from nltk import Tree
from nltk.tree import Tree

from nltk.chunk.api import *
from nltk.chunk.util import *
Expand Down Expand Up @@ -967,7 +967,6 @@ def _notrace_apply(self, chunkstr):
rule.apply(chunkstr)

def parse(self, chunk_struct, trace=None):
from nltk import Tree
"""
@type chunk_struct: C{Tree}
@param chunk_struct: the chunk structure to be (further) chunked
Expand Down Expand Up @@ -1224,8 +1223,8 @@ def demo_eval(chunkparser, text):
evaluation.
@type text: C{string}
"""

from nltk import chunk, Tree
from nltk import chunk
from nltk.tree import Tree

# Evaluate our chunk parser.
chunkscore = chunk.ChunkScore()
Expand Down

0 comments on commit a015b5d

Please sign in to comment.