Package nltk :: Module yamltags
[hide private]
[frames] | no frames]

Source Code for Module nltk.yamltags

 1  import yaml 
 2   
 3  """ 
 4  Register YAML tags in the NLTK namespace with the YAML loader, by telling it 
 5  what module and class to look for. 
 6   
 7  NLTK uses simple '!' tags to mark the types of objects, but the fully-qualified 
 8  "tag:nltk.sourceforge.net,2007:" prefix is also accepted in case anyone ends up 
 9  using it. 
10  """ 
11   
12 -def custom_import(name):
13 components = name.split('.') 14 module_path = '.'.join(components[:-1]) 15 mod = __import__(module_path) 16 for comp in components[1:]: 17 mod = getattr(mod, comp) 18 return mod
19
20 -def metaloader(classpath):
21 def loader(*args, **kwds): 22 classref = custom_import(classpath) 23 return classref.from_yaml(*args, **kwds)
24 return loader 25
26 -def register_tag(tag, classpath):
27 yaml.add_constructor(u'!'+tag, metaloader(classpath)) 28 yaml.add_constructor(u'tag:nltk.sourceforge.net,2007:'+tag, 29 metaloader(classpath))
30 31 register_tag(u'tag.Unigram', 'nltk.tag.unigram.Unigram') 32 register_tag(u'tag.Brill', 'nltk.tag.brill.Brill') 33 34 __all__ = ['custom_import', 'metaloader', 'register_tag'] 35