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.........
9 1666
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
the code which i use now is in javascript which i have given below. - var unknownword = 0;
-
var valid = 1;
-
var invalid = 2;
-
var remaining = 3;
-
-
var schandler = {
-
activeRequest: false,
-
words : new Array(),
-
pending : new Array(),
-
activeWord : null,
-
instances : new Array(),
-
serverURI :
-
invalidWordBg:
-
httpMethod : 'GET',
-
wordsPerReq : 100
-
};
-
-
function spellcheckerfn(el, bAllowRich) {
-
var lcase, browser, language, o, elRich;
-
-
lcase = navigator.userAgent.toLowerCase();
-
browser = ((lcase.indexOf("msie") != -1) && (lcase.indexOf("opera") == -1));
-
language = ((lcase.indexOf('gecko') != -1) && (lcase.indexOf("khtml") == -1));
-
-
if ((!browser) && (!language)) {
-
this.el = el;
-
this.supported = false;
-
return;
-
}
-
-
elRich = document.createElement('iframe');
-
-
this.supported = true;
-
elRich.className = 'webfx-spelltextbox';
-
el.parentNode.insertBefore(elRich, el);
-
elRich.style.width = el.offsetWidth + 'px';
-
elRich.style.height = el.offsetHeight + 'px';
-
el.style.display = 'none';
-
-
if (schandler.instances.length == 0) { schandler._init(); }
-
o = new Object();
-
o.el = elRich;
-
o.doc = null;
-
o.self = this;
-
schandler.instances.push(o);
-
-
this.el = el;
-
this.rich = elRich;
-
this.doc = '';
-
this.allowRich = bAllowRich;
-
-
window.setTimeout(function() {
-
var doc = elRich.contentDocument || elRich.contentWindow.document;
-
doc.designMode = "on";
-
window.setTimeout(function() {
-
doc.body.style.background = 'window';
-
doc.body.style.color = 'windowtext';
-
doc.body.style.padding = '0.5ex';
-
doc.body.style.margin = '0px';
-
doc.body.style.fontFamily = 'menu';
-
doc.body.style.border = 'none';
-
if ("addEventListener" in doc) {
-
doc.addEventListener("keypress", schandler._handleKey, false);
-
doc.addEventListener("keyup", schandler._handleKeyUp, false);
-
doc.addEventListener("click", schandler._handleContextMenu, false);
-
}
-
else if ("attachEvent" in doc) {
-
doc.attachEvent("onkeyup", schandler._handleKeyUp);
-
doc.body.attachEvent("onpaste", schandler._handlePaste);
-
doc.attachEvent("onclick", schandler._handleContextMenu);
-
doc.attachEvent("oncontextmenu", schandler._handleContextMenu);
-
}
-
doc._text = el;
-
if (schandler.instances.length == 0) { schandler._init(); }
-
o.doc = doc
-
o.self.doc = doc;
-
}, 100);
-
}, 0);
-
}
-
-
-
spellcheckerfn.prototype.fromForm = function() {
-
if (!this.supported) { return; }
-
this.el.value = this.getText();
-
}
-
-
-
spellcheckerfn.prototype.toForm = function() {
-
if (!this.supported) { return; }
-
this.setText(this.el.value);
-
}
-
-
-
spellcheckerfn.prototype.getText = function() {
-
if (!this.supported) { return this.el.value; }
-
return schandler.getInnerText(this.doc.body);
-
};
-
-
-
spellcheckerfn.prototype.getHTML = function() {
-
if (!this.supported) { return this.el.value; }
-
return schandler.getInnerHTML(this.doc.body);
-
};
-
-
-
spellcheckerfn.prototype.setText = function(str) {
-
var i, len, c, str, node;
-
-
if (!this.supported) { this.el.value = str; return; }
-
-
while (this.doc.body.firstChild) { this.doc.body.removeChild(this.doc.body.firstChild); }
-
-
len = str.length;
-
word = '';
-
for (i = 0; i < len; i++) {
-
c = str.substr(i, 1);
-
if (!c.match(/[\w\']/)) { // Match all but numbers, letters, - and '
-
if (word) {
-
this.doc.body.appendChild(schandler._createWordNode(word, this.doc));
-
}
-
this.doc.body.appendChild(this.doc.createTextNode(c));
-
word = '';
-
}
-
else { word += c; }
-
}
-
if (word) { this.doc.body.appendChild(this.doc.createTextNode(word)); }
-
};
-
-
-
spellcheckerfn.prototype.rescan = function() {
-
if (!this.supported) { return; }
-
-
schandler._hideSuggestionsMenu();
-
schandler._rescanNode(this.doc.body, this.doc, this.allowRich);
-
}
-
-
-
spellcheckerfn.prototype.execCommand = function(sCmd, bShow, sValue) {
-
if (!this.supported) { return; }
-
-
if (document.getElementById('webfxSpellCheckMenu').style.display == 'none') {
-
if (this.allowRich) {
-
this.doc.execCommand(sCmd, bShow, sValue);
-
} } };
-
-
-
schandler._handleKey = function(e) {
-
var el = e.target || e.srcElement;
-
-
schandler._hideSuggestionsMenu();
-
if (e.charCode) {
-
schandler._char = String.fromCharCode(e.charCode);
-
}
-
else {
-
if (e.keyCode == 13) {
-
schandler._char = '\n';
-
schandler._moz_parseActiveNode(e, el.ownerDocument);
-
return;
-
}
-
schandler._char = '';
-
}
-
};
-
-
-
schandler._handleKeyUp = function(e) {
-
var n, el = e.target || e.srcElement;
-
-
schandler._hideSuggestionsMenu();
-
-
/* Internet Explorer */
-
if (document.all) {
-
if (e.keyCode == 13) {
-
for (n = 0; n < schandler.instances.length; n++) {
-
if (schandler.instances[n].doc == el.ownerDocument) {
-
window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
-
} } }
-
else if ((e.keyCode == 32) || (e.keyCode == 188) || (e.keyCode == 190) || (e.keyCode == 109) || (e.keyCode == 189) || (e.keyCode == 186)) {
-
schandler._ie_parseActiveNode(el.ownerDocument || el.document);
-
} }
-
-
/* Mozilla */
-
else {
-
schandler._moz_parseActiveNode(e, el.ownerDocument);
-
}
-
};
-
-
-
schandler._handlePaste = function(e) {
-
var n, el = el = e.target || e.srcElement;
-
-
for (n = 0; n < schandler.instances.length; n++) {
-
if (schandler.instances[n].doc == el.ownerDocument) {
-
window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
-
}
-
}
-
};
-
-
-
schandler._handleContextMenu = function(e) {
-
var el = e.target || e.srcElement;
-
if ((el.tagName == 'SPAN') && (el.firstChild)) {
-
var word = el.firstChild.nodeValue;
-
if ((schandler.words[word]) && (schandler.words[word][0] == invalid)) {
-
schandler._showSuggestionMenu(e, el, word);
-
if (document.all) { return false; }
-
else { e.preventDefault(); }
-
return;
-
} }
-
schandler._hideSuggestionsMenu();
-
};
-
-
-
schandler._init = function() {
-
var menu, inner, item;
-
-
menu = document.createElement('div');
-
menu.id = 'webfxSpellCheckMenu';
-
menu.className = 'webfx-spellchecker-menu';
-
menu.style.display = 'none';
-
-
inner = document.createElement('div');
-
inner.className = 'inner';
-
menu.appendChild(inner);
-
-
item = document.createElement('div');
-
item.className = 'separator';
-
inner.appendChild(item);
-
-
item = document.createElement('a');
-
item.href = 'javascript:schandler._ignoreWord();'
-
item.appendChild(document.createTextNode('Ignore'));
-
inner.appendChild(item);
-
-
document.body.appendChild(menu);
-
};
-
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.
the code which i use now is in javascript which i have given below. - var unknownword = 0;
-
var valid = 1;
-
var invalid = 2;
-
var remaining = 3;
-
-
var schandler = {
-
activeRequest: false,
-
words : new Array(),
-
pending : new Array(),
-
activeWord : null,
-
instances : new Array(),
-
serverURI :
-
invalidWordBg:
-
httpMethod : 'GET',
-
wordsPerReq : 100
-
};
-
-
function spellcheckerfn(el, bAllowRich) {
-
var lcase, browser, language, o, elRich;
-
-
lcase = navigator.userAgent.toLowerCase();
-
browser = ((lcase.indexOf("msie") != -1) && (lcase.indexOf("opera") == -1));
-
language = ((lcase.indexOf('gecko') != -1) && (lcase.indexOf("khtml") == -1));
-
-
if ((!browser) && (!language)) {
-
this.el = el;
-
this.supported = false;
-
return;
-
}
-
-
elRich = document.createElement('iframe');
-
-
this.supported = true;
-
elRich.className = 'webfx-spelltextbox';
-
el.parentNode.insertBefore(elRich, el);
-
elRich.style.width = el.offsetWidth + 'px';
-
elRich.style.height = el.offsetHeight + 'px';
-
el.style.display = 'none';
-
-
if (schandler.instances.length == 0) { schandler._init(); }
-
o = new Object();
-
o.el = elRich;
-
o.doc = null;
-
o.self = this;
-
schandler.instances.push(o);
-
-
this.el = el;
-
this.rich = elRich;
-
this.doc = '';
-
this.allowRich = bAllowRich;
-
-
window.setTimeout(function() {
-
var doc = elRich.contentDocument || elRich.contentWindow.document;
-
doc.designMode = "on";
-
window.setTimeout(function() {
-
doc.body.style.background = 'window';
-
doc.body.style.color = 'windowtext';
-
doc.body.style.padding = '0.5ex';
-
doc.body.style.margin = '0px';
-
doc.body.style.fontFamily = 'menu';
-
doc.body.style.border = 'none';
-
if ("addEventListener" in doc) {
-
doc.addEventListener("keypress", schandler._handleKey, false);
-
doc.addEventListener("keyup", schandler._handleKeyUp, false);
-
doc.addEventListener("click", schandler._handleContextMenu, false);
-
}
-
else if ("attachEvent" in doc) {
-
doc.attachEvent("onkeyup", schandler._handleKeyUp);
-
doc.body.attachEvent("onpaste", schandler._handlePaste);
-
doc.attachEvent("onclick", schandler._handleContextMenu);
-
doc.attachEvent("oncontextmenu", schandler._handleContextMenu);
-
}
-
doc._text = el;
-
if (schandler.instances.length == 0) { schandler._init(); }
-
o.doc = doc
-
o.self.doc = doc;
-
}, 100);
-
}, 0);
-
}
-
-
-
spellcheckerfn.prototype.fromForm = function() {
-
if (!this.supported) { return; }
-
this.el.value = this.getText();
-
}
-
-
-
spellcheckerfn.prototype.toForm = function() {
-
if (!this.supported) { return; }
-
this.setText(this.el.value);
-
}
-
-
-
spellcheckerfn.prototype.getText = function() {
-
if (!this.supported) { return this.el.value; }
-
return schandler.getInnerText(this.doc.body);
-
};
-
-
-
spellcheckerfn.prototype.getHTML = function() {
-
if (!this.supported) { return this.el.value; }
-
return schandler.getInnerHTML(this.doc.body);
-
};
-
-
-
spellcheckerfn.prototype.setText = function(str) {
-
var i, len, c, str, node;
-
-
if (!this.supported) { this.el.value = str; return; }
-
-
while (this.doc.body.firstChild) { this.doc.body.removeChild(this.doc.body.firstChild); }
-
-
len = str.length;
-
word = '';
-
for (i = 0; i < len; i++) {
-
c = str.substr(i, 1);
-
if (!c.match(/[\w\']/)) { // Match all but numbers, letters, - and '
-
if (word) {
-
this.doc.body.appendChild(schandler._createWordNode(word, this.doc));
-
}
-
this.doc.body.appendChild(this.doc.createTextNode(c));
-
word = '';
-
}
-
else { word += c; }
-
}
-
if (word) { this.doc.body.appendChild(this.doc.createTextNode(word)); }
-
};
-
-
-
spellcheckerfn.prototype.rescan = function() {
-
if (!this.supported) { return; }
-
-
schandler._hideSuggestionsMenu();
-
schandler._rescanNode(this.doc.body, this.doc, this.allowRich);
-
}
-
-
-
spellcheckerfn.prototype.execCommand = function(sCmd, bShow, sValue) {
-
if (!this.supported) { return; }
-
-
if (document.getElementById('webfxSpellCheckMenu').style.display == 'none') {
-
if (this.allowRich) {
-
this.doc.execCommand(sCmd, bShow, sValue);
-
} } };
-
-
-
schandler._handleKey = function(e) {
-
var el = e.target || e.srcElement;
-
-
schandler._hideSuggestionsMenu();
-
if (e.charCode) {
-
schandler._char = String.fromCharCode(e.charCode);
-
}
-
else {
-
if (e.keyCode == 13) {
-
schandler._char = '\n';
-
schandler._moz_parseActiveNode(e, el.ownerDocument);
-
return;
-
}
-
schandler._char = '';
-
}
-
};
-
-
-
schandler._handleKeyUp = function(e) {
-
var n, el = e.target || e.srcElement;
-
-
schandler._hideSuggestionsMenu();
-
-
/* Internet Explorer */
-
if (document.all) {
-
if (e.keyCode == 13) {
-
for (n = 0; n < schandler.instances.length; n++) {
-
if (schandler.instances[n].doc == el.ownerDocument) {
-
window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
-
} } }
-
else if ((e.keyCode == 32) || (e.keyCode == 188) || (e.keyCode == 190) || (e.keyCode == 109) || (e.keyCode == 189) || (e.keyCode == 186)) {
-
schandler._ie_parseActiveNode(el.ownerDocument || el.document);
-
} }
-
-
/* Mozilla */
-
else {
-
schandler._moz_parseActiveNode(e, el.ownerDocument);
-
}
-
};
-
-
-
schandler._handlePaste = function(e) {
-
var n, el = el = e.target || e.srcElement;
-
-
for (n = 0; n < schandler.instances.length; n++) {
-
if (schandler.instances[n].doc == el.ownerDocument) {
-
window.setTimeout('schandler.instances[' + n + '].self.rescan();', 100);
-
}
-
}
-
};
-
-
-
schandler._handleContextMenu = function(e) {
-
var el = e.target || e.srcElement;
-
if ((el.tagName == 'SPAN') && (el.firstChild)) {
-
var word = el.firstChild.nodeValue;
-
if ((schandler.words[word]) && (schandler.words[word][0] == invalid)) {
-
schandler._showSuggestionMenu(e, el, word);
-
if (document.all) { return false; }
-
else { e.preventDefault(); }
-
return;
-
} }
-
schandler._hideSuggestionsMenu();
-
};
-
-
-
schandler._init = function() {
-
var menu, inner, item;
-
-
menu = document.createElement('div');
-
menu.id = 'webfxSpellCheckMenu';
-
menu.className = 'webfx-spellchecker-menu';
-
menu.style.display = 'none';
-
-
inner = document.createElement('div');
-
inner.className = 'inner';
-
menu.appendChild(inner);
-
-
item = document.createElement('div');
-
item.className = 'separator';
-
inner.appendChild(item);
-
-
item = document.createElement('a');
-
item.href = 'javascript:schandler._ignoreWord();'
-
item.appendChild(document.createTextNode('Ignore'));
-
inner.appendChild(item);
-
-
document.body.appendChild(menu);
-
};
-
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.
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?
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.
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.
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.
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.
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. -
import java.io.BufferedReader;
-
import java.io.FileInputStream;
-
import java.io.InputStreamReader;
-
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");
-
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
-
-
String record = br.readLine();
-
System.out.println("hi");
-
Set data = new HashSet();
-
-
try {
-
while (record != null) {
-
data.add(record);
-
record = br.readLine();
-
}
-
br.close();
-
-
} catch (IOException e) {
-
System.out.println("Error");
-
}
-
if (data.contains("mastering"))
-
System.out.println("true");
-
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Alex |
last post by:
Hello people,
The following is not a troll but a serious request.
I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
|
by: Micha |
last post by:
Hello there,
I think I've run into some classic c++ pitfall and maybe some of
you guys can help me out.
For my project I will need to use matrices and vectors and so I
decided to implement them...
|
by: Henrik_the_boss |
last post by:
Hello all.
I have a couple of aspx pages. When something fails in them, I would like
them to be able to log to either a database, a logfile, or the application
log. All code is in C#
I run...
|
by: moondaddy |
last post by:
I want to create a public enum that can be used throughout a project. I
created an enum like this in a module:
Public Enum ParentType
Project = 0
Stage = 1
VIP = 2
Func = 3
Equipment = 4...
|
by: mike |
last post by:
I help manage a large web site, one that has over
600 html pages... It's a reference site for ham radio
folks and as an example, one page indexes over
1.8 gb of on-line PDF documents.
The site...
|
by: Izzy |
last post by:
I'm writting an app that has two DataGridViews, in the first grid I
load a list on companies. Between the 2 grids are two buttons with
arrows, one points to the right the other to the left.
When...
|
by: Andrew Hedges |
last post by:
Wherein I attempt to debunk some myths about the relative merits of
the two methods for programmatically adding content to a web page:
...
|
by: Ramon F Herrera |
last post by:
My goal is to study (in the RMS sense) and familiarize myself with
some OSS code, until I reach the point at which I can make non-trivial
modifications to it. The class of applications I have in...
|
by: shahiz |
last post by:
This the error i get when i try to run my program
Error: Unable to realize com.sun.media.amovie.AMController@18b81e3
Basically i have a mediapanel class that initialize and play the media as...
|
by: duane.lortie |
last post by:
I'm writing a routine that fetches XML and attempts to parse some
values from it.
A condensed entry node of the XML looks like this ..
<entry>
<title>Some title</title>
<author>...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |