By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,728 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,728 IT Pros & Developers. It's quick & easy.

i am in need of a source code

P: 3
i want the source code in java to retrive a word from a file which has more than 50,000 words.The file is named as "english.0" which contains most of the words in english which i am using as a dictionary.........
Feb 1 '07 #1
Share this Question
Share on Google+
9 Replies

10K+
P: 13,262
i want the source code in java to retrive a word from a file which has more than 50,000 words.The file is named as "english.0" which contains most of the words in english which i am using as a dictionary.........
We don't like giving code to people who do not make an effort to write the program themselves. Just try the program yourself and post if you get any problems
Feb 1 '07 #2

P: 3
the code which i use now is in javascript which i have given below.
Expand|Select|Wrap|Line Numbers
  1.  var unknownword = 0; 
  2. var valid = 1;
  3. var invalid = 2;
  4. var remaining = 3;
  5.  
  6. var schandler = {
  7.     activeRequest: false,
  8.     words : new Array(),
  9.     pending : new Array(),
  10.     activeWord : null,
  11.     instances : new Array(),
  12.     serverURI : 
  13.     invalidWordBg: 
  14.     httpMethod : 'GET',
  15.     wordsPerReq : 100
  16. };
  17.  
  18. function spellcheckerfn(el, bAllowRich) {
  19.     var lcase, browser, language, o, elRich;
  20.  
  21.     lcase = navigator.userAgent.toLowerCase();
  22.     browser = ((lcase.indexOf("msie") != -1) && (lcase.indexOf("opera") == -1));
  23.     language = ((lcase.indexOf('gecko') != -1) && (lcase.indexOf("khtml") == -1));
  24.  
  25.     if ((!browser) && (!language)) {
  26.         this.el = el;
  27.         this.supported = false;
  28.         return;
  29.     }
  30.  
  31.     elRich = document.createElement('iframe');
  32.  
  33.     this.supported = true;
  34.     elRich.className = 'webfx-spelltextbox';
  35.     el.parentNode.insertBefore(elRich, el);
  36.     elRich.style.width = el.offsetWidth + 'px';
  37.     elRich.style.height = el.offsetHeight + 'px';
  38.     el.style.display = 'none';
  39.  
  40.     if (schandler.instances.length == 0) { schandler._init(); }
  41.     o = new Object();
  42.     o.el = elRich;
  43.     o.doc = null;
  44.     o.self = this;
  45.     schandler.instances.push(o);
  46.  
  47.     this.el = el;
  48.     this.rich = elRich;
  49.     this.doc = '';
  50.     this.allowRich = bAllowRich;
  51.  
  52.     window.setTimeout(function() {
  53.         var doc = elRich.contentDocument || elRich.contentWindow.document;
  54.         doc.designMode = "on";
  55.         window.setTimeout(function() {
  56.             doc.body.style.background = 'window';
  57.             doc.body.style.color = 'windowtext';
  58.             doc.body.style.padding = '0.5ex';
  59.             doc.body.style.margin = '0px';
  60.             doc.body.style.fontFamily = 'menu';
  61.             doc.body.style.border = 'none';
  62.             if ("addEventListener" in doc) {
  63.                 doc.addEventListener("keypress", schandler._handleKey, false);
  64.                 doc.addEventListener("keyup", schandler._handleKeyUp, false);
  65.                 doc.addEventListener("click", schandler._handleContextMenu, false);
  66.             }
  67.             else if ("attachEvent" in doc) {
  68.                 doc.attachEvent("onkeyup", schandler._handleKeyUp);
  69.                 doc.body.attachEvent("onpaste", schandler._handlePaste);
  70.                 doc.attachEvent("onclick", schandler._handleContextMenu);
  71.                 doc.attachEvent("oncontextmenu", schandler._handleContextMenu);
  72.             }
  73.             doc._text = el;
  74.             if (schandler.instances.length == 0) { schandler._init(); }
  75.             o.doc = doc
  76.             o.self.doc = doc;
  77.         }, 100);
  78.     }, 0);
  79. }
  80.  
  81.  
  82. spellcheckerfn.prototype.fromForm = function() {
  83.     if (!this.supported) { return; }
  84.     this.el.value = this.getText();
  85. }
  86.  
  87.  
  88. spellcheckerfn.prototype.toForm = function() {
  89.     if (!this.supported) { return; }
  90.      this.setText(this.el.value);
  91. }
  92.  
  93.  
  94. spellcheckerfn.prototype.getText = function() {
  95.     if (!this.supported) { return this.el.value; }
  96.     return schandler.getInnerText(this.doc.body);
  97. };
  98.  
  99.  
  100. spellcheckerfn.prototype.getHTML = function() {
  101.     if (!this.supported) { return this.el.value; }
  102.     return schandler.getInnerHTML(this.doc.body);
  103. };
  104.  
  105.  
  106. spellcheckerfn.prototype.setText = function(str) {
  107.     var i, len, c, str, node;
  108.  
  109.     if (!this.supported) { this.el.value = str; return; }
  110.  
  111.     while (this.doc.body.firstChild) { this.doc.body.removeChild(this.doc.body.firstChild); }
  112.  
  113.     len = str.length;
  114.     word = '';
  115.     for (i = 0; i < len; i++) {
  116.         c = str.substr(i, 1);
  117.         if (!c.match(/[\w\']/)) { // Match all but numbers, letters, - and '
  118.             if (word) {
  119.                 this.doc.body.appendChild(schandler._createWordNode(word, this.doc));
  120.             }
  121.             this.doc.body.appendChild(this.doc.createTextNode(c));
  122.             word = '';
  123.         }
  124.         else { word += c; }
  125.     }
  126.     if (word) { this.doc.body.appendChild(this.doc.createTextNode(word)); }
  127. };
  128.  
  129.  
  130. spellcheckerfn.prototype.rescan = function() {
  131.     if (!this.supported) { return; }
  132.  
  133.     schandler._hideSuggestionsMenu();
  134.     schandler._rescanNode(this.doc.body, this.doc, this.allowRich);
  135. }
  136.  
  137.  
  138. spellcheckerfn.prototype.execCommand = function(sCmd, bShow, sValue) {
  139.     if (!this.supported) { return; }
  140.  
  141.     if (document.getElementById('webfxSpellCheckMenu').style.display == 'none') {
  142.         if (this.allowRich) {
  143.             this.doc.execCommand(sCmd, bShow, sValue);
  144. }    }    };
  145.  
  146.  
  147. schandler._handleKey = function(e) {
  148.     var el = e.target || e.srcElement;
  149.  
  150.     schandler._hideSuggestionsMenu();
  151.     if (e.charCode) {
  152.         schandler._char = String.fromCharCode(e.charCode);
  153.     }
  154.     else {
  155.         if (e.keyCode == 13) {
  156.             schandler._char = '\n';
  157.             schandler._moz_parseActiveNode(e, el.ownerDocument);
  158.             return;
  159.         }
  160.         schandler._char = '';
  161.     }
  162. };
  163.  
  164.  
  165. schandler._handleKeyUp = function(e) {
  166.     var n, el = e.target || e.srcElement;
  167.  
  168.     schandler._hideSuggestionsMenu();
  169.  
  170.     /* Internet Explorer */
  171.     if (document.all) {
  172.         if (e.keyCode == 13) {
  173.             for (n = 0; n < schandler.instances.length; n++) {
  174.                 if (schandler.instances[n].doc == el.ownerDocument) {
  175.                     window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
  176.         }    }    }
  177.         else if ((e.keyCode == 32) || (e.keyCode == 188) || (e.keyCode == 190) || (e.keyCode == 109) || (e.keyCode == 189) || (e.keyCode == 186)) {
  178.              schandler._ie_parseActiveNode(el.ownerDocument || el.document);
  179.     }    }
  180.  
  181.     /* Mozilla */
  182.     else {
  183.         schandler._moz_parseActiveNode(e, el.ownerDocument);
  184.     }
  185. };
  186.  
  187.  
  188. schandler._handlePaste = function(e) {
  189.     var n, el = el = e.target || e.srcElement;
  190.  
  191.     for (n = 0; n < schandler.instances.length; n++) {
  192.         if (schandler.instances[n].doc == el.ownerDocument) {
  193.             window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
  194.         }
  195.     }
  196. };
  197.  
  198.  
  199. schandler._handleContextMenu = function(e) {
  200.     var el = e.target || e.srcElement;
  201.     if ((el.tagName == 'SPAN') && (el.firstChild)) {
  202.         var word = el.firstChild.nodeValue;
  203.         if ((schandler.words[word]) && (schandler.words[word][0] == invalid)) {
  204.             schandler._showSuggestionMenu(e, el, word);
  205.             if (document.all) { return false; }
  206.             else { e.preventDefault(); }
  207.             return;
  208.     }    }
  209.     schandler._hideSuggestionsMenu();
  210. };
  211.  
  212.  
  213. schandler._init = function() {
  214.     var menu, inner, item;
  215.  
  216.     menu = document.createElement('div');
  217.     menu.id = 'webfxSpellCheckMenu';
  218.     menu.className = 'webfx-spellchecker-menu';
  219.     menu.style.display = 'none';
  220.  
  221.     inner = document.createElement('div');
  222.     inner.className = 'inner';
  223.     menu.appendChild(inner);
  224.  
  225.     item = document.createElement('div');
  226.     item.className = 'separator';
  227.     inner.appendChild(item);
  228.  
  229.     item = document.createElement('a');
  230.     item.href = 'javascript:schandler._ignoreWord();'
  231.     item.appendChild(document.createTextNode('Ignore'));
  232.     inner.appendChild(item);
  233.  
  234.     document.body.appendChild(menu);
  235. };
  236.  
in which in schandler i am not able to specify the serverURL and invalidwordBg
becoz i dono which file i should place in the server to access the dictionary.
Feb 1 '07 #3

10K+
P: 13,262
the code which i use now is in javascript which i have given below.
Expand|Select|Wrap|Line Numbers
  1.  var unknownword = 0; 
  2. var valid = 1;
  3. var invalid = 2;
  4. var remaining = 3;
  5.  
  6. var schandler = {
  7.     activeRequest: false,
  8.     words : new Array(),
  9.     pending : new Array(),
  10.     activeWord : null,
  11.     instances : new Array(),
  12.     serverURI : 
  13.     invalidWordBg: 
  14.     httpMethod : 'GET',
  15.     wordsPerReq : 100
  16. };
  17.  
  18. function spellcheckerfn(el, bAllowRich) {
  19.     var lcase, browser, language, o, elRich;
  20.  
  21.     lcase = navigator.userAgent.toLowerCase();
  22.     browser = ((lcase.indexOf("msie") != -1) && (lcase.indexOf("opera") == -1));
  23.     language = ((lcase.indexOf('gecko') != -1) && (lcase.indexOf("khtml") == -1));
  24.  
  25.     if ((!browser) && (!language)) {
  26.         this.el = el;
  27.         this.supported = false;
  28.         return;
  29.     }
  30.  
  31.     elRich = document.createElement('iframe');
  32.  
  33.     this.supported = true;
  34.     elRich.className = 'webfx-spelltextbox';
  35.     el.parentNode.insertBefore(elRich, el);
  36.     elRich.style.width = el.offsetWidth + 'px';
  37.     elRich.style.height = el.offsetHeight + 'px';
  38.     el.style.display = 'none';
  39.  
  40.     if (schandler.instances.length == 0) { schandler._init(); }
  41.     o = new Object();
  42.     o.el = elRich;
  43.     o.doc = null;
  44.     o.self = this;
  45.     schandler.instances.push(o);
  46.  
  47.     this.el = el;
  48.     this.rich = elRich;
  49.     this.doc = '';
  50.     this.allowRich = bAllowRich;
  51.  
  52.     window.setTimeout(function() {
  53.         var doc = elRich.contentDocument || elRich.contentWindow.document;
  54.         doc.designMode = "on";
  55.         window.setTimeout(function() {
  56.             doc.body.style.background = 'window';
  57.             doc.body.style.color = 'windowtext';
  58.             doc.body.style.padding = '0.5ex';
  59.             doc.body.style.margin = '0px';
  60.             doc.body.style.fontFamily = 'menu';
  61.             doc.body.style.border = 'none';
  62.             if ("addEventListener" in doc) {
  63.                 doc.addEventListener("keypress", schandler._handleKey, false);
  64.                 doc.addEventListener("keyup", schandler._handleKeyUp, false);
  65.                 doc.addEventListener("click", schandler._handleContextMenu, false);
  66.             }
  67.             else if ("attachEvent" in doc) {
  68.                 doc.attachEvent("onkeyup", schandler._handleKeyUp);
  69.                 doc.body.attachEvent("onpaste", schandler._handlePaste);
  70.                 doc.attachEvent("onclick", schandler._handleContextMenu);
  71.                 doc.attachEvent("oncontextmenu", schandler._handleContextMenu);
  72.             }
  73.             doc._text = el;
  74.             if (schandler.instances.length == 0) { schandler._init(); }
  75.             o.doc = doc
  76.             o.self.doc = doc;
  77.         }, 100);
  78.     }, 0);
  79. }
  80.  
  81.  
  82. spellcheckerfn.prototype.fromForm = function() {
  83.     if (!this.supported) { return; }
  84.     this.el.value = this.getText();
  85. }
  86.  
  87.  
  88. spellcheckerfn.prototype.toForm = function() {
  89.     if (!this.supported) { return; }
  90.      this.setText(this.el.value);
  91. }
  92.  
  93.  
  94. spellcheckerfn.prototype.getText = function() {
  95.     if (!this.supported) { return this.el.value; }
  96.     return schandler.getInnerText(this.doc.body);
  97. };
  98.  
  99.  
  100. spellcheckerfn.prototype.getHTML = function() {
  101.     if (!this.supported) { return this.el.value; }
  102.     return schandler.getInnerHTML(this.doc.body);
  103. };
  104.  
  105.  
  106. spellcheckerfn.prototype.setText = function(str) {
  107.     var i, len, c, str, node;
  108.  
  109.     if (!this.supported) { this.el.value = str; return; }
  110.  
  111.     while (this.doc.body.firstChild) { this.doc.body.removeChild(this.doc.body.firstChild); }
  112.  
  113.     len = str.length;
  114.     word = '';
  115.     for (i = 0; i < len; i++) {
  116.         c = str.substr(i, 1);
  117.         if (!c.match(/[\w\']/)) { // Match all but numbers, letters, - and '
  118.             if (word) {
  119.                 this.doc.body.appendChild(schandler._createWordNode(word, this.doc));
  120.             }
  121.             this.doc.body.appendChild(this.doc.createTextNode(c));
  122.             word = '';
  123.         }
  124.         else { word += c; }
  125.     }
  126.     if (word) { this.doc.body.appendChild(this.doc.createTextNode(word)); }
  127. };
  128.  
  129.  
  130. spellcheckerfn.prototype.rescan = function() {
  131.     if (!this.supported) { return; }
  132.  
  133.     schandler._hideSuggestionsMenu();
  134.     schandler._rescanNode(this.doc.body, this.doc, this.allowRich);
  135. }
  136.  
  137.  
  138. spellcheckerfn.prototype.execCommand = function(sCmd, bShow, sValue) {
  139.     if (!this.supported) { return; }
  140.  
  141.     if (document.getElementById('webfxSpellCheckMenu').style.display == 'none') {
  142.         if (this.allowRich) {
  143.             this.doc.execCommand(sCmd, bShow, sValue);
  144. }    }    };
  145.  
  146.  
  147. schandler._handleKey = function(e) {
  148.     var el = e.target || e.srcElement;
  149.  
  150.     schandler._hideSuggestionsMenu();
  151.     if (e.charCode) {
  152.         schandler._char = String.fromCharCode(e.charCode);
  153.     }
  154.     else {
  155.         if (e.keyCode == 13) {
  156.             schandler._char = '\n';
  157.             schandler._moz_parseActiveNode(e, el.ownerDocument);
  158.             return;
  159.         }
  160.         schandler._char = '';
  161.     }
  162. };
  163.  
  164.  
  165. schandler._handleKeyUp = function(e) {
  166.     var n, el = e.target || e.srcElement;
  167.  
  168.     schandler._hideSuggestionsMenu();
  169.  
  170.     /* Internet Explorer */
  171.     if (document.all) {
  172.         if (e.keyCode == 13) {
  173.             for (n = 0; n < schandler.instances.length; n++) {
  174.                 if (schandler.instances[n].doc == el.ownerDocument) {
  175.                     window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
  176.         }    }    }
  177.         else if ((e.keyCode == 32) || (e.keyCode == 188) || (e.keyCode == 190) || (e.keyCode == 109) || (e.keyCode == 189) || (e.keyCode == 186)) {
  178.              schandler._ie_parseActiveNode(el.ownerDocument || el.document);
  179.     }    }
  180.  
  181.     /* Mozilla */
  182.     else {
  183.         schandler._moz_parseActiveNode(e, el.ownerDocument);
  184.     }
  185. };
  186.  
  187.  
  188. schandler._handlePaste = function(e) {
  189.     var n, el = el = e.target || e.srcElement;
  190.  
  191.     for (n = 0; n < schandler.instances.length; n++) {
  192.         if (schandler.instances[n].doc == el.ownerDocument) {
  193.             window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
  194.         }
  195.     }
  196. };
  197.  
  198.  
  199. schandler._handleContextMenu = function(e) {
  200.     var el = e.target || e.srcElement;
  201.     if ((el.tagName == 'SPAN') && (el.firstChild)) {
  202.         var word = el.firstChild.nodeValue;
  203.         if ((schandler.words[word]) && (schandler.words[word][0] == invalid)) {
  204.             schandler._showSuggestionMenu(e, el, word);
  205.             if (document.all) { return false; }
  206.             else { e.preventDefault(); }
  207.             return;
  208.     }    }
  209.     schandler._hideSuggestionsMenu();
  210. };
  211.  
  212.  
  213. schandler._init = function() {
  214.     var menu, inner, item;
  215.  
  216.     menu = document.createElement('div');
  217.     menu.id = 'webfxSpellCheckMenu';
  218.     menu.className = 'webfx-spellchecker-menu';
  219.     menu.style.display = 'none';
  220.  
  221.     inner = document.createElement('div');
  222.     inner.className = 'inner';
  223.     menu.appendChild(inner);
  224.  
  225.     item = document.createElement('div');
  226.     item.className = 'separator';
  227.     inner.appendChild(item);
  228.  
  229.     item = document.createElement('a');
  230.     item.href = 'javascript:schandler._ignoreWord();'
  231.     item.appendChild(document.createTextNode('Ignore'));
  232.     inner.appendChild(item);
  233.  
  234.     document.body.appendChild(menu);
  235. };
  236.  
in which in schandler i am not able to specify the serverURL and invalidwordBg
becoz i dono which file i should place in the server to access the dictionary.
In which case I'll now move this to the Javascript forum.
Feb 1 '07 #4

acoder
Expert Mod 15k+
P: 16,027
Did you write this code yourself? If not, it must have come with some documentation about how to define serverURI and invalidWordBg. If you provide a link or check the site for yourself, it should be easy to figure out how to specify these.

One other thing: where are these two actually used? You must have a function library which you are using?
Feb 1 '07 #5

iam_clint
Expert 100+
P: 1,208
this is a ridiculous amount of code for what your trying to achieve.


A database would take you 3 lines of code to search it.
Feb 1 '07 #6

P: 3
forget those.
let me start from first again. i have stared trying that assignment in java.
as that of the first step i am not able to search a word from the file where i have all the words.right now i have wrote something to create a file and search a text from it.i have atext file named "english.0" which contains the following.

master's

mastered

masterful

masterfully

masterfulness

mastering

masterings

masterliness

masterly

masterpiece


my code is this

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class filetry {
public String record = null;

public static void main(String args[]) throws IOException
{
System.out.println("started");
FileInputStream fis = new FileInputStream("english.0");
System.out.println("stage1");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
String record = dis.readLine();
System.out.println("hi");
Set data = new HashSet();

while (record != null) {
data.add(record);
record = dis.readLine();
}

try {

while ( (record=dis.readLine()) != null ) {
System.out.println(record);
data.contains("Mastering");
System.out.println("true");
}

} catch (IOException e) {
System.out.println("Error");
}

the output which i get is a error saying that

started
Exception in thread "main" java.io.FileNotFoundException: english.0 (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at filetry.main(filetry.java:14)


but i have that fileonly in the same folder.
i have tried specifying that by giving "/english.0" , but i get the same error.
i run this program with the help of eclipse.
Feb 2 '07 #7

P: 18
You might need to rethink this. An exhaustive search of english.0 is going to take a while. You probably need to build a binary file that contains a search tree instead. Then you would traverse the tree until you found the word (spelling was correct) or you ran to the end of the tree (word was not correct). You can get some very good performance this way.
Feb 8 '07 #8

P: 18
Oh - and reading 50,000 words into a HashSet like you described in the above code may work, but it's going to kill some serious memory. HashSet really wasn't meant to contain that many entries. Besides, you have to create the HashSet every time your program runs.
Feb 8 '07 #9

dorinbogdan
Expert 100+
P: 839
I did few changes to the Java code, and it should work:
(DataInputStream.readLine is deprecated, use BufferedReader.readLine instead)
Also, search the file for "mastering" instead of "Mastering", it is case sensitive.

Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.InputStreamReader;
  4. import java.io.IOException;
  5. import java.util.HashSet;
  6. import java.util.Set;
  7.  
  8. public class filetry {
  9. public String record = null; 
  10.  
  11. public static void main(String args[]) throws IOException
  12. {
  13. System.out.println("started");
  14. FileInputStream fis = new FileInputStream("english.0");
  15. System.out.println("stage1");
  16. BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  17.  
  18. String record = br.readLine();
  19. System.out.println("hi");
  20. Set data = new HashSet();
  21.  
  22. try { 
  23.     while (record != null) {
  24.         data.add(record);
  25.         record = br.readLine();
  26.         }
  27.         br.close();
  28.  
  29. } catch (IOException e) { 
  30. System.out.println("Error");
  31. if (data.contains("mastering"))
  32.     System.out.println("true");
  33.  
  34. }
  35. }
Feb 9 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.