autocomplete from dictionary file

Hey guys and girls, its me (creativemfs).

Anyway, I am working on a project and have compiled a dictionary text file of english language words. I want to use this dictionary for an autocomplete feature that I would like to add to the program.

I am having trouble finding anything about either
1) an autocomplete library I can #include into my code.
2) how to write my own.

Can anyone point me to a pre-written (free) autocomplete lib or
explain to me how they work so I can write my own.

Thank you all, and nice to be back!!
Michaela

p.s.

I need it to be in either C or c#. If i can get the algorithm in C then I can just convert it to c#
Last edited on
Autocomplete is simple: you just take what user are currently typing, look up words starting from this in dictionary and suggest them to user.
Look at "bash-completion" source code.
It is written in C and it is from GNU project.
Thank you guys, and you were right. It is very simple. I know this version is O(n) but for a first try, I am happy. I will be working on something better, such as a bst method or something like that.

This version uses '1' and '2' to accept words because std::cin skips whitespace so I cannot get ' ' to show up. Anyway, when we put it in c# it will be much better because then we can just use the onchange event and have a text box to work with. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * autoc.cpp
 *
 *  Created on: Mar 15, 2013
 *      Author: michaela
 */

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

int main()
{
	std::vector<std::string> dict;
	std::vector<std::string>::iterator it;
	std::ifstream inf;
	std::string temp, word, sentence;
	bool done = false;
	char input;

	inf.open("dict.txt");

	while(!inf.eof())
	{
		inf >> word;
		dict.push_back(word);
	}

	temp = "";
	word = "";
	sentence = "";

	std::cout << "Press 1 to accept autocomplete word\n" <<
			  "Press 2 to accept current word\n" << std::endl;

	while(!done)
	{
		std::cin >> input;
		if(input == '1')
		{
			sentence += temp + ' ';
			word = "";
			std::cout << sentence << std::endl;
		}
		else if(input == '2')
		{
			sentence += word + ' ';
			word = "";
			std::cout << sentence << std::endl;
		}
		else if(input != '0')
		{
			word += input;
			for(it = dict.begin(); it != dict.end(); ++it)
			{
				if(word == (*it).substr(0, word.length()))
				{
					std::cout << (*it) << std::endl;
					temp = (*it);
					break;
				}
			}
		}
		else
			done = true;
	}

	std::cout << sentence << std::endl;

	return 0;
}
Topic archived. No new replies allowed.