write a recognizer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Klaus Neuner

    write a recognizer

    Hello,

    I want to write a class Recognizer, like so:

    class Recognizer(obje ct):

    def is_of_category_ 1(self, token):
    if token == 1:
    return "1"
    else:
    return False

    def is_of_category_ 2(self, token):
    if token == 2:
    return "2"
    else:
    return False

    def recognize(self, token):
    for fun in <?>:
    result = apply(fun, token)
    if result:
    return result
    return False

    What do I have to write instead of <?>?
    Or: How should I design the recognizer, if the above design is not good?

    Klaus
  • Peter Otten

    #2
    Re: write a recognizer

    Klaus Neuner wrote:
    [color=blue]
    > Hello,
    >
    > I want to write a class Recognizer, like so:
    >
    > class Recognizer(obje ct):
    >
    > def is_of_category_ 1(self, token):
    > if token == 1:
    > return "1"
    > else:
    > return False
    >
    > def is_of_category_ 2(self, token):
    > if token == 2:
    > return "2"
    > else:
    > return False
    >
    > def recognize(self, token):
    > for fun in <?>:
    > result = apply(fun, token)
    > if result:
    > return result
    > return False
    >
    > What do I have to write instead of <?>?
    > Or: How should I design the recognizer, if the above design is not good?
    >
    > Klaus[/color]

    The following assumes that all category checker method names start with a
    common prefix. Those are automatically extracted in the __init__() method.
    If you are interested in this technique, I stole it from cmd.py in the
    library. IIRC, the implementation is more complete as it also inspects the
    base classes.

    class Recognizer(obje ct):
    def __init__(self):
    r = self.recognizer s = []
    for n in dir(self.__clas s__):
    if n.startswith("i s_"):
    r.append(getatt r(self, n))

    def is_of_category_ 1(self, token):
    if token == 1:
    return "1"

    def is_of_category_ 2(self, token):
    if token == 2:
    return "2"

    def recognize(self, token):
    # would also work:
    #for fun in [self.is_of_cate gory_1, self.is_of_cate gory_2]:

    for fun in self.recognizer s:
    result = fun(token)
    if result:
    return result
    return False

    if __name__ == "__main__":
    r = Recognizer()
    for t in "12341":
    print r.recognize(int (t)),
    print

    Peter

    Comment

    • Chunming Luo

      #3
      Re: write a recognizer

      Peter Otten wrote:
      [color=blue]
      >Klaus Neuner wrote:
      >
      >
      >[color=green]
      >>Hello,
      >>
      >>I want to write a class Recognizer, like so:
      >>
      >>class Recognizer(obje ct):
      >>
      >> def is_of_category_ 1(self, token):
      >> if token == 1:
      >> return "1"
      >> else:
      >> return False
      >>
      >> def is_of_category_ 2(self, token):
      >> if token == 2:
      >> return "2"
      >> else:
      >> return False
      >>
      >> def recognize(self, token):
      >> for fun in <?>:
      >> result = apply(fun, token)
      >> if result:
      >> return result
      >> return False
      >>
      >>What do I have to write instead of <?>?
      >>Or: How should I design the recognizer, if the above design is not good?
      >>
      >>Klaus
      >>
      >>[/color]
      >
      >The following assumes that all category checker method names start with a
      >common prefix. Those are automatically extracted in the __init__() method.
      >If you are interested in this technique, I stole it from cmd.py in the
      >library. IIRC, the implementation is more complete as it also inspects the
      >base classes.
      >
      >class Recognizer(obje ct):
      > def __init__(self):
      > r = self.recognizer s = []
      > for n in dir(self.__clas s__):
      > if n.startswith("i s_"):
      > r.append(getatt r(self, n))
      >
      > def is_of_category_ 1(self, token):
      > if token == 1:
      > return "1"
      >
      > def is_of_category_ 2(self, token):
      > if token == 2:
      > return "2"
      >
      > def recognize(self, token):
      > # would also work:
      > #for fun in [self.is_of_cate gory_1, self.is_of_cate gory_2]:
      >
      > for fun in self.recognizer s:
      > result = fun(token)
      > if result:
      > return result
      > return False
      >
      >if __name__ == "__main__":
      > r = Recognizer()
      > for t in "12341":
      > print r.recognize(int (t)),
      > print
      >
      >Peter
      >[/color]

      Here is another solution:

      def recongnize(self , token):
      for item in self.__class__. __dict__.keys() :
      method = self.__class__. __dict__[item]
      if callable(method ) and method !=
      self.__class__. __dict__["recongnize "]:
      result = method(self, token)
      if result:
      return result
      return false

      -Chunming


      Comment

      • max khesin

        #4
        Re: write a recognizer

        This is cool. Curious (my py object recall is a bit stale): would this
        solution work for a class that derives from Recognizer (and implements
        an 'is_' method)?

        thanks,
        max


        Peter Otten wrote:
        [color=blue]
        > Klaus Neuner wrote:
        >
        >[color=green]
        >>Hello,
        >>
        >>I want to write a class Recognizer, like so:
        >>
        >>class Recognizer(obje ct):
        >>
        >> def is_of_category_ 1(self, token):
        >> if token == 1:
        >> return "1"
        >> else:
        >> return False
        >>
        >> def is_of_category_ 2(self, token):
        >> if token == 2:
        >> return "2"
        >> else:
        >> return False
        >>
        >> def recognize(self, token):
        >> for fun in <?>:
        >> result = apply(fun, token)
        >> if result:
        >> return result
        >> return False
        >>
        >>What do I have to write instead of <?>?
        >>Or: How should I design the recognizer, if the above design is not good?
        >>
        >>Klaus[/color]
        >
        >
        > The following assumes that all category checker method names start with a
        > common prefix. Those are automatically extracted in the __init__() method.
        > If you are interested in this technique, I stole it from cmd.py in the
        > library. IIRC, the implementation is more complete as it also inspects the
        > base classes.
        >
        > class Recognizer(obje ct):
        > def __init__(self):
        > r = self.recognizer s = []
        > for n in dir(self.__clas s__):
        > if n.startswith("i s_"):
        > r.append(getatt r(self, n))
        >
        > def is_of_category_ 1(self, token):
        > if token == 1:
        > return "1"
        >
        > def is_of_category_ 2(self, token):
        > if token == 2:
        > return "2"
        >
        > def recognize(self, token):
        > # would also work:
        > #for fun in [self.is_of_cate gory_1, self.is_of_cate gory_2]:
        >
        > for fun in self.recognizer s:
        > result = fun(token)
        > if result:
        > return result
        > return False
        >
        > if __name__ == "__main__":
        > r = Recognizer()
        > for t in "12341":
        > print r.recognize(int (t)),
        > print
        >
        > Peter[/color]

        Comment

        • Peter Otten

          #5
          Re: write a recognizer

          max khesin wrote:
          [color=blue]
          > This is cool. Curious (my py object recall is a bit stale): would this
          > solution work for a class that derives from Recognizer (and implements
          > an 'is_' method)?[/color]

          No, but you can use the following instead of dir(...) in the for loop of
          __init__():

          (copied from cmd.py in the libarary)

          def get_names(self) :
          # Inheritance says we have to look in class and
          # base classes; order is not important.
          names = []
          classes = [self.__class__]
          while classes:
          aclass = classes.pop(0)
          if aclass.__bases_ _:
          classes = classes + list(aclass.__b ases__)
          names = names + dir(aclass)
          return names

          Peter

          Comment

          • Klaus Neuner

            #6
            Re: write a recognizer

            Thanks to all who participated in this thread.

            Comment

            Working...