Keyword-based text parser, anyone?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
fade
Prole
Posts: 9
Joined: Tue Jan 26, 2016 9:47 pm

Keyword-based text parser, anyone?

Post by fade »

Hey, guys! Me again! I'm not yet at this stage in developing my first love game, but still, it's better to know beforehand. What are your thoughts on implementing a keyword-based text parser as a way to talk to NPCs in a love2d game? It's like this:
You activate an NPC. A special NPC screen pops up. The NPC says hello. But you reply not by clicking on dialog options or stuff like that, but by directly typing in your greeting. After that you just use the sucker like a chat bot, which recognizes certain words and reacts to them. For example you might type in "Let us trade", and a shop interface pops up. Or, if the NPC is not a merchant, he tells you so and directs you to the closest store. But how do you get love to recognize text? I've used this code:

Code: Select all

local utf8 = require("utf8")
function love.textinput(t)
  if npcchat == "on" then
  yourphrase = yourphrase .. t
  
function love.update(dt)
      if kb.isDown('backspace') and npcchat == true then
        local byteoffset = utf8.offset(yourphrase, -1)
        if byteoffset and npcchat == true then
            yourphrase = string.sub(yourphrase, 1, byteoffset - 1)
        end
    end
    
    function love.keypressed(key)
  if kb.isDown("return") and npcchat == true then
    npcchat = "off"
    InterpretingWhatYouTyped = "on"
  end
  
But after this part I'm stuck. It seems that the text you input this way is seen as a collection of utf8 symbols. I think love knows how many there is, for example 5 in a word "hello" typed into the line, but it doesn't know which letters are used. But I need it to go through the typed word or phrase, and compare it to words in a "dictionary" to react. For example: I type in "Hey, man, have you seen any dragons lately?". I press enter. The game finds I used the word "dragon", and gives me a pre-written reply: "They say, dragons live west of here, "..player.name..""!" How do I do it, really? And can I really do it? I learned that tons of people are doing their best at making a love rpg, so I think this topic is pretty relevant, and I'm not the only one to benefit from a nice answer! Thanks guys! Be seeing ya'll!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Keyword-based text parser, anyone?

Post by Robin »

I see two ways to go about this:

1. Some kind of parsing, which is hard to do and very brittle. It's possible to do, but... I don't recommend it at this stage. Especially if you want the NPC to talk back.
2. Keep a word list, like you talked about, and use yourphrase:find(word) for every word in the list. You probably want to check for doubles, because if you say to an NPS "I want to trade a dragon", does the shop interface pop up or does he start talking to you about dragons?
Help us help you: attach a .love.
fade
Prole
Posts: 9
Joined: Tue Jan 26, 2016 9:47 pm

Re: Keyword-based text parser, anyone?

Post by fade »

Robin wrote:I see two ways to go about this:

1. Some kind of parsing, which is hard to do and very brittle. It's possible to do, but... I don't recommend it at this stage. Especially if you want the NPC to talk back.
2. Keep a word list, like you talked about, and use yourphrase:find(word) for every word in the list. You probably want to check for doubles, because if you say to an NPS "I want to trade a dragon", does the shop interface pop up or does he start talking to you about dragons?
Wow! find(word) is a pretty useful tool! I think it's possible to work with it for this goal. And the doubles problem I thought about, the best idea I have now is decision based on the alphabetical order. "D-ragon" goes before "t-rade", so the guy says his phrase on the dragon, and just that. Not the best idea, but it could save from some mess.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Keyword-based text parser, anyone?

Post by Robin »

That's possible, but the player does not know the word list, and so the output could be confusing. Although you could easily change that later, so maybe first make the simplest thing that works, and if the players start complaining, you can work on doing something fancier. ;)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests