Hi I want to try making a simple application for android phones for which I will be requiring a dictionary. I thought of using urbandictionary.com as the reference site. Is there any technique by which I can extract all the words with the definitions and their respective words in the thesaurus ?
I was checking out the Google example found at
http://developer.android.com/resources/samples/SearchableDictionary/index.html
It appears that they just add their words with this example
private void loadWords() throws IOException {
Log.d(TAG, "Loading words...");
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 2) continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
However, when I look for R.raw.definitions that directory is empty.
http://developer.android.com/resources/samples/SearchableDictionary/res/raw/index.html
Related
I Want to Make A System that compares password (4 letter numeric).
Input Code (I Already Have fis)
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
while((text = br.readLine()) != null)
{
sb.append(text).append("\n");
}
String finalinput = sb.toString();
String finalpassinput = password.getText().toString();
Toast.makeText(this, "finalinput:"+finalinput+"finalpassinput:"+finalpassinput,Toast.LENGTH_LONG).show();
if(finalinput.equals(finalpassinput))
{
Toast.makeText(this,"Login!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, HomeActivity.class));
}
else
{
Toast.makeText(this,"Incorrect Password",Toast.LENGTH_SHORT).show();
}
And This Source Code will keep say that the two passwords are different.
How I Wrote My File :
fos = openFileOutput(FILE_NAME_PW, MODE_PRIVATE);
fos.write(encodedpw.getBytes());
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME_PW, Toast.LENGTH_LONG).show();
I am curious if encodepw.getBytes() will change anything to the string (Including null characters, etc.) and if Java won't think them the same.
Thank you.
I found myself the answer to it. The finalinput variable had some null charachter before it, so you can get pure string using this function.
public static String FileStringParse(String FileString)
{
FileString = FileString.replaceAll("\\D+","");
return FileString;
}
InputStream is = getResources().openRawResource(R.raw.babynames);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
String line = "";
try {
reader.readLine();
while ((line = reader.readLine()) != null) {
Log.d("MyActivity", "Line: " + line);
String[] tokens = line.split(",");
BabyName name = new BabyName();
name.setGender(tokens[1]);
name.setMeaning(tokens[2]);
name.setName(tokens[3]);
name.setOrigin(tokens[4]);
babyNames.add(name);
Log.d(TAG, "Just created: " + name);
}
} catch (IOException e) {
Log.wtf("MyActivity", "Error reading data file on line" + line, e);
e.printStackTrace();
}
i am trying this but the app crashes and I got an error of array index out of bound and in logs i am getting the data
BabyName name = new BabyName();
name.setGender(tokens[1]);
name.setMeaning(tokens[2]);
name.setName(tokens[3]);
name.setOrigin(tokens[4]);
shouldn't the index start from 0
name.setGender(tokens[0]);
Need to parse this file (mixed latin & arabic):
1|حِيمِ
2|الَمِينَ
The file was saved as UTF8 in notepad++, and put in android asset folder.
Expected result: for line1, the entries are "1" and "حِيمِ" (split by "|").
AssetManager manager = context.getAssets();
InputStream inStream = null;
inStream = manager.open("file.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
String line = "";
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
if (columns.length>=3) {
Toast toast = Toast.makeText(context, "Line: " + columns[0] + " and " + columns[1], Toast.LENGTH_LONG);
toast.show();
}
}
Actual Result:
columns[0] = "1" ok, but
columns[1] = "%D8%AD%D9..." not Ok, expected "حِيمِ".
How to fix this, or is there better way? Please help. Thanks in advance.
Solved, changing:
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
into
while ((line = buffer.readLine()) != null) {
String[] columns = line.split("\\|");
I want to make epub reader app.Now i am getting only chapter name in the file but how to get whole data in the chapter.
I think I have already posted this out before.
Using nl.siegmann.epublib which you can google.
In my code I will show you how I did it as you look at Book class which shows how the the epub works.
Using Spine on book class I get the maximum spine of the book which means the entire book.
I then convert it to string.
Here is my code on how I did it.
public String getEntireBook()
{
String line, linez = null;
Spine spine = amBook().getSpine();
Resource res;
List<SpineReference> spineList = spine.getSpineReferences() ;
int count = spineList.size();
int start = 0;
StringBuilder string = new StringBuilder();
for (int i = start; count > i; i = i +1) {
res = spine.getResource(i);
try {
InputStream is = res.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((line = reader.readLine()) != null) {
linez = string.append(line + "\n").toString();
}
} catch (IOException e) {e.printStackTrace();}
} catch (IOException e) {
e.printStackTrace();
}
}
return linez;
}
I found a solution for reading epub books in android using epublib. I am able to read the subtitles of the book. But I didn't find a way to read the line by line of the content. How can I acheive this?
Sample code for getting titles of the book is
private void logTableOfContents(List<TOCReference> tocReferences, int depth) {
if (tocReferences == null) {
return;
}
for (TOCReference tocReference : tocReferences) {
StringBuilder tocString = new StringBuilder();
StringBuilder tocHref=new StringBuilder();
for (int i = 0; i < depth; i++) {
tocString.append("\t");
tocHref.append("\t");
}
tocString.append(tocReference.getTitle());
tocHref.append(tocReference.getCompleteHref());
Log.e("Sub Titles", tocString.toString());
Log.e("Complete href",tocHref.toString());
//logTableOfContents(tocReference.getChildren(), depth + 1);
}
}
Got this code from http://www.siegmann.nl/epublib/android
How can I get the story of the book...
I'm not sure is that is the way to navigate in epub file. As far as I know (till now - I'm still learning), better way to get all book cocntent is based on spine section.
But still - I don't know how to connect this two things (TOC and real spine) with epublib interface.
According to documentation:
"The spine sections are the sections of the book in the order in which the book should be read. This contrasts with the Table of Contents sections which is an index into the Book's sections."
that is something - if You likie - this is a snippet:
Spine spine = new Spine(book.getTableOfContents());
for (SpineReference bookSection : spine.getSpineReferences()) {
Resource res = bookSection.getResource();
try {
InputStream is = res.getInputStream();
//do something with stream
} catch (IOException e) {
Well - i'm not exacly sure about navigating, but also wonder how to do it
For now - i have something like this (it is line - by line read):
private void logTableOfContents(List<TOCReference> tocReferences, int depth) {
if (tocReferences == null) {
return;
}
for (TOCReference tocReference : tocReferences) {
StringBuilder tocString = new StringBuilder();
for (int i = 0; i < depth; i++) {
tocString.append("\t");
}
try{
InputStream is = tocReference.getResource().getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = r.readLine()) != null) {
String line = Html.fromHtml(line).toString();
}
}
catch(IOException e){
}
//logTableOfContents(tocReference.getChildren(), depth + 1);
}
}