11 Replies Latest reply: Jun 12, 2007 8:55 AM by 807605 RSS

    call one file to open and read several others

    807605
      hi, this code that i have can open a file and find all its three word combinations and count them. i would like it to open a file called "Nytt" where i (will) have 48 text files stored and count all three word combinations in each file and present the result from each file... eg

      as far as:
      smith: 13
      johnson: 2
      brown: 35
      ...

      once upon a:
      smith: 1
      johnson: 43
      brown: 0

      when i write
       public static void main(String... args) throws Exception {
                findIdioms(new File("Nytt.java"));
      it doesn't open the files in "Nytt.java" but reads "Nytt" itself.

      these are the two codes:
      public class GetStrings2 {
           public static void main(String... args) throws Exception {
                findIdioms(new File("Nytt.java"));
           }
       
           public static void findIdioms(File file) throws Exception {
                Scanner sc = new Scanner(file);
                Pattern p = Pattern.compile("\\b(\\w+)\\s+(?=(\\w+)\\s+(\\w+)\\b)");
       
                Map<String, Integer> counts = new TreeMap<String, Integer>();
                String idiom = null;
                int maxLen = 0;
       
                while (sc.findWithinHorizon(p, 0) != null) {
                     MatchResult m = sc.match();
                     idiom = m.group(1) + " " + m.group(2) + " " + m.group(3);
                     if (counts.get(idiom) == null) {
                          counts.put(idiom, 1);
                     } else {
                          counts.put(idiom, counts.get(idiom) + 1);
                     }
                     maxLen = idiom.length() > maxLen ? idiom.length() : maxLen;
                }
       
                String spec = "%-" + (maxLen + 2) + "s%d%n";
                for (String key : counts.keySet()) {
                     System.out.printf(spec, key, counts.get(key));
      
                }
           }
       
      }
      that opens and reads from:
      public class Nytt {
           public static void main(String[] args) throws Exception {     
       
                File[] corpus = {
                     new File("Test.txt"),
                     new File("Hej.txt")
                }; //here is where i'll add the other 46 files
                     
                for(int i = 0; i < corpus.length; i++) {
                     GetStrings.findIdioms(corpus);
                }
           }
      }


      anyone who knows? i'm grateful for all replies!
        • 1. Re: call one file to open and read several others
          807605
          about a week ago, i got help with this code:
          public class Foo
          {
               public static void main( String[] args ) {
                    File[] files = new File[48];
                    for (int i = 0; i < files.length; i++) {
                         files[i] = new File("Test" + (i + 1) + ".txt");
                    }
                    System.out.println(Arrays.toString(files));
                    for (File f : files) {
                         doStuffWithFile(f);
                    }
               }
           
               private static void doStuffWithFile(File f) {
                    try {
                         Scanner sc = new Scanner(f);
                         // now use sc to do whatever you want
                         // e.g. to find idioms
                    } catch (FileNotFoundException e) {
                         System.err.println("File not found");
                         e.printStackTrace();
                    }
               }
          }
          it works wonderfully but it's quite important that i can have different names on the files to (more than 1, 2, 3...) so that i can keep them apart.

          thanks again
          • 2. Re: call one file to open and read several others
            791266
            hi, this code that i have can open a file and find
            all its three word combinations and count them. i
            would like it to open a file called "Nytt" where i
            (will) have 48 text files stored and count all three
            word combinations in each file and present the result
            from each file... eg
            What is it that you want to do?

            Do you want to open a file that contains filenames, read each filename and process that file?
            • 3. Re: call one file to open and read several others
              807605
              exactely!

              and i forgot to say, but the files will be called things like smith.txt, johnson.txt and brown.txt
              • 4. Re: call one file to open and read several others
                791266
                exactely!

                and i forgot to say, but the files will be called
                things like smith.txt, johnson.txt and brown.txt
                The names/types doesn't matter.

                This is what you need to do.

                Place each filename on a separate line in the main file (nytt.txt?).

                1) Open the main file using a BufferedReader or Scanner.
                2) Read a line,
                3) create a File object with the line as argument.
                4) call findIdioms on the file.
                5) repeat step 2-4 as long as there are more lines in the main file.

                Kaj
                • 5. Re: call one file to open and read several others
                  807605
                  correct me if i'm wrong but 2) is ReadLine, right? i used readline in previous versions of this code and learned that it breaks when there's a line break so that if it says
                  one two three four five six seven
                  eight nine ten eleven twelve
                  the outcome will be:
                  one two three
                  two three four
                  ...
                  five six seven
                  eight nine twelve
                  ie it skips "six seven eight" and "seven eight nine".

                  obviously i can't fix this myself so i really am grateful for all answers. ...but i'm hoping the code is almost correct as it is (because i know there won't be any unpleasant surprises) and that i could change for example
                  findIdioms(new File("NyttB.java"))
                  to something like
                  findIdioms(new File("NyttB.java"<"File [] corpus"))
                  or... what could also work is to add the 48 files to the source code in something like:
                  File[] files = {
                                 new File("smith.txt"), 
                                 new File("johnson.txt") };
                            for(int i = 0; i < corpus.length; i++) {
                                 findIdioms(files, corpus);
                            }
                       }
                  • 6. Re: call one file to open and read several others
                    791266
                    correct me if i'm wrong but 2) is ReadLine, right? i
                    used readline in previous versions of this code and
                    learned that it breaks when there's a line break so
                    that if it says
                    one two three four five six seven
                    eight nine ten eleven twelve
                    the outcome will be:
                    one two three
                    two three four
                    ...
                    five six seven
                    eight nine twelve
                    ie it skips "six seven eight" and "seven eight
                    nine".
                    I don't know what you mean by that. Yes, you should use readLine. It will read one line, that is "one two three four five six seven" and "eight nine ten eleven twelve" in your example above.

                    Why would it skip any values?

                    Kaj
                    • 7. Re: call one file to open and read several others
                      807605
                      it did so last time, i don't know why.

                      it really seems as easy as adding a "<" for it to open "corpus" in "NyttB"
                      findIdioms(new File("NyttB.java"<"File [] corpus"))
                      or just writing the a txt files within braces in the source file (and having one file instead of two)
                      File[] files = {
                                     new File("smith.txt"), 
                                     new File("johnson.txt") };
                                for(int i = 0; i < corpus.length; i++) {
                                     findIdioms(files, corpus);
                                }
                           }

                      i don't know where to read about this.
                      • 9. Re: call one file to open and read several others
                        800322
                        What is the current question?
                        • 10. Re: call one file to open and read several others
                          807605
                          What is the current question?
                          the current question is: i have these two files, "GetStrings2" and "Nytt". "GetStrings2" finds wordcombinations and counts them. "Nytt" stores the files that i'd like "GetStrings2" to go through.

                          what the current "GetStrings2" does is opening "Nytt" but it doesn't open the files stored in "Nytt".

                          as i see it, i have to options. the first is to find a command that makes "GetStrings2" to open the files in "Nytt" and process them. the second is to store the files in "GetStrings2".
                          i can't find a way to do either but i think it is as easy as adding a "<" or braces (however, i've tried these two without success but i think it should be something that little).

                          thanks for any help. and please just ask if you have any questions.