How to implement simple "stop-word/skip-list" function?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 5327 Views - Last Post: 29 March 2009 - 11:46 AM Rate Topic: -----

#1 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

How to implement simple "stop-word/skip-list" function?

Post icon  Posted 28 March 2009 - 09:36 AM

Hi guys,

I have a C programming homework where I have to implement a function that will "stop/skip" words entered by a user that are less than 3 characters in length (example: a, the, on, if, lol, wtf etc.). There are no specific "target" words that need to be ignored. Anything equal to or less than 3 characters in length should be ignored. Any ideas how I should go about it? I googled skip lists and word stopping implementation, and each one of them I came across mostly went way over my head! :blink: The requirement they expect of me is very simple, yet I'm too disillusioned by the codes I've been reading and other codes I've been writing that I'm simply not able to think of a simple implementation of said function. :crazy: Any ideas would be greatly appreciated!

Thanks in advance.

This post has been edited by agent_logic: 28 March 2009 - 09:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to implement simple "stop-word/skip-list" function?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 09:43 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 09:52 AM

Hi PsychoCoder,

That's my dilemma, I haven't been able to think of a workable soultion for my problem. I'm not asking anyone to write code for me, I'm just asking for ideas and a few pointers in the right direction in terms of logic for my problem. I hope this is not against D.I.C policy, asking for pointers rather than code.

Cheers.
Was This Post Helpful? 0
  • +
  • -

#4 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 10:59 AM

Can I ask what you mean by "skip" a word?
Was This Post Helpful? 0
  • +
  • -

#5 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 07:08 PM

For example, if the user enters "This is a query", return "This query." "is" and "a" have been skipped because they are less than 3 characters in length. Something like what search engines do, but this one is a lot simpler.
Was This Post Helpful? 0
  • +
  • -

#6 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 07:18 PM

Do you know how to read a string from standard input?
Was This Post Helpful? 0
  • +
  • -

#7 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 07:23 PM

Yeah, I know the getting-input-from-user part of it. It is just the working-with-the-input part I'm baffled with. I'm probably wrong, but I have a vague feeling that a combination of substr() and strlen() might do the job...

This post has been edited by agent_logic: 28 March 2009 - 07:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 07:29 PM

You're using C++ and not C, right?
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 07:49 PM

generally I would say "RegEx" but well we are in C.

Basically if you are just removing any words less then 3 chars long this is not hard, just set up a "result" buffer and scan over the input string and copy over any words longer than 3 letters.

!!! Not compiled -- just off the top of my head to show you what I am thinking !!!
char input[] = "I had a tiny turtle his name was tiny tim";
char buffer[sizeof(input)];
char *bufPtr = buffer;
char *basePtr = input;
char *ptr = input;
while(*ptr != 0) {
	if (*ptr = 32) {
		if (ptr - basePtr > 3) { //if it is long copy it over
			while (basePtr <  ptr) { *bufPtr++ = *basePtr++; }
		} else {
			basePtr = ptr; //ignore the short little word
		}
	ptr++;
}
*bufPtr = 0; // null terminate string
printf("result is: %s", buffer);
-- Note that I can almost guarantee that the above code has bugs, but it should show you a direction to go.
Was This Post Helpful? 1
  • +
  • -

#10 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 08:14 PM

@bsaunders: No mate, I'm using C.

@ NickDMax: Thanks for the logic mate, I have something to work towards now! :D I'll work on it and get back. By the way, I did read about RegEx in C, but it looked way complicated than the ones I've come across in php and perl, so I dropped the idea lol.

Cheers.
Was This Post Helpful? 0
  • +
  • -

#11 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: How to implement simple "stop-word/skip-list" function?

Posted 28 March 2009 - 08:55 PM

NickDMax's solution should work. The only way I can think of in C is:

	char line[80];
	char *linePtr;

	linePtr = line;

	/* Read a line from standard input */
	gets(line);

	while(*linePtr) {
		char word[20];

		/* Extract a word from the line string */
		sscanf(linePtr, "%s", word);

		if(strlen(word) > 3) { /* Is the word longer than three letters? */
			static int firstWord = 1;

			/* Print the word to standard output */

			/* Print a space before the word if it isn't the first word to
			   seperate it from the previous word */
			if(!firstWord)
				printf(" ");

			printf(word);

			firstWord = 0;
		}

		linePtr += strlen(word);
	}

This post has been edited by bsaunders: 28 March 2009 - 09:04 PM

Was This Post Helpful? 1
  • +
  • -

#12 agent_logic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-July 08

Re: How to implement simple "stop-word/skip-list" function?

Posted 29 March 2009 - 10:55 AM

That is pure genius, bsaunders! I was just asking around for directions but you took the time to slap down the entire code! I feel guilty now, lol. :D Thanks man, really appreciate it! :D
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to implement simple "stop-word/skip-list" function?

Posted 29 March 2009 - 11:04 AM

lol, sscanf() should do the inner loop of my example without all the pointer work. Sorry I had been working in assembly language and was not really thinking about the tools available in C
Was This Post Helpful? 0
  • +
  • -

#14 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: How to implement simple "stop-word/skip-list" function?

Posted 29 March 2009 - 11:07 AM

View PostNickDMax, on 29 Mar, 2009 - 10:04 AM, said:

lol, sscanf() should do the inner loop of my example without all the pointer work. Sorry I had been working in assembly language and was not really thinking about the tools available in C


Your code was very good from something off the top of the head. :)
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to implement simple "stop-word/skip-list" function?

Posted 29 March 2009 - 11:37 AM

I am sorry to say that I don't quite feel so warm and fuzzy about your example:

Just a few security/stability notes:
gets(line); -- gets really should not be used in general since it makes no attempt to ensure the input does not exceed the end of the buffer. Use fgets, or scanf instead (or roll your own).

printf(word); -- Never let a user control the format string of a printf! If the word was "%s%s" then a bunch of random memory would be output and it would more than likely crash the program. use printf("%s",word);


I realize that these probably don't make a differance in student code, but one might as well start out learning good habits rather than bad ones.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2