Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to store strings in Python?

I am building a conversation bot in Python. While I would like to generate as much text as possible from scratch, I still need a way to catalog and store a bunch of dialog fragment strings. Ideally I would like to contain some sort of hierarchy/classifications among the strings. For example:

Greetings:

"Oh, nice to meet you {0}" "My name is Bob, how about you?"

Flirtation:

"Stop it" "I'm blushing" "How flattering"

etc...

While I could store these in a database, it would be nice to have different format that people could edit easily by hand. CSV? JSON? Is there any precedent for stuff like this?

like image 866
samfr Avatar asked Dec 20 '22 17:12

samfr


1 Answers

That depends on how do you want to use it. If the strings are only meant to be used by Python you should consider storing them in their very own .py file. Yes, a module, but it is also a simple text file which happens that can be interpreted by Python :)

A lot of projects use .py files as configuration files (Django) and importing its contents is very easy since you only have to do import answer_strings and you'll already got them in variables or classes.

You can for example do this:

#bot answers module

greetings = ["hello {0}", "what's up {0}"]
farewells = ["see you soon {0}", "nos vemos {0}"]
...

And can return equivalent answers randomly, etc.

On the other side, if these are meant to be also read by Javascript, Java, node.js or whatever technology other than Python then a more universal format should be use, JSON, XML, YAML, you name it.

I think this is better in a text file (a project's resource) than in a database since that way (as you mentioned it) is more customizable. And I would also recommend to use a format that have semantics included. A CSV file is IMHO very cold, just a bunch of data dumped to a file. With XML, JSON, etc you can group your data in categories like "Greetings", "Farewells", etc, etc.

Not to forget that since you have several options, it would be very good to build your code in a modular way and decoupled. Thus, if you made a decision and in the future need to change, it would be as seamless as possible to accomplish.

Hope this helps!

like image 143
Paulo Bu Avatar answered Dec 24 '22 00:12

Paulo Bu