My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import yaml

"""
Register YAML tags in the NLTK namespace with the YAML loader, by telling it
what module and class to look for.

NLTK uses simple '!' tags to mark the types of objects, but the fully-qualified
"tag:nltk.org,2011:" prefix is also accepted in case anyone ends up
using it.
"""

def custom_import(name):
components = name.split('.')
module_path = '.'.join(components[:-1])
mod = __import__(module_path)
for comp in components[1:]:
mod = getattr(mod, comp)
return mod

def metaloader(classpath):
def loader(*args, **kwds):
classref = custom_import(classpath)
return classref.from_yaml(*args, **kwds)
return loader

def register_tag(tag, classpath):
yaml.add_constructor(u'!'+tag, metaloader(classpath))
yaml.add_constructor(u'tag:nltk.org,2011:'+tag,
metaloader(classpath))

register_tag(u'tag.Unigram', 'nltk.tag.unigram.Unigram')
register_tag(u'tag.Brill', 'nltk.tag.brill.Brill')

__all__ = ['custom_import', 'metaloader', 'register_tag']

Change log

r8732 by StevenBird1 on Mar 10, 2011   Diff
Updated Yaml tag prefix.
Go to: 
Sign in to write a code review

Older revisions

r5666 by edloper on Jan 22, 2008   Diff
Rewrapped lines to be under 80 chars
wide
r4797 by stevenbird on Jun 29, 2007   Diff
* added __all__ to top level modules
* changed imports in nltk.__init__ to
include __all__ from top level modules
* removed antiquated Sets.set() from
evaluate.py
...
r4647 by stevenbird on Jun 10, 2007   Diff

*****************************
  Changed nltk_lite to nltk
*****************************
...
All revisions of this file

File info

Size: 1040 bytes, 34 lines
Powered by Google Project Hosting