I've seen a few people asking for this elsewhere, so here's a basic
implementation of a string trie in C#. I'm using this to load and
search a dictionary of about two hundred thousand words. It's fairly
quick (a couple of seconds for the full load). It's a bit of a memory
hog -- about 50MB resident, once the file is loaded -- but most of my
C#/.NET programs are.
Example usage:
StringTrie st = new StringTrie();
st.LoadFromFile ("wordlist.txt" );
st.Add("additio nalword");
st.Add("additio nalotherword");
if ( st.Search("mywo rd") == StringTrie.Foun d )
etc...
using System;
using System.IO;
using System.Collecti ons.Generic;
using System.Text;
namespace TrieLib
{
class StringTrie
{
private StringTrieBucke t bucket = new StringTrieBucke t(' ');
public enum SearchResults
{
NotFound,
Found,
FoundPartial
}
public StringTrie()
{
}
public void LoadFromFile(st ring pathname)
{
TextReader tr = File.OpenText(p athname);
string line;
while ( (line = tr.ReadLine() ) != "")
{
string[] sa = line.Split(' ');
foreach (string s in sa)
if (s.Length < 11)
Add(s);
}
tr.Close();
GC.Collect();
}
public void Add(string s)
{
Queue<charlette rs = new
Queue<char>(s.T oLower().ToChar Array());
AddToTrie(bucke t, letters);
}
public SearchResults Search(string s)
{
Queue<charlette rs = new
Queue<char>(s.T oLower().ToChar Array());
return SearchTrie(buck et, letters);
}
private SearchResults SearchTrie(Stri ngTrieBucket b,
Queue<charlette rs)
{
if (letters.Count == 0)
if (b.terminal)
return SearchResults.F ound;
else
return SearchResults.F oundPartial;
Char c = letters.Dequeue ();
StringTrieBucke t bucket = b;
foreach (StringTrieBuck et stb in b.kids)
if (stb.letter == c)
return SearchTrie(stb, letters);
return SearchResults.N otFound;
}
private void AddToTrie(Strin gTrieBucket b, Queue<char>
letters)
{
if (letters.Count == 0)
{
b.terminal = true;
return;
}
Char c = letters.Dequeue ();
StringTrieBucke t bucket = null;
bool found = false;
foreach (StringTrieBuck et stb in b.kids)
if (stb.letter == c)
{
bucket = stb;
found = true;
break;
}
if (! found)
{
bucket = new StringTrieBucke t(c);
b.kids.Add(buck et);
}
AddToTrie(bucke t, letters);
}
}
class StringTrieBucke t
{
internal char letter;
internal List<StringTrie Bucketkids;
internal bool terminal;
internal StringTrieBucke t(char c)
{
this.letter = c;
kids = new List<StringTrie Bucket>();
terminal = false;
}
}
} 0 2075 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Klaus Neuner |
last post by:
Hello,
I need a function that converts a list into a set of regexes. Like so:
string_list =
print string_list2regexes(string_list)
This should return something like:
|
by: Chris S. |
last post by:
I have a number of strings, containing wildcards (e.g. 'abc#e#' where #
is anything), which I want to match with a test string (e.g 'abcdef').
What would be the best way for me to store my strings so lookup is as
fast as possible?
|
by: Polar |
last post by:
Hi!
Do you know a C library for trie data structures (a particular type of
tree)? And some link about trie with C language?
Thank you
Polar
|
by: Paul |
last post by:
Does anyone have an example of a trie data structure implemented in c#? I'm
looking for a string trie to use to hold the word list for my spell checker.
Any examples or suggestions would be great.
thanks
Paul
|
by: openleren |
last post by:
Hi all,
with a little help from my friends, I am trying to construct a
connectionstring from a relative path in web.config.
It contains
<add key="conAccess" value="microsoft.jet.oledb.4.0;data
source=data/database.mdb" />
In my webform I constructed a fucntion
Public Function getConnectionString() As String
| |
by: csharpula csharp |
last post by:
Hello ,
I need to perform the following operation:
There are two strings (str1 and strSet) ,I need to write a function
which will get str1 and strSet and will return an index of the first
occurrence of a character in str1 that belongs to the set of characters
in strSet.
I need to perform it with complexity of O(m+n) /n-str lenght ,m-strSet
lenght.
|
by: anto.anish |
last post by:
Hi ,
Since, i did not want to write instantiations in Source file of all
template methods for various different datatypes that my client might
use, i choose to write implementation of template methods along with
their declarations in the header file. Well, there are also other
files in the project, which include this header file as well, which
all gets compiled, linked and tested well.
#ifndef __ATT_H__
|
by: anto.anish |
last post by:
Hi ,
Since, i did not want to write explicit instantiations in Source file
of all template methods for various different datatypes that my client
might use, i choose to write implementation of template methods along
with their declarations in the header file. Well, there are also other
files in the project, which include this header file as well, which
all gets compiled, linked and tested well.
#ifndef __ATT_H__
|
by: iaminsik |
last post by:
I implemented a TRIE in memory by myself in C.
But, as you know, when a size of data increases, loading time of data
becomes longer.
I want to implement a TRIE in file, which means division of INDEX &
DATA in file and
whenever a user asks a data, TRIE accesses a disk according to INDEX.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |