Package nltk_lite :: Module detect
[hide private]
[frames] | no frames]

Source Code for Module nltk_lite.detect

 1  # Natural Language Toolkit: Detect Features 
 2  # 
 3  # Copyright (C) 2001-2007 University of Pennsylvania 
 4  # Author: Edward Loper <edloper@gradient.cis.upenn.edu> 
 5  #         Steven Bird <sb@csse.unimelb.edu.au> (porting) 
 6  # URL: <http://nltk.sf.net> 
 7  # For license information, see LICENSE.TXT 
 8   
 9  """ 
10  Functions for detecting a token's X{features}.  Features are stored in 
11  a dictionary which maps feature names to feature values. 
12   
13  (Not yet ported from NLTK: A X{feature encoder} can then be used to 
14  translate the feature dictionary into a homogenous representation 
15  (such as a sparse boolean list), suitable for use with other 
16  processing tasks.) 
17  """ 
18   
19 -def feature(functions):
20 """ 21 Return a feature detector that applies the supplied functions 22 to each token. 23 24 @type functions: dictionary of functions 25 @param properties: one or more functions in one string argument to compute 26 the features. 27 """ 28 29 return lambda tokens: list((feature,function(tokens)) for (feature, function) in functions.items())
30 31
32 -def get_features(str):
33 """ 34 takes a string 35 returns a list of tuples (feature type, feature value) 36 """
37 38
39 -def text_feature():
40 return feature({'text': lambda t:t})
41
42 -def stem_feature(stemmer):
43 return feature({'stem': stemmer})
44 45 # def context_feature(): 46 # Meet the need that motivated BagOfContainedWordsFeatureDetector and SetOfContainedWordsFeatureDetector 47 48 49 ###################################################################### 50 ## Demo 51 ###################################################################### 52
53 -def demo():
54 from nltk_lite.corpora import brown 55 from nltk_lite import detect 56 57 detector = detect.feature({'initial': lambda t:[t[0]], 'len': lambda t:[len(t)]}) 58 59 for sent in list(brown.raw('a'))[:10]: 60 print detector(sent)
61 62 if __name__ == '__main__': demo() 63