Knowledgebase Home Page  >  RapidSpell Desktop .NET  >  Behavior Customization
Search the Knowledge Base
How can I use a database based user dictionary instead of the default file based dictionary?
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=129&catId=62

Options

Print this page
Email this to a friend
By default, setting the .UserDictionaryFile property in the controls to a file path will mean that when the user clicks "Add" in the GUI, that the word is stored in that file.  To instead work with database based user dictionaries you can specify your own descendant of our UserDictionary class, to be used as the user dictionary.  This means of course that you can override the behavior in order to work with databases or anything else.
 
This class is just a skeleton that needs the actual DB implementation code to be added. This code need only read the word list from a table, and add new words to the list/DB. The example shows how a dictionary identifier (dictionaryId) can be passed, however this isn't mandatory and all users can share the same dictionary. Although, the identifier could be used to decide who can add words and who cannot etc.
In the example code below, a dummy list wordList holds a preset dictionary - your task will be to pull a list of words from your database and populate the list.  The AddWord method is also responsible for writing the new word to your DB.
 
 
DBUserDictionary Class


C#
===

using System;

using System.Collections;

using System.Data.SqlClient;

using System.Data;

namespace CustomUserDic

{

/// <summary>

/// Extends and customises UserDictionary functionality.

/// This is a simple example class, to demonstrate the principle, your own implementation

/// should handle your connections, db setup and exceptions as per the rest of your

/// application.

/// </summary>

public class DBUserDictionary : Keyoti.RapidSpell.UserDictionary

{

ArrayList wordList;

int wordLimit;

String dictionaryId;

public DBUserDictionary(String dictionaryId, int maxNumWords)

{

wordList = new ArrayList(50);

wordLimit = maxNumWords;

this.dictionaryId = dictionaryId;

//Commented out to simplify and test;

//The purpose of this is to read all user words stored in the DB into the list

//SqlConnection conn = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=pb_mcsis;server=localhost;Connect Timeout=30");

//SqlCommand command = new SqlCommand("select * From DictionaryUser", conn);

//conn.Open();

//SqlDataReader dr = command.ExecuteReader(CommandBehavior.SingleResult);

//while(dr.Read())

// wordList.Add(dr[0]);

//conn.Close();

wordList.Add("ProgressBook");

wordList.Add("Paul");

wordList.Add("Dale");

wordList.Add("WordsBySA");

}

 

public DBUserDictionary(String dictionaryId) : this(dictionaryId, 100000) { }

 

 

public override int ReadAll(ArrayList a)

{

//used by RapidSpell to read the words from the DB.

a.Clear();

a.AddRange(wordList);

return a.Count;

}

 

public override bool IsValid()

{

//return true if the word list exists and is valid.

//this should return false, if for example there was an sql exception at some point.

//return wordList != null;

//Defaulted to True for test.

return true;

}

public override bool ReloadDictionary()

{

//doing anything necessary here

return true;

}

public override bool AddWord(String word)

{

wordList.Add(word);

OnListChanged(new ListChangedEventArgs(ListChangedEventArgs.ListChangeOperation.WordAdd, word));

//The purpose of this is to store the word (in "word") in the database.

// SqlConnection conn = new SqlConnection("server=(local)\\NetSDK;database=....;Trusted_Connection=yes");

// SqlCommand command = new SqlCommand("INSERT INTO .............", conn);

// conn.Open();

// command.ExecuteNonQuery();

// conn.Close();

return true;

}

public override bool RemoveWord(string word)

{

wordList.Remove(word);

OnListChanged(new ListChangedEventArgs(ListChangedEventArgs.ListChangeOperation.WordRemove, word));

return base.RemoveWord(word);

}

}

}






VB.NET
=======

Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Data


Namespace CustomUserDic
_
'/
'/ Extends and customises UserDictionary functionality.
'/ This is a simple example class, to demonstrate the principle, your own implementation
'/ should handle your connections, db setup and exceptions as per the rest of your
'/ application.
'/

Public Class DBUserDictionary
Inherits Keyoti.RapidSpell.UserDictionary

Private wordList As ArrayList
Private wordLimit As Integer
Private dictionaryId As [String]


Public Sub New(dictionaryId As [String], maxNumWords As Integer)
wordList = New ArrayList(50)
wordLimit = maxNumWords
Me.dictionaryId = dictionaryId

'Commented out to simplify and test;
'The purpose of this is to read all user words stored in the DB into the list
Dim conn As New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=pb_mcsis;server=localhost;Connect Timeout=30")
Dim command As New SqlCommand("select * From DictionaryUser", conn)

conn.Open()
Dim dr As SqlDataReader = command.ExecuteReader(CommandBehavior.SingleResult)
While dr.Read()
wordList.Add(dr(0))
End While
conn.Close()
End Sub 'New


Public Overrides Function ReloadDictionary() As Boolean

'doing anything necessary here

Return True

End Function




Public Sub New(dictionaryId As [String])
MyClass.New(dictionaryId, 100000)
End Sub 'New



Public Overrides Function ReadAll(a As ArrayList) As Integer
'used by RapidSpell to read the words from the DB.
a.Clear()
a.AddRange(wordList)
Return a.Count
End Function 'ReadAll



Public Overrides Function IsValid() As Boolean
'return true if the word list exists and is valid.
'this should return false, if for example there was an sql exception at some point.
'return wordList != null;
'Defaulted to True for test.
Return True
End Function 'IsValid


Public Overrides Function AddWord(w As [String]) As Boolean
wordList.Add(w)

'The purpose of this is to store the word (in "w") in the database.
Dim conn As New SqlConnection("server=(local)\NetSDK;database=....;Trusted_Connection=yes")
Dim command As New SqlCommand("INSERT INTO .............", conn)
conn.Open()
command.ExecuteNonQuery()
conn.Close()
Return True
End Function 'AddWord
 

Public Overrides Function RemoveWord(word as String) As Boolean

‘remove the word from the dictionary (this is used by undo)

wordlist.Remove(word)

End Function


End Class 'DBUserDictionary
End Namespace 'CustomUserDic

 

Using The DBUserDictionary Class

The new subclass of UserDictionary is then used like this

RapidSpellDialog.CheckerEngine.SetUserDictionary( ud );

RapidSpellAsYouType.RapidSpellChecker.SetUserDictionary( ud );

 
 
 
Email support@keyoti.com if you have any questions please.

Related Questions:

Attachments:

No attachments were found.