DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2005
    Posts
    6

    Talking interesting problem

    Hi all, It is a very interesting java problem , lets see if anyone can solve
    make a program to count the occurance of words in a paragraph,the first and last word in a sentence should not be counted.

    the out put should be displayed as
    foreg:- He is a brilliant programmer.His brilliant technique is very useful

    brilliant-2
    is-2
    a-1
    very-1

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Been there, done that, two ways
    Although i never took the job to snip off first-last words in sentences, but I mean
    seriously, I'm not Einstein
    eschew obfuscation

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here is the kernel....

    Anybody for snipping of first-last sentence word ?

    Code:
    /**
     * Word occurrence count
     * @author sjalle
     */
    import java.util.*;
    import java.io.*;
    
    public class WordCount {
      private String inputString=null;
      private int high;
      private String highWord=null;
      public WordCount(String inputString) {
        this.inputString=inputString;
      }
      public int getHighestOccurrence() {
        return high;
      }
      public String getMostFrequentWord() {
        return highWord;
      }
      public void count () {
        //Split the input string into a word array
        String [] words=inputString.split(" ");
    
        //make a hashtable for counting:
        Hashtable ht=new Hashtable();
    
        //loop over the word array and sum up word occurrences, storing the
        //word count in the hashtable:
    
        for (int i = 0; i < words.length; i++) {
          Integer tot = (Integer) ht.get(words[i]);
          if (tot == null) {
            tot = new Integer(1);
            ht.put(words[i], tot);
          }
          else {
            Integer newTot = new Integer(tot.intValue() + 1);
            ht.put(words[i], newTot);
          }
        }
    
        //get highest occurrence
    
        Enumeration e = ht.keys();
        high=Integer.MIN_VALUE;
        highWord="";
        while (e.hasMoreElements()) {
          Object key = e.nextElement();
          int cnt=((Integer)ht.get(key)).intValue();
          if (cnt > high) {
            high=cnt;
            highWord=(String)key;
          }
        }
      }
      public static void main(String[] args) {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        System.out.println("Enter lines of text (blank to finish)");
    
        String aLine = null;
        try {
          aLine = br.readLine().trim();
          while (aLine.length() > 0) {
            sb.append(aLine);
            aLine = br.readLine().trim();
          }
        }
        catch (IOException ex) {
          System.out.println("An Error ocurred");
          ex.printStackTrace();
        }
        WordCount wc = new WordCount(sb.toString());
        wc.count();
        System.out.println("Most frequent word is:" + wc.getMostFrequentWord() +
                           ", it occurred " + wc.getHighestOccurrence() + " times");
    
      }
    
    }
    eschew obfuscation

  4. #4
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    Without having read the code:
    what about "technique-1"? Did I miss something?
    : Beware of bugs in the above code - I have only proved it correct, not tried it.

  5. #5
    Join Date
    May 2005
    Posts
    6
    but does it includes the resolution for the first and the last ? if yes then please explain the logic

  6. #6
    Join Date
    May 2005
    Posts
    6
    appreciable effort sjalle.....let me tell u an interesting fact ...this is the question from one of the interviews I had given at a fresher level for dveloper profile

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Quote Originally Posted by meisl
    Without having read the code:
    what about "technique-1"? Did I miss something?
    Hard to tell, I'm not sure what you mean, but I can't see any other
    sensible way of doing this. A word count of Shakespeare's collected
    work would require some memory administration but the basics remain.
    eschew obfuscation

  8. #8
    Join Date
    Sep 2006
    Posts
    1

    Red face get a summery of a text

    hello all , I have this problem guys needs help please , I saw the program who count the words of a text , which will be the first step of what I need , the issue is that we have a text , need a summry of it as foloows \

    1. step1 , count all the words of the text
    2. step 2 neglect the too low frequency , and the too high frequecy
    (such as a , the ,)
    3. step 3 the words that are not neglected are the significant word(S.N) , the word with too low or too high are non significant word(NSW) .
    4. step 4 take a sentence or a block from the text depending on the number of NSW lets say for every 5 NSW ( e.g. “ [ * - * * - - * - - * ] and calculate the Significant factor (SF = number of Squre SW /sum of SW ans NSW ) ,,SF for the above block =(5*5) /10 = 2.5
    5. return the block or the sentence which has the SF more than a cuttoff or number we choose .


    Thanks guys

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links