0

I need to load a text file of information into Java. The Text file looks like this

"reproduce": {
    "VB": 7
}, 
"drill": {
    "VB": 8, 
    "NN": 16
}, 
"subgross": {
    "JJ": 2
}, 
"campsites": {
    "NNS-HL": 1, 
    "NNS": 1
}, 
"streamed": {
    "VBN": 1, 
    "VBD": 2
}

It is basically a huge collection of words with some tags included. I need to save this information in some sort of Java data-structure so that the program can search and retrieve tag statistics for a given word.

From what I've read, using a type of HashMap would be the best idea? Something like:

Map<KeyType, List<ValueType>>

Is that a good idea? How would I go about scanning this data from the text file? I could probably find a way to print the dictionary to the text file that would be easier to scan into Java.

Nate Cook3
  • 586
  • 1
  • 7
  • 19
  • 2
    Your data looks an awful lot like [JSON](https://en.wikipedia.org/wiki/JSON) (but not exactly because there isn't a single root node). You can parse it in java with a library such as [GSON](https://github.com/google/gson) or [Jackson](https://github.com/FasterXML/jackson). – Asaph Jun 10 '15 at 19:52

1 Answers1

1

While your input does not look exactly like JSON, you might be able to preprocess[1] it in a simple way to make it valid JSON. Because JSON is probably much more widespread and therefore better supported than your custom format.


If your problem then is JSON deserialization, then take a look at Jackson or Gson, which will convert your input string into objects.

Simple example in Jackson:

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
Map<String,Object> data = mapper.readValue(new File("file.json"), Map.class);
// process data further here ...

Both Jackson and Gson have a lot of options and can handle complex inputs in various ways, e.g. they can serialize and deserialize from and to Maps, custom Objects, can handle polymorphism (mapping different inputs to objects of different classes) and more.


Given the input, that is currently in your question, you can simply prepend and append a curly bracket, and you would have valid JSON:

{
  "reproduce": {
    "VB": 7
  },
  "drill": {
    "VB": 8,
    "NN": 16
  },
  "subgross": {
    "JJ": 2
  },
  "campsites": {
    "NNS-HL": 1,
    "NNS": 1
  },
  "streamed": {
    "VBN": 1,
    "VBD": 2
  }
}
miku
  • 181,842
  • 47
  • 306
  • 310
  • The text the OP posted isn't valid JSON because it doesn't contain exactly 1 root node. – Asaph Jun 10 '15 at 19:55
  • I've been trying to install Jackson since you posted. I've got the jar file in my reference library for my project, but I can't import ObjectMapper – Nate Cook3 Jun 10 '15 at 20:28
  • @NateCook3, sorry to hear that. Usually, if you are working with maven projects and an IDE like eclipse or NetBeans, it's mostly a matter of adding a dependency and you are good to go. This [question](http://stackoverflow.com/q/18429468/89391) seems related. – miku Jun 10 '15 at 20:37
  • What does the import statement look like for you to use ObjectMapper? – Nate Cook3 Jun 10 '15 at 20:55
  • Probably [com.fasterxml.jackson.databind.ObjectMapper](http://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ObjectMapper.html)? – miku Jun 10 '15 at 20:57
  • That's what I'm using, but I keep getting an error loading the file. (No such file or directory) Map data = mapper.readValue(new File("/json_corpus.json"), Map.class); – Nate Cook3 Jun 10 '15 at 21:23
  • @NateCook3, I'm sorry, it's hard to debug remotely - but keep trying :) – miku Jun 10 '15 at 22:43