简体   繁体   中英

nltk .lhs() issues with comparing strings

When assigning string = i.lhs() its value, then later comparing with print (var == 'P') returns false when var is 'P' .

 for i in grammar.productions():
    var = i.lhs()
    P = "P"
    if (not i.is_nonlexical()):
        print var
        print (var == P)

it returns false on anything. I don't understand why it returns false on everything. Any tip?

You can't compare lhs()-objects with strings, but you can get the object's string representation with .lhs().__str__() , and compare that with another string, like so:

for i in grammar.productions() :
   var = i.lhs()
   P = "P"
   if (not i.is_nonlexical()) :
       print var
       print (var.__str__() == P)

Also, you can use is_lexical() instead of asking if it's not nonlexical

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM