Obtaining a Word Within A Sentance - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html) +--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html) +--- Thread: Obtaining a Word Within A Sentance (/thread-8478.html) Pages:
1
2
|
Obtaining a Word Within A Sentance - WaffleMan - 12-01-2005 Hey guys. Long time no see. Anyway, here's the deal: I'm currently trying my hand at programming a Chatroom simulator (don't ask; I was bored), and I'm having a little trouble with the basics. You see, I'm storing the responses to and from the "characters" within a text file that looks like this: Quote:@HELLO HI HOWDY SUP HEY I obtain these responses by using this code: Code: Sub GetResponse(user as STRING, response AS STRING) If the user types something that isn't in the conversation file, I make the characters say "huh?" However, that is not the problem. I want the characters to respond to someone in a certain way if a certain keyword is within that sentance, i.e. if someone says "hola amigos sup" the other characters would respond with "how yah doin'" or "hello". The catch: I don't know how to do this. The code only makes the characters respond as appropriate if the reply is one word and is in the conversation file (the characters respond if the user types "hi", but not if the user types "hi gang what's going on?"). I've tried using FOR-NEXTs within each other, letter counting (letter$ = letter$ + MID$(string$, loop var, 1)), and MID$ itself, but I can't get anything to work. Can someone guide me in the right direction? :-? PS: I'm making this in FreeBasic, but it seems to me the code I currently have would look no different than if I wrote it into QBASIC. I also didn't post all the code because I thought that would've been a waste of time. Obtaining a Word Within A Sentance - Agamemnus - 12-01-2005 This is the general outline: 1) Alphabetize the search words. 2) Parse the sentence into words and: For each word (definition: space or start/end of line on either side, minus . or " or ' etc.) search for the word in the word list.. using a neat function I made ... ...which is somewhere in the old qbasicnews logs.. 3) Convert each word into its index value.. then search for the index value set in your defined index value searches. Ex: Word string is: 91 2104 344 4567 You have a search value set of the same values, say set 84: (91 2104 344 4567) You then respond with set 244 to set 84, ex: (466 23 23 12) Obtaining a Word Within A Sentance - WaffleMan - 12-01-2005 I'm sorry, but I don't understand what you mean by "converting the words string into an index value" and then searching for said value. Obtaining a Word Within A Sentance - Agamemnus - 12-01-2005 Yes I feared you wouldn't.. Did you understand everything else? :lol: Word string example: A tall green tree grows here. A tall green tree grows here You have an alphabetized array of words. A is 1 on the array list. Its index is 1. Tall is say 573, green is maybe 124, tree is 590, grows is 129, here is 110. Thus your values are: 1, 573, 124, 590, 129, 110. Let's say you have a response (sentence) to that statement and the response code is 346, 23, 1, 311, 93, 116. Then you should match up your input (1, 573, 124, 590, 129, 110) to the output (346, 23, 1, 311, 93, 116) In qbasic or freebasic you may use the following structure: inputquestion%(1 to questionamount) outputresponse%(1 to questionamount) inputquestionLength%(1 to questionamount) outputresponseLength%(1 to questionamount) inputquestionlist%(1 to questionamount*5) outputresponselist%(1 to questionamount*5) Inputquestion% and outputresponse%, and the Length% values indicate the starting index number and length of the sentence in the inputquestionlist% and outputresponselist% arrays. Code: Example: EDIT: For now, just try making something that separates the string into words. Obtaining a Word Within A Sentance - WaffleMan - 12-02-2005 Quote:EDIT:Done: Code: CLS I pretty much figured that wasn't enough when I popped it into my program and it didn't work right. Anyway, what next? Obtaining a Word Within A Sentance - Agamemnus - 12-03-2005 For simplicity, that's fine, but for a user: You can't assume that each message has just one sentence, not that each word is separated by just one space. Now you alphabetize the array.. Obtaining a Word Within A Sentance - WaffleMan - 12-03-2005 Well, I'm pretty much still a novice at programming, so the only way I can think to seperate words with no space between them is to make a database of common words than search through them. Anyway, here's the new code. I took the sorting code from a great QBASIC tutorial book and altered it (aka "changed the variables") to work with my program. I also moved my punctuation code so if the user types something like "o.ops! a mist.a?ke" the program will still take it as a valid answer, and will take into account the contents of two sentances. Code: CLS Now I hope you're gonna teach me the hard stuff. Obtaining a Word Within A Sentance - Agamemnus - 12-07-2005 What I meant is if you have two spaces, it will add two instead of 1. And, that is a really newb searching algorithm. You can try quicksort, which I have a link for here: http://forum.qbasicnews.com/viewtopic.php?t=4545&highlight=quicksort Let's cut to the chase though. Add this sub in your code, and call it like so: Code: qsort.string.lowstart words$, UBOUND(words$) 'assuming you start from 1.. Obtaining a Word Within A Sentance - Agamemnus - 12-07-2005 Now, step 2 consists of you thinking up how exactly you want to do this. Do you want to have a response to each "question", word for word? Several responses picked randomly? Maybe you want to somehow weigh each word, giving a number, and then pick a response that has the closest weight? Obtaining a Word Within A Sentance - WaffleMan - 12-07-2005 Well, I just want the response based on the first key word within a sentance. For example, say the user says "Hey everyone. I like pie." Even though "pie" and "hey" are two different keywords, the characters will only respond to "hey," and then say a random response based on the list of responses under the "hey" keyword. Also, I think I have another idea on how to do it. I'll try that out, then have another look at your code. Thanks for the help! |