fork download
  1. def exstractFeature1(text):
  2. return [len(text)]
  3.  
  4. class HasChar(object):
  5. def __init__(self, ch):
  6. self.ch = ch
  7. def __call__(self, text):
  8. return [0 if text.find(self.ch)<0 else 1]
  9.  
  10.  
  11. class FeatureExtractor(object):
  12. def __init__(self, features):
  13. self._features = features
  14.  
  15. def __call__(self, text):
  16. new_feature = []
  17. for f in self._features:
  18. new_feature.extend(f(text))
  19. return new_feature
  20.  
  21.  
  22. d = {
  23. 'Length': exstractFeature1,
  24. 'HasA': HasChar('A'),
  25. 'HasB': HasChar('B'),
  26. }
  27.  
  28. features = FeatureExtractor(d.values())
  29. print features('HAS') #-> [3, 1, 0]
Success #stdin #stdout 0.08s 10840KB
stdin
Standard input is empty
stdout
[3, 1, 0]