classifier.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package raku
  2. /* A classifier classifies Word tokens by their semantic type. */
  3. type Classifier interface {
  4. /* Classify the given token text. Return the type, and a bool that indicate
  5. * whether or not the token was known. */
  6. Classify(TokenText) (TokenType, bool)
  7. /* Adds a token type mapping to the classifier. Returns whether or
  8. * not the addition was acceptable. */
  9. Add(text TokenText, typ TokenType) bool
  10. }
  11. type DefaultClassifier struct {
  12. text2type map[TokenText]TokenType;
  13. }
  14. func (clf DefaultClassifier) Add(text TokenText, typ TokenType) bool {
  15. if clf.text2type == nil {
  16. clf.text2type = make(map[TokenText]TokenType)
  17. }
  18. clf.text2type[text] = typ
  19. return true
  20. }
  21. func (clf DefaultClassifier) AddMany(tokenmap map[string] TokenType) bool {
  22. result := true
  23. for text, typ := range tokenmap {
  24. tokentext := TokenText(text);
  25. call := clf.Add(tokentext, typ)
  26. result = result && call
  27. }
  28. return result
  29. }
  30. func (clf DefaultClassifier) Classify(text TokenText) (TokenType, bool) {
  31. typ, ok := clf.text2type[text]
  32. return typ, ok
  33. }
  34. type ClasssifierObject interface {
  35. Send(message string, arguments ... ClasssifierObject) (ClasssifierObject, error)
  36. }