Hello again, how many problems and all of them have to google.

This time, when I wrote a text RPG, I wondered if it was possible to write such a function that could split up the entered text into words and check for a match with the basic actions (prescribed in advance), if there are 2 or more words then they are executed in order (all).

Type "pronounce a monstrous spell and drink a cool potion":

  • spell
  • drink a potion

Code:

sumka=['a','b','c'] def gdl (a): for e in range(len(a)): if a[e] in ("атака", "атаковать", "ударить"): return ('вы атакуете') elif a[e] in ('выпить', 'взять', 'пить', 'глотнуть', 'хлебнуть'): return ('вы пьёте') elif a[e] in ('сумка', 'портфель', 'рюкзак', 'сумку', 'с'): return (sumka) else: return ('хммм, может по-другому или что ещё') 

How to do it, and is it even possible?

  • Well, it will make me feel like checking in a certain order - MioMelliot
  • for e in range(len(a)) and so it checks in a certain order. By the way, this line is an anti-pattern and it is better to use for e in a . - Enikeyschik
  • And change the title. This is not a seminar on philosophy. - Enikeyschik
  • one
    As I understand it, you need an "intelligent" command line parsing? By similar tokens or even grammatically, verbs, nouns ... - vp_arth
  • Um, I would understand how to make the next test easier than I think so, as it will be loud and maybe not right, but what you already explain I wrote myself - MioMelliot

1 answer 1

For me it should be done like this:

  1. Bring everything to normal form (for example, replace all verbs with infinitives, nouns with a nominative case, etc.)
  2. Split a sentence into composite actions (and, or, commas)
  3. And already to the normal form of the divided sentence to apply patterns of specific actions.

If you have a question, how to compare a pack of strings with another string, then better read about regexps.

  • And how to translate everything to normal form, I do not know such commands ( - MioMelliot
  • one
    "And this is the right question" :-) In the simplest approximation, for example, the words "Attack", "Attack", "Hit" are replaced by "Attack". How to replace the words in the text you know? - Chad
  • No, this has not happened in python - MioMelliot
  • Perhaps you can start with the documentation? pythonworld.ru/tipy-dannyx-v-python/… - Chad
  • What do you mean, I read this article but did not see anyone replacing words ( - MioMelliot