Repository: nltk/nltk

Issue:
nltk.parse.stanford.StanfordParser doctest is broken
There are a few syntax errors - too many parenthesis and invalid quoting. See here: https://github.com/nltk/nltk/blob/develop/nltk/parse/stanford.py#L25

See also https://github.com/nltk/nltk/issues/576 - the doctest should be skipped if jar is not available.



Discussion:

Resolving patch:
diff --git a/nltk/internals.py b/nltk/internals.py
--- a/nltk/internals.py
+++ b/nltk/internals.py
@@ -578,7 +578,7 @@ def find_jar_iter(name_pattern, path_to_jar=None, env_vars=(),
     if path_to_jar is not None:
         if os.path.isfile(path_to_jar):
             yield path_to_jar
-        raise ValueError('Could not find %s jar file at %s' %
+        raise LookupError('Could not find %s jar file at %s' %
                          (name_pattern, path_to_jar))
 
     # Check environment variables
diff --git a/nltk/parse/stanford.py b/nltk/parse/stanford.py
--- a/nltk/parse/stanford.py
+++ b/nltk/parse/stanford.py
@@ -22,28 +22,37 @@
 
 _stanford_url = 'http://nlp.stanford.edu/software/lex-parser.shtml'
 
+def setup_module(module):
+    from nose import SkipTest
+
+    try:
+        StanfordParser(
+            model_path='edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz'
+        )
+    except LookupError:
+        raise SkipTest('doctests from nltk.parse.stanford are skipped because the stanford parser jar doesn\\\'t exist')
+
 class StanfordParser(ParserI):
     '''
     Interface to the Stanford Parser
 
     >>> parser=StanfordParser(
-    ...     path_to_jar='../third/stanford-parser/stanford-parser.jar',
     ...     model_path='edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz'
     ... )
     >>> parser.raw_batch_parse((
     ...     'the quick brown fox jumps over the lazy dog',
     ...     'the quick grey wolf jumps over the lazy fox'
-    ... )))
+    ... ))
     [Tree('ROOT', [Tree('NP', [Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['quick']), Tree('JJ', ['brown']),
     Tree('NN', ['fox'])]), Tree('NP', [Tree('NP', [Tree('NNS', ['jumps'])]), Tree('PP', [Tree('IN', ['over']),
-    Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['lazy']), Tree('NN', ['dog'])])])])])]), Tree('ROOT', [Tree(
-    'NP', [Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['quick']), Tree('JJ', ['grey']), Tree('NN', ['wolf'])]),
-    Tree('NP', [Tree('NP', [Tree('NNS', ['jumps'])]), Tree('PP', [Tree('IN', ['over']), Tree('NP',
-    [Tree('DT', ['the']), Tree('JJ', ['lazy']), Tree('NN', ['fox'])])])])])])]
+    Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['lazy']), Tree('NN', ['dog'])])])])])]), Tree('ROOT', [Tree('NP',
+    [Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['quick']), Tree('JJ', ['grey']), Tree('NN', ['wolf'])]), Tree('NP',
+    [Tree('NP', [Tree('NNS', ['jumps'])]), Tree('PP', [Tree('IN', ['over']), Tree('NP', [Tree('DT', ['the']),
+    Tree('JJ', ['lazy']), Tree('NN', ['fox'])])])])])])]
 
     >>> parser.batch_parse((
-    ...     'I \'m a dog'.split(),
-    ...     'This is my friends \' cat ( the tabby )'.split(),
+    ...     'I \\\'m a dog'.split(),
+    ...     'This is my friends \\\' cat ( the tabby )'.split(),
     ... ))
     [Tree('ROOT', [Tree('S', [Tree('NP', [Tree('PRP', ['I'])]), Tree('VP', [Tree('VBP', ["'m"]),
     Tree('NP', [Tree('DT', ['a']), Tree('NN', ['dog'])])])])]), Tree('ROOT', [Tree('S', [Tree('NP',
@@ -77,17 +86,20 @@ def __init__(self, path_to_jar=None, path_to_models_jar=None,
                  encoding='UTF-8', verbose=False, java_options='-mx1000m'):
 
         self._stanford_jar = find_jar(
-                self._JAR, path_to_jar,
-                env_vars=('STANFORD_PARSER',),
-                searchpath=(), url=_stanford_url,
-                verbose=verbose)
+            self._JAR, path_to_jar,
+            env_vars=('STANFORD_PARSER',),
+            searchpath=(), url=_stanford_url,
+            verbose=verbose
+        )
 
         # find the most recent model
-        self._model_jar=max(find_jar_iter(
+        self._model_jar=max(
+            find_jar_iter(
                 self._MODEL_JAR_PATTERN, path_to_models_jar,
                 env_vars=('STANFORD_MODELS',),
                 searchpath=(), url=_stanford_url,
-                verbose=verbose, is_regex=True),
+                verbose=verbose, is_regex=True
+            ),
             key=lambda model_name: re.match(self._MODEL_JAR_PATTERN, model_name)
         )
 
diff --git a/nltk/tokenize/stanford.py b/nltk/tokenize/stanford.py
--- a/nltk/tokenize/stanford.py
+++ b/nltk/tokenize/stanford.py
@@ -38,10 +38,11 @@ class StanfordTokenizer(TokenizerI):
 
     def __init__(self, path_to_jar=None, encoding='UTF-8', options=None, verbose=False, java_options='-mx1000m'):
         self._stanford_jar = find_jar(
-                self._JAR, path_to_jar,
-                env_vars=('STANFORD_POSTAGGER',),
-                searchpath=(), url=_stanford_url,
-                verbose=verbose)
+            self._JAR, path_to_jar,
+            env_vars=('STANFORD_POSTAGGER',),
+            searchpath=(), url=_stanford_url,
+            verbose=verbose
+        )
 
         self._encoding = encoding
         self.java_options = java_options


Test patch: