How can i use python lower command

As you can see in the image, I put the lower command to convert the characters to lower case, but I am getting an error. I think it’s probably because of some files I need to import. Can you help me what should I do about this?

@grigrigrigon Hi, can I asked why you want to use “lower” in your use case and what is the significance for the same?

If still you want to use try use : import nltk

If the data entered by the user is capitalized, I want to convert it to lowercase. so I have to avoid a character-related problem while the program is running.

@grigrigrigon I guess rasa automatically deal the character lowercase and uppercase character.

Hello, this is a general Python question unrelated to Rasa. I’d suggest using Stack Overflow for that :slight_smile:

But, here’s your answer: Simply use .lower() with parenthesis since it is a method, not a property.

While that is true for text processing, this is regular Python code inside a Custom Action. The == operator considers if a letter is lowercase or uppercase.

Even in the case that tracker.get_slot() always returns a lowercase string, it is best practice to use .lower() (or .upper(), .title(), etc.) on both sides of the operator: x.lower() == y.lower() - Unless the case is important of course :slight_smile:

But maybe country.lower() == "america".lower() is too much :joy:

Also, your code is not scalable. Now it’s fine since you have only two elements but for best practices, you should use a dictionary, JSON file, database, or the like.

I took my liberty to modify the output a bit:

name = tracker.get_slot("name")
country = tracker.get_slot("country")

data = {
    "america": "trump",
    "france": "macron"
}

message = "{} belongs to {}".format(name.title(), country.title())

if country.lower() in data.keys():
    leader_name = data[country.lower()]
    message = message + " and leader name is {}".format(leader_name.title())

dispatcher.utter_message(text = message)

return []

Or, to stay true to your code:

name = tracker.get_slot("name")
country = tracker.get_slot("country")

data = {
    "america": "Trump",
    "france": "Macron"
}

message = "not found in database!"

if country.lower() in data.keys():
    leader_name = data[country.lower()]
    message = "{} belongs to {} and leader name is {}".format(name, country, leader_name)

dispatcher.utter_message(text = message)

return []
1 Like