views:

64

answers:

4

Hi All, I want to do search and replace with the help of java.

I have TreeMap that contains file names and its contents. I have one another TreeMap that contains word and its meaning.

I want to search file contents and replace found word with its meaning.

Thanks

P.S: It is not an assignment. I used simple reading file and replace function. However, I need speedy way to do it. Any sample code or ref. will be appreciated.

A: 

The basics could be:

  • loop over the entrySet
  • for each entry get the value
  • Split that value into words
  • for each word lookup that word in the dictionary
  • if found add that word to your translated stringbuilder
  • otherwise add the original
  • add space or so
  • setvalue on the map entry with stringbuilder.toString
extraneon
is it good speed wise?
@user431276 speed is not the issue, memory consumption is. Keeping file contents in the map is not that good. Better load and save in the loop over the files, and use a Set for the files in stead of a map. I also placed a comment on the winning answer about the word substitution.
extraneon
+1  A: 

Let me give you some general development advice that helps when a problem is overwhelming.

First of all, write down a list--an algorithm that you think will solve the problem. In your case try:

  • for each file in treemap 1
  • for each entry in that file
  • for each line in the current file
  • search for each term in the second treemap
  • for each found term, replace with the definition.

Now, go into each one of those steps and enhance it. For the first one you should

  • Iterate over the list of files in treemap 1
  • open the next file
  • start reading the contents

Do this for each step that you can't currently envision "Just Coding" until you can see just how you would do each step.

Once you are done with this, I suggest writing a Very Basic application that does one thing from your list--for instance, open a file. It's very helpful here to write a junit test or write a test in your main. Make sure that it opens the file and can read from it.

Once you are at this point, keep adding to your code--just add a little bit then recompile/retest. Retest every single line until you have coded for like 5 years, then go to every 2 or 3 lines.

Keep updating your tests to test your code. If you break an old test rewrite it, don't throw it away.

Iterate.

I hope this helps.

Bill K
Your answer reminds me my childhood (college days) when they used to teach us such *basic* programming things :) anyway thanks
hehe, probably the way you asked suggested someone learning *basic* programming
Carlos Heuberger
Kinda sounded like a homework problem to me--Sorry I misunderstood.
Bill K
A: 

This example will work for all classes implementing the map interface, not just a TreeMap:

public void searchAndReplace(Map<File, byte[]> fileToContentMap, Map<String, String> wordToDefinitionMap) {

    // for each file
    for (File f : fileToContentMap.keySet()) { 
        // get the file's contents
        String contents = new String(fileToContentMap.get(f));

        // replace the contents with definitions
        for (String word : wordToDefinitionMap.keySet()) {
          contents = contents.replaceAll(word, wordToDefinitionMap.get(word));   
        }

        // make sure to update the contents in the array
        fileToContentMap.put(f, contents.getBytes());

        // note, remember to update the physical file as well

    }
}
rob
Thanks for your example. It gave me idea and I solved my problem :)
The replaceAll has an issue with combined words, if you have a definition foo=something bar=blah and a word foobar you get somethingblah. I think you should split to words and do an equals with the definition keys to prevent that.
extraneon
Yes but "foobar" does not equal "foo" and it does not equal "bar", so it should not be replaced.
rob
A: 

I would not use a TreeMap to store the contents of all files+.
You need to read the contents anyway, so there is no advantage in first storing all of them+.
Read one file a time, process it and save it.

Have a look at the NIO package of Java. On the NIO examples page there is an example for searching in a list of files (Grep.java).

+ if not needed {elsewhere}

Carlos Heuberger