Repository: nltk/nltk

Issue:
Framenet doctests fails
To run them, execute `nltk/test/runtests.py nltk/test/framenet.doctest` from nltk checkout:

```
> nltk/test/runtests.py nltk/test/framenet.doctest
F
======================================================================
FAIL: Doctest: framenet.doctest
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 2201, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for framenet.doctest
  File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 0

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 75, in framenet.doctest
Failed example:
    pprint(fn.frames(r'(?i)medical'))
Expected:
    [{'ID': 239, 'name': 'Medical_conditions'},
     {'ID': 256, 'name': 'Medical_specialties'},
     {'ID': 257, 'name': 'Medical_instruments'},
     {'ID': 255, 'name': 'Medical_professionals'}]
Got:
    [<frame ID=239 name=Medical_conditions>, <frame ID=256 name=Medical_specialties>, ...]
----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 95, in framenet.doctest
Failed example:
    pprint([x.name for x in f.FE])
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1289, in __run
        compileflags, 1) in test.globs
      File "<doctest framenet.doctest[11]>", line 1, in <module>
        pprint([x.name for x in f.FE])
    AttributeError: 'str' object has no attribute 'name'
----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 97, in framenet.doctest
Failed example:
    pprint(f.frameRelation)
Expected:
    [{'_type': 'framerelation', 'relatedFrame': ['Cure'], 'type': 'Uses'}]
Got:
    [[relatedFrame] ['Cure']
    [_type] framerelation
    [type] Uses
    ]
----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 112, in framenet.doctest
Failed example:
    fn.frames_by_lemma(r'(?i)a little')
Expected:
    [('Quantity', 189), ('Degree', 2001)]
Got:
    [<frame ID=189 name=Quantity>, <frame ID=2001 name=Degree>]
----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 182, in framenet.doctest
Failed example:
    len(fn.lexical_units())
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1289, in __run
        compileflags, 1) in test.globs
      File "<doctest framenet.doctest[17]>", line 1, in <module>
        len(fn.lexical_units())
    AttributeError: 'FramenetCorpusReader' object has no attribute 'lexical_units'
----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 184, in framenet.doctest
Failed example:
    pprint(fn.lexical_units(r'(?i)a little'))
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1289, in __run
        compileflags, 1) in test.globs
      File "<doctest framenet.doctest[18]>", line 1, in <module>
        pprint(fn.lexical_units(r'(?i)a little'))
    AttributeError: 'FramenetCorpusReader' object has no attribute 'lexical_units'
----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/test/framenet.doctest", line 218, in framenet.doctest
Failed example:
    pprint(fn.lu(256).lexeme)
Expected:
    {'POS': 'V', 'name': 'foresee'}
Got:
    [POS] V
    [name] foresee
    <BLANKLINE>


----------------------------------------------------------------------
Ran 1 test in 9.075s

FAILED (failures=1)
```



Discussion:
This is because the two patches to framenet.py were submitted in parallel, so the second one [overrode](https://github.com/nltk/nltk/commit/55cafbff53d167c4e6e1aaddba78da4a61e6c8c4#commitcomment-4015618) the first one.

I'm working on other changes now, so I should probably just manually incorporate, and then update, the doctests from @wooters in #474.

@nschneid thanks for noticing this -- yes, please do that, thanks.

Tests now pass in Python 2.x, but fail in 3.3 with the following exceptions:

```
======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 451, in FramenetCorpusReader

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 459, in nltk.corpus.reader.framenet.FramenetCorpusReader
Failed example:
    list(sorted(fn.lus('prejudice.n')[0].frame.frameRelations)) == list(sorted(fn.frame_relations('Partiality')))
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 1287, in __run
        compileflags, 1), test.globs)
      File "<doctest nltk.corpus.reader.framenet.FramenetCorpusReader[3]>", line 1, in <module>
        list(sorted(fn.lus('prejudice.n')[0].frame.frameRelations)) == list(sorted(fn.frame_relations('Partiality')))
    TypeError: unorderable types: AttrDict() < AttrDict()


======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader.documents
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader.documents
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1321, in documents

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1331, in nltk.corpus.reader.framenet.FramenetCorpusReader.documents
Failed example:
    pprint(set([x.corpname for x in fn.documents()]))
Expected:
    set(['ANC',
         'C-4',
         'KBEval',
         'LUCorpus-v0.3',
         'Miscellaneous',
         'NTI',
         'PropBank',
         'QA',
         'SemAnno'])
Got:
    {'ANC',
     'C-4',
     'KBEval',
     'LUCorpus-v0.3',
     'Miscellaneous',
     'NTI',
     'PropBank',
     'QA',
     'SemAnno'}


======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader.fe_relations
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader.fe_relations
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1477, in fe_relations

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1487, in nltk.corpus.reader.framenet.FramenetCorpusReader.fe_relations
Failed example:
    PrettyDict(ferels[0], breakLines=True)
Expected:
    {'ID': 2,
     '_type': 'ferelation',
     'frameRelation': <Parent=Process -- Inheritance -> Child=Precipitation>,
     'subFE': <fe ID=2737 name=Duration>,
     'subFEName': 'Duration',
     'subFrame': <frame ID=314 name=Precipitation>,
     'subID': 2737,
     'supID': 2026,
     'superFE': <fe ID=2026 name=Duration>,
     'superFEName': 'Duration',
     'superFrame': <frame ID=233 name=Process>,
     'type': <framerelationtype ID=1 name=Inheritance>}
Got:
    {'ID': 16384,
     '_type': 'ferelation',
     'frameRelation': <Parent=Cause_to_start -- Using -> Child=Launch_process>,
     'subFE': <fe ID=12747 name=Epistemic_stance>,
     'subFEName': 'Epistemic_stance',
     'subFrame': <frame ID=2070 name=Launch_process>,
     'subID': 12747,
     'supID': 12693,
     'superFE': <fe ID=12693 name=Epistemic_stance>,
     'superFEName': 'Epistemic_stance',
     'superFrame': <frame ID=381 name=Cause_to_start>,
     'type': <framerelationtype ID=3 name=Using>}


======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader.frame_relation_types
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader.frame_relation_types
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1373, in frame_relation_types

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1379, in nltk.corpus.reader.framenet.FramenetCorpusReader.frame_relation_types
Failed example:
    isinstance(frts, list)
Expected:
    True
Got:
    False
----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1383, in nltk.corpus.reader.framenet.FramenetCorpusReader.frame_relation_types
Failed example:
    PrettyDict(frts[0], breakLines=True)
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 1287, in __run
        compileflags, 1), test.globs)
      File "<doctest nltk.corpus.reader.framenet.FramenetCorpusReader.frame_relation_types[4]>", line 1, in <module>
        PrettyDict(frts[0], breakLines=True)
    TypeError: 'dict_values' object does not support indexing


======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader.lu
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader.lu
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 865, in lu

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 882, in nltk.corpus.reader.framenet.FramenetCorpusReader.lu
Failed example:
    pprint(map(PrettyDict, fn.lu(256).lexemes))
Expected:
    [{'POS': 'V', 'breakBefore': 'false', 'headword': 'false', 'name': 'foresee', 'order': 1}]
Got:
    <map object at 0x113bc1790>


======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader.lu_basic
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader.lu_basic
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 838, in lu_basic

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 845, in nltk.corpus.reader.framenet.FramenetCorpusReader.lu_basic
Failed example:
    PrettyDict(fn.lu_basic(256), breakLines=True)
Expected:
    {'ID': 256,
     'POS': 'V',
     '_type': 'lu',
     'definition': 'COD: be aware of beforehand; predict.',
     'frame': <frame ID=26 name=Expectation>,
     'lemmaID': 15082,
     'lexemes': [{'POS': 'V', 'headword': 'false', 'breakBefore': 'false', 'order': 1, 'name': 'foresee'}],
     'name': 'foresee.v',
     'semTypes': [],
     'sentenceCount': {'total': 227, 'annotated': 44},
     'status': 'FN1_Sent'}
Got:
    {'ID': 256,
     'POS': 'V',
     '_type': 'lu',
     'definition': 'COD: be aware of beforehand; predict.',
     'frame': <frame ID=26 name=Expectation>,
     'lemmaID': 15082,
     'lexemes': [{'order': 1, 'breakBefore': 'false', 'name': 'foresee', 'headword': 'false', 'POS': 'V'}],
     'name': 'foresee.v',
     'semTypes': [],
     'sentenceCount': {'annotated': 44, 'total': 227},
     'status': 'FN1_Sent'}


======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader.semtypes
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader.semtypes
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1508, in semtypes

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 1516, in nltk.corpus.reader.framenet.FramenetCorpusReader.semtypes
Failed example:
    stypes[0].keys()
Expected:
    ['definition', '_type', 'name', 'rootType', 'abbrev', 'superType', 'subTypes', 'ID']
Got:
    dict_keys(['name', 'abbrev', '_type', 'ID', 'rootType', 'superType', 'subTypes', 'definition'])
```

Also fails in 2.7: `import unicode_literals` means most string outputs now have a `u` prefix

They don't fail under 2.7 if executed using NLTK's runtests.py script because it handles this Python 2/3 case - u'string' and 'string' are considered the same by NLTK test runner when encountered in doctest output.

```
> nltk/test/runtests.py nltk/test/framenet.doctest
.
----------------------------------------------------------------------
Ran 1 test in 7.351s

OK
```

Oh good. I was doing `python -m doctest nltk/corpus/reader/framenet.py`.

There are some testing docs here: https://github.com/nltk/nltk/blob/master/web/dev/local_testing.rst - they are not perfect, and also they are not very visible, but they explain some gotchas of NLTK test runner. 

Thanks @kmike! The pull request hopefully addresses the 3.3 doctest failures.

There is one remaining issues in Python 3.3:

```
py33 runtests: commands[1] | python runtests.py nltk.corpus.reader.framenet
............F....
======================================================================
FAIL: Doctest: nltk.corpus.reader.framenet.FramenetCorpusReader.lu_basic
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/doctest.py", line 2154, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for nltk.corpus.reader.framenet.FramenetCorpusReader.lu_basic
  File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 838, in lu_basic

----------------------------------------------------------------------
File "/Users/kmike/svn/nltk/nltk/corpus/reader/framenet.py", line 845, in nltk.corpus.reader.framenet.FramenetCorpusReader.lu_basic
Failed example:
    PrettyDict(fn.lu_basic(256), breakLines=True)
Expected:
    {'ID': 256,
     'POS': 'V',
     '_type': 'lu',
     'definition': 'COD: be aware of beforehand; predict.',
     'frame': <frame ID=26 name=Expectation>,
     'lemmaID': 15082,
     'lexemes': [{'POS': 'V', 'headword': 'false', 'breakBefore': 'false', 'order': 1, 'name': 'foresee'}],
     'name': 'foresee.v',
     'semTypes': [],
     'sentenceCount': {'total': 227, 'annotated': 44},
     'status': 'FN1_Sent'}
Got:
    {'ID': 256,
     'POS': 'V',
     '_type': 'lu',
     'definition': 'COD: be aware of beforehand; predict.',
     'frame': <frame ID=26 name=Expectation>,
     'lemmaID': 15082,
     'lexemes': [{'name': 'foresee', 'headword': 'false', 'breakBefore': 'false', 'order': 1, 'POS': 'V'}],
     'name': 'foresee.v',
     'semTypes': [],
     'sentenceCount': {'annotated': 44, 'total': 227},
     'status': 'FN1_Sent'}

```

And sorry for confusion - I suggested you to run `framenet.doctest` previously, while you were interested in `nltk.corpus.reader.framenet` doctests.


Resolving patch:
diff --git a/nltk/corpus/reader/framenet.py b/nltk/corpus/reader/framenet.py
--- a/nltk/corpus/reader/framenet.py
+++ b/nltk/corpus/reader/framenet.py
@@ -304,7 +304,7 @@ def _short_repr(self):
             except KeyError:    # no ID--e.g., for _type=lusubcorpus
                 return "<{0} name={1}>".format(self['_type'], self['name'])
         else:
-            return dict.__repr__(self)
+            return self.__repr__()
         
     def _str(self):
         outstr = ""
@@ -849,10 +849,10 @@ def lu_basic(self, fn_luid):
          'definition': 'COD: be aware of beforehand; predict.',
          'frame': <frame ID=26 name=Expectation>,
          'lemmaID': 15082,
-         'lexemes': [{'POS': 'V', 'headword': 'false', 'breakBefore': 'false', 'order': 1, 'name': 'foresee'}],
+         'lexemes': [{'POS': 'V', 'breakBefore': 'false', 'headword': 'false', 'name': 'foresee', 'order': 1}],
          'name': 'foresee.v',
          'semTypes': [],
-         'sentenceCount': {'total': 227, 'annotated': 44},
+         'sentenceCount': {'annotated': 44, 'total': 227},
          'status': 'FN1_Sent'}
 
         :param fn_luid: The id number of the desired LU
@@ -1535,7 +1535,7 @@ def _load_xml_attributes(self, d, elt):
         :rtype: dict
         """
 
-        d = AttrDict(d)
+        d = type(d)(d)
 
         try:
             attr_dict = elt.attrib
@@ -1807,11 +1807,11 @@ def _handle_framelexunit_elt(self, elt):
                 luinfo['definition'] = self._strip_tags(sub.text)
             elif sub.tag.endswith('sentenceCount'):
                 luinfo['sentenceCount'] = self._load_xml_attributes(
-                    AttrDict(), sub)
+                    PrettyDict(), sub)
             elif sub.tag.endswith('lexeme'):
-                luinfo['lexemes'].append(self._load_xml_attributes(AttrDict(), sub))
+                luinfo['lexemes'].append(self._load_xml_attributes(PrettyDict(), sub))
             elif sub.tag.endswith('semType'):
-                semtypeinfo = self._load_xml_attributes(AttrDict(), sub)
+                semtypeinfo = self._load_xml_attributes(PrettyDict(), sub)
                 luinfo['semTypes'].append(self.semtype(semtypeinfo.ID))
 
         return luinfo
@@ -1844,7 +1844,7 @@ def _handle_lexunit_elt(self, elt, ignorekeys):
                 if sc is not None:
                     luinfo['subCorpus'].append(sc)
             elif sub.tag.endswith('lexeme') and 'lexeme' not in ignorekeys:
-                luinfo['lexemes'].append(self._load_xml_attributes(AttrDict(), sub))
+                luinfo['lexemes'].append(self._load_xml_attributes(PrettyDict(), sub))
             elif sub.tag.endswith('semType') and 'semType' not in ignorekeys:
                 semtypeinfo = self._load_xml_attributes(AttrDict(), sub)
                 luinfo['semTypes'].append(self.semtype(semtypeinfo.ID))


Test patch: