How to load dictionary from json file

843 views
Skip to first unread message

omer....@gmail.com

unread,
Mar 9, 2019, 10:11:29 AM3/9/19
to DroidScript
Hello
Anyone know how to load a dictionary from json file and use it in my app? If so please help me.

Regards,
Omer

Steve Garman

unread,
Mar 9, 2019, 12:43:51 PM3/9/19
to DroidScript
Perhaps you could explain what you mean by the word dictionary.

Also are you saying you already have a representation of the dictionary in a JSON file?

What do you want to do with the dictionary when you have loaded it.

nikhil babychan

unread,
Mar 9, 2019, 1:41:33 PM3/9/19
to DroidScript
Hi,
You can use this json english word dictionary if you want.
https://raw.githubusercontent.com/matthewreagan/WebstersEnglishDictionary/master/dictionary.json

Then you can parse it by this:

//=============
var dictn = app.ReadFile( "dictionary.json" );
var parsed = JSON.parse(dictn);
var word = "apple";
var meaning = parsed[word];
alert(meaning);

omer....@gmail.com

unread,
Mar 11, 2019, 1:00:14 AM3/11/19
to DroidScript
I want to make a translate app by using dictionary format. I also want that if the user want to add a translation for himself he will be able to make it.

Steve Garman

unread,
Mar 11, 2019, 4:03:00 AM3/11/19
to DroidScript
First a couple of warnings.

1) To use a JSON file for this sort of purpose, the whole dictionary will need to be loaded into memory. If you expect the dictionary to grow to more than a few hundred words, I suggest you consider SQL instead.

2) To translate single words, this model may be fine but don't expect to be able easily to expand it to translate phrases and sentences. That is a very much more complex task and you would almost certainly be better off using a ready-built engine like Google Translate

Having said that, if you still think JSON is right for your purposes you may find some hints on how to get started in the attached spk
lookup.spk

omer....@gmail.com

unread,
Mar 11, 2019, 7:46:49 AM3/11/19
to DroidScript
I want that the user will be able to add a translation only for him.
To do this I need to use a JSON file and every time that the user add a new translation it adds to the dictionary in the JSON file.

How can I load a dictionary from JSON file and how can I use the data from him?

Steve Garman

unread,
Mar 11, 2019, 9:30:14 AM3/11/19
to DroidScript
To use the data from the user with the spk I posted above you could use these functions

function addToWords(word, definition, save)
{
    words
[word]=definition;
   
if(save)
   
{
      writeAsJson
(wordFile,words,true);
   
}
}


//general purpose function to store object in file
function writeAsJson(path, obj, pretty)
{
 
//if pretty==true, add spacing to json
 
var fmt = pretty ? 1 : null;
  app
.WriteFile(path, JSON.stringify(obj, null, fmt));
}


If you need further advice I suggest you start work on your app and post any code you are having trouble with.

Kallikratidas

unread,
Mar 11, 2019, 1:05:03 PM3/11/19
to DroidScript
Well, I created a hole demo for your question.
dictionary.spk

omer....@gmail.com

unread,
Mar 11, 2019, 4:16:45 PM3/11/19
to DroidScript
Thank you guys! You have helped me so much!

Kallikratidas

unread,
Mar 11, 2019, 6:42:35 PM3/11/19
to DroidScript
You are welcome.
That's why we have this group!
Reply all
Reply to author
Forward
0 new messages