Count words in each sentece in a file

Hi all
i want to ask how can i count words in each sentece in a file??
ie if i have the follwoing sentece
i ate the cake.
today is Sundy.
i went to school 5 day a week.
to have the number as
4
3
8
any ideas??

you could read the file line per line, put the line
in a string and use StringTokenizer to split it into
word and count them. Or you could read file char per
char, increasing the word counter everytime you find
a blank char, when the read char is a newline you
save the old counter and start a new word count for
the new line.That's an option, but a sentence is not ended by a newline. A sentence ends with a full stop/point.
Kaj

Similar Messages

  • Read the first word of each line in a text file

    i need to read the first word of each line of a text file.
    i know of line.split() but not actually sure how i would go about using it
    Any help most appreciated
    Many Thanks
    Ben

    Hi thanks for the reply!
    this is what i tried... and it still doesn't get me the first word of each line!
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.io.*;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import javax.swing.text.*;
    import javax.swing.event.*;
    import java.util.Calendar;
    import java.util.Scanner;
    import java.util.Vector;
    import java.text.SimpleDateFormat;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.io.BufferedReader;
    public class testing {
         Vector progs=new Vector();
         Vector rand=new Vector();
         String[] tmp = new String [2];
         String str;
         String str2="ewerwer";
         String str3;
         public void programTest()
                   try
                             BufferedReader in = new BufferedReader(new FileReader("progList.log"));
                             while ((str = in.readLine()) != null)
                                  progs.add(str);
                        catch(IOException e)
                             System.out.println("cannot read file");
         //////THE ISSUES IS HERE....I WANT TO GET THE FIRST WORD FROM EACH LINE OF THE FILE!!!     
              try
                             BufferedReader in2 = new BufferedReader(new FileReader("eventLog.log"));
                             while ((str = in2.readLine()) != null)
                                  tmp = str.split(" ");
                                  System.out.println(tmp[0]);
                        catch(IOException e)
                             System.out.println("cannot read file");
    public static void main(String[] args)
                 testing B = new testing();
                 B.programTest();
               //  B.fileToVector();
                 //B.LoginWindow();
               //B.anomDetect();
    }//end class

  • How to count words in a PDF file?

    Is there any way I can count words in a PDF file without resorting to Acrobat Reader (which apparently has that feature)?
    That's a massive program, which I actually don't like.
    I need to count words in the PDF file because I write my papers with LaTeX, and they're full of my extensive comments.
    Do you know of any alternative?

    that utility IIRC cannot be found on xpdf (the official Archlinux package) anymore and its part of poppler
    edit: its pdftotext btw
    Last edited by dolby (2008-05-11 13:35:05)

  • Program to read only specific words in each Line in a text file

    Hi
    I have a question
    I need to write a program where the program must read only specific words in each line
    I will give you an example...
    PSAPPSRV.21201      1-42 13.35.54 238.360 Cur#1.HDEV RC=0 Dur=0.000 COM Stmt=SELECT VERSION FROM PSVERSION WHERE OBJECTTYPENAME = 'SYS'
    PSAPPSRV.21201      1-43 13.35.54 0.040 Cur#2.HDEV RC=0 Dur=0.000 COM Stmt=SELECT STYLESHEETNAME FROM PSOPTIONS
    These are two lines in my text file...now I need to read only the SQL statements present that is both SELECT statements.. Can you please suggest a method.......

    My first reaction to the question is why would you want such thing, but may be I am unknown :)
    Assuming you have the text as string, I mean
    String str = "PSAPPSRV.21201 1-42 13.35.54 238.360 Cur#1.HDEV RC=0 Dur=0.000 COM Stmt=SELECT VERSION FROM PSVERSION WHERE OBJECTTYPENAME = \'SYS\'";you can obtain the sql statement using substring method like
    String result = str.substring(str.indexOf("SELECT")); Again I assume there is no word SELECT preceding and the word SELECT is all caps.

  • Select first word in each line of a text file to do something.

    How do I select the first word in each line to do something newer from a text file in Powershell?
    Example:
     GBL                            Primary    File            
     local                          Primary    File            
     localhost                      Primary    File            
     Opstest                        Primary    File            
     TrustAnchors                   Primary    File            
    99upgrade

    Hi 99,
    here's an example on how to do it:
    $lines = Get-Content "C:\ExampleFolder\Example.txt"
    foreach ($line in $lines)
    Write-Host ($line.Split(" "))[0]
    Just replace the Write-Host line in the loop with what you actually want to do with the first word in the line.
    Cheers,
    Fred
    There's no place like 127.0.0.1

  • Help with program using hashtable to count words & other chars in txt file

    I need to use only a hashtable to count the occurences words and chars in a text file and display them alphabetically. I am not to use anything but the hashtable. so far, I can get it to count only the words in the file and not the chars, I want to know how to make it count the chars (,.;:?(){}[]!@#$%^&\t\"<>/`~ ) that may be found and if it is possible to get it to display them in a sorted (alphabetical) order w/o using anything else.
    This is what I have: mport java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    class words{
    String word;
    int count;
    public class WordCount{
    static Hashtable h=new Hashtable();
    static words w;
    static void countWords(String s){
    if((w=(words)h.get((java.lang.Object)s))==null){
    w=new words();
    w.word=s;
    w.count=1;
    h.put(s,w);
    }else{
    w.count++;
    h.remove(s);
    h.put(s,w);
    public static void main(String args[]){
    String s;
    StringTokenizer st;
    String t;
    String fn = JOptionPane.showInputDialog("Enter the filename:");
    BufferedReader br = null;
    try{
    br = new BufferedReader(new FileReader(fn));
    s=br.readLine();
    while(s!=null){
    st= new StringTokenizer(s, " ,.;:?(){}[]!@#$%^&\t\"<>/`~  ");
    // Split your words.
    while(st.hasMoreTokens()){
    t=st.nextToken();
    countWords(t);
    s=br.readLine();
    }catch(Exception e){
    e.printStackTrace();
    Enumeration e=h.elements();
    w=(words)e.nextElement();
    while(e.hasMoreElements()){
    System.out.println(w.word + " " + w.count);
    w=(words)e.nextElement();
    System.exit(0);
    }

    Please don't crosspost. It cuts down on the effectiveness of responses, leads to people wasting their time answering what others have already answered, makes for difficult discussion, and is generally just annoying and bad form.

  • Counting words in a textfile

    Hello I am trying to make a program that counts the number of words in a textfile specified below - later i will make it so you can choose the textfile. When I try to compile this I get the errors:
    java:23: char cannot be dereferenced
                   if(inWord && character.isWhiteSpace(character))
    ^
    java:29: char cannot be dereferenced
                   else if(!inWord && character.isLetterOrDigit(character))
    Please could someone tell how to get this working - I know its something to do with the format of the data, maybe I should change it to a string or something?
    cheers anyone
    import java.io.*;
    public class NumberOfWords
         public static void main(String[] argStrings)
                final int EOF = -1;
                int count = 0;
                boolean inWord = false;
                FileReader file = new FileReader("TestFile.txt");
                for(int i = file.read(); i != EOF; i = file.read())
                   char character = (char)i;
                   if(inWord && character.isWhiteSpace(character))
                     // we've come to the end of a word, so count it
                     count++;
                     inWord = false;
                   else if(!inWord && character.isLetterOrDigit(character))
                     // we've just started a word or number
                     inWord = true;
                if(inWord)  // count the last word in the file
                   count++;
          System.out.println(count);
    }

    Hello again, I am making great progress as I now have a program where you can specify the file and it does the required function. I would like to modify it to count the number of words on each line. Please could I have some tips on how to do this.
    cheers
    *Write an application which displays the number of words on each
    *line of a text file. Assume one space between words, and no spaces
    *at the start and end of the lines. Test the application with a
    *suitable input file.
    import java.io.*;
    import java.util.Scanner;
    public class NumberOfWords
         public static void main(String[] argStrings)throws Exception
                final int EOF = -1;
                int count = 0;
                boolean inWord = false;
                System.out.println("Enter file for word counting");
                Scanner scan = new Scanner(System.in);
                String inputFile = scan.nextLine();
                FileReader file = new FileReader(inputFile);
                for(int i = file.read(); i != EOF; i = file.read())
                   char myChar = (char)i;
                   if(inWord && Character.isWhitespace(myChar))
                     // we've come to the end of a word, so count it
                     count++;
                     inWord = false;
                   else if(!inWord && Character.isLetterOrDigit(myChar))
                     // we've just started a word or number
                     inWord = true;
                if(inWord)  // count the last word in the file
                   count++;
           System.out.println();
           System.out.println("Number of words: " + count);
    }

  • Tell me Logic for search for duplicate words(or strings) in a large file.

    Search for duplicate words (or strings) in a text file containing one word per line. For each word that occurs more than once in the flat file output should be as follows
    <word> <number of occurrences> <line numbers in the file where the word occurs>
    For example, if the word Hello occurs thrice in a file at lines 100, 178 and 3456 the output should read
    Hello, 3, [100, 178, 3456]

    Incidentally i wrote similar code some days back. You need to do some modifications to get the exact output you want, but i hope it will be of some help.
    One more thing its written using JAVA5
    public class Test
         private static final String COLLECTIONS_TEXT = "C:\\Documents and Settings\\amrainder\\Desktop\\Collections.txt";
         public static void main(String[] args) throws IOException
              findDuplicateWords();
         private static void findDuplicateWords() throws IOException
              Collection<String> words = new LinkedHashSet<String>();
              File file = new File(COLLECTIONS_TEXT);
              StreamTokenizer streamTokenizer = new StreamTokenizer(new FileReader(file));
              int token = streamTokenizer.nextToken();
              while(token != StreamTokenizer.TT_EOF)
                   if(token == StreamTokenizer.TT_WORD)
                        words.add(streamTokenizer.sval);
                   token = streamTokenizer.nextToken();
              System.out.println(words);
    }Cheers,
    Amrainder

  • Does anyone know how to use pages so you can export pdfs from the internet and automatically drag words from the document into the file name of the pdf (i.e., author, title of a scientific paper)

    Does anyone know how to use pages so you can export pdfs from the internet and automatically drag words from the document into the file name of the pdf (i.e., author, title of a scientific paper). For example, if I am downloading a paper by smith called "Surgery" that was published in 2002, it will automatically set the file name in the download to smith- surgery 2002. I have heard pages is smart enough to do this.
    thank you

    Pages can export only its own documents. They may be exported as PDF, MS Word, RTF or Text files.
    Pages can import (ie. Open a file as, or Insert a file into, a Pages document) documents in several formats, but won't rename the document as you describe. Documents that can be Opened (eg. Text, AppleWorks 6 WP, MS Word files) are converted to Pages documents, and retain their original names, with .pages replacing the original file extension. Files that can be Inserted (generally .jpg, .pdf and other image files) become part of the existing Pages file and lose their names.
    It may be possible, using AppleScript, to extract the text you want and to Save a Pages file using that text as the filename, but that would depend in part on being able to identify which text is wanted and which is not.
    How will the script determine where the author's name begins and where it ends?
    How will the script recognize the beginning and of the title, an decide how much of the title to use in the filename?
    How will the script recognize the year of publication?
    For papers published in a specific journal, with a strict format for placing each of these pieces on information, or containing the needed information as searchable meta data in the file, this might be possible. But it would require knowledge of the structure of these files, and would probably handle only papers published in a specific journal or set of journals.
    Outside my field of knowledge, but there are some talented scripters around here who might want to take a closer look.
    Best of luck.
    Regards,
    Barry

  • Using adobe pro 9 to move photos out of jpeg into a word doc and keeping name of file as well

    Hi
    I had an issue at work today and I wanted to move 300 jpeg photos into word.  The file became very large and I query whether adobe pro 9 can reduce size of the picture and also the size of the file before putting it into word.  I also wanted the name of the file to appear.
    Thanks
    marie

    Sorry I may not have been clear.  With adobe pro 9 there is a function to combine files which is what I did.  I selected all the jpegs right hand click to convert to adobe (in windows).  This was sufficient for my purpose but I wanted the name of the file to also appear on each of the jpeg files.  I heard it is possible.
    [auto-quote deleted by host]

  • Counting words in a textArea

    Help !!
    I am trying to count the number of words contained in a text area - Is it possible to do this ?
    I am new to Java and I'm stuck because I only know how to write programs that use FileReader to count the number of words in a file.
    Is it possible to count the number of words in a string directly - or will I have to save the string to file and then apply another program to count the number of words ?

    You inser this code in your source file (TextArea1 is the name of the TextArea, whose you want to count words)
    String texte=TextArea1.getText();
    String rt=String.valueOf((char)13) + String.valueOf((char)10);
    StringTokenizer mots=new StringTokenizer(texte," ,.:;!?\t"+rt);
    int nbremots=mots.countTokens();
    and you place at the header of the file
    import java.util.StringTokenizer;
    nbremots is the number of words The delimitors between words are noticed in the second argument of the constructor of class StringTokenizer. I have choosen the main signes of punctuation like space, coma,stop... the String rt
    symbolise return on Windows. If you work on Unix prefer this code
    String texte=TextArea1.getText();
    StringTokenizer mots=new StringTokenizer(texte," ,.:;!?\t\n");
    int nbremots=mots.countTokens();
    You can also with the class StringTokenizer read the words one by one with this type of boucle
    while (mots.hasMoreTokens())
    String mot=mots.nextToken();
    mots.nextToken();
    It is an useful class. See the API about this class StringTokenizer in the package java.util

  • How to finds specific words in each sentence?

    import java.io.*;
    import java.text.*;
    import java.util.*;
    public class FindingWordsSpecific {
         static String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "every Tuesday"};
      public static void main( String args[] ) throws IOException {
               // the file must be called 'myfile.txt'
               String s = "myfile.txt";
               File f = new File(s);
               if (!f.exists())
                    System.out.println("\'" + s + "\' does not exit. Bye!");
                    return;
               BufferedReader inputFile = new BufferedReader(new FileReader(s));
               String line;
               int nLines = 0;
               while ((line = inputFile.readLine()) != null)
                    nLines++;
                   System.out.println(findTheIndex(line));     
               inputFile.close();
           public static String findTheIndex(String sentence){
                String result = "";
                String[] s = sentence.split("\\s");
              for (String s1: s){
                   for (String s2: days){
                        if (s1.equalsIgnoreCase(s2)) {
                             if(s2.matches("every Tuesday")){
                                             }else if (s2.matches("every Wednesday")){
                                              }What is wrong with it because I tried to find "every Tuesday" in
    myfile.txt: "Go fishing every Tuesday and every Wednesday"
    There is big problem with split statement because it takes each word not more than a word.
    I need to have "every Tuesday" not "Tuesday". How to make it correct codes?

    I am going to give you a picture of how the output will look.
    Here are two sentences from myfile.txt:
    Go fishing every Tuesday and every Wednesday
    Meet with research students on Thursday
    I need to read from myfile.txt and to find specific words in each sentences like this output:
    Every Tuesday : Go fishing
    Every Wednesday : Go fishing
    Thursday : Meet with research students
    Ok. make sense? Now I am trying to figure out how to find specific words in each sentence from myfile.txt.
    That is why I have difficult with the splits statement and loops. Like this:
           public static String findTheIndex(String sentence){
                String result = "";
                String[] s = sentence.split("\\s");
              for (String s1: s){
                   for (String s2: days){
                        if (s1.equalsIgnoreCase(s2)) {
                             if(s2.matches("every Tuesday")){
                             }else if(s2.matches("every Wednesday")){
                             }else if(s2.matches("Thursday")){
                             }else{
                                  System.out.println("That sentence is not working");
                return result;
      }So look at the "Thursday" it is working the output because I have split statement to give me only one word not more than
    a word in sentence. So there is big problem with more than a word in sentence like this "every Tuesday" and it won't work at all because of split. Could you please help me how to do that? I appreciated for that help. Thanks.

  • Making a phrase counter--word scanning

    Hi everybody,
    I'm writing a phrase counter, and so far it's working, but I'm trying to optimize it because running over a for loop is really slowing me down.
    Here's the problem:
    I want to get phrases that repeat more than once, from length 3 words to 10 words. Each time I change the length of the phrase (say, from 10 to 9 to 8, etc down to 3), I have to run over the entire word list all over again, which eventually amounts to 8 total passes over an ArrayList that's almost 45000 elements long right now, and will get longer.
    An added complication in this is that there are certain identifiers in the word list that mark the beginnings of chapters (#) and sentences ($); any time they are hit, the iterator moves to the next word.
    So, basically, something like...
    example example example example example $ # example example example $
    ...with the repeating loop I have now would give:
    0 phrases of length 10, 9, 8, 7, or 6
    1 of length 5
    2 of length 4
    4 of length 3
    Could anyone give me advice?
    Thanks!
    Jezzica85

    Yes, you've got that right. Thank you, I think I'll try that. I won't be able to come back on and tell if it worked until sometime tomorrow probably, but in any case, thanks, I think this will get me where I need to go with a little tweaking. Come to think of it, this might be really fast, especially if I delete an index once it runs into an identifier. Very cool.
    Thanks again,
    Jezzica85
    PS--Oh, and by the way I see you'll have been registered here for two years soon. Congrats!
    Message was edited by:
    jezzica85

  • I have 2 ipods,one for music and the other for old time radio shows.Each had their own file.My computer crashed and I had to buy a new one.Now both ipod files are merged into one.How do I seperate them.

    I have 2 ipods,one for music and the other for old time radio shows.Each had their own file.My computer crashed and I had to buy a new one.Now both ipod files are merged into one.How do I seperate them on the computer?

    Hi Craig
    Unfortunately, in your case, there isn't really a way to separate them as far as I can think.
    You could try restoring from a backup, and choosing an older backup perhaps
    Cheers

  • Counting words in a single cell in Numbers'09

    Hi there,
    I'm relatively new to Mac world, but I do have years of computer experience from a PC and have also had to do with Macs at the age of first eMacs . I have finally decided to switch to the brighter side of life (hopefully ;)).
    But here is my question: I need to count words in a cell in Numbers'09.
    Is there a specific function combination for achieving this? My idea was: strip excessive spaces, count the occurencies of all space character in a cell, add 1 and voila! Problem is I can not achieve it using formulas in Numbers'09. I have found some help for Excell but the formulas are a little different. And well, I would like to leave the past behind and stick to a Apple programs - if I can. I don't like the idea to install Excell on a Windows Bootcamp partion only for this purpose.
    Any help would be greatly appreciated. Thanks.
    Aleksander

    Badunit wrote:
    Yvan once had a list of all the different localizations. He may still have it.
    I'm late but, I was very busy
    The table with every localized functions names is (and will remain) available on my iDisk :
    <http://public.me.com/koenigyvan>
    Download :
    For_iWork:iWork '09:functionsNames.numbers.zip
    An easy soluce for foreign users (like me) is to duplicate Numbers.app and remove its languages resources minus English.
    Running it you will have it running in English (minus the decimal and the parameters separators, minus also date time formats and default currency).
    It would be easy to enter the formulas given in this forum.
    Once saved, we may open the doc in the 'standard' Numbers and the formulas will be automatically localized.
    Yvan KOENIG (VALLAURIS, France) mardi 2 mars 2010 18:30:45

Maybe you are looking for

  • Automator query: I want to batch process with a program that doesn't offer batch processing.

    After much searching and experimenting I'm resorting to asking the direct question to see if you good folks can help me. I would like to be able to automate some image processing with the software "SketchMee". Unfortunately this is not something that

  • Need help! easy question about scrollpane?

    DefaultStyledDocument document = new DefaultStyledDocument(); JTextPane tp = new JTextPane(document); tp.setMargin(new Insets(5,5,5,5)); JScrollPane tpScrollPane = new JScrollPane(); tpScrollPane.setBounds(new Rectangle(5,5,100,200)); tpScrollPane.ge

  • Mountin Lion or Mavericks on Mid 2010

    Hello, I have a MacBook Pro, 13-inch Mid 2010 Processor 2.66  GHz Intel Core 2 Duo Memory 4 GB 1067 MHz DDR3 Graphics NVIDIA GeForce 320M 256 MB I recently installed the latest update OS X 10.9.3, I'm curious is there perhaps by any chance that havin

  • Apple TV and projector 2.35:1

    Hi, I'm about to build a house and I wan't a home theater (projector and a big screen). I just don't know the aspect ratio of the screen. I would prefer 2.35:1 because I watch mostly the movies. However, I'm not sure how the Apple TV works. I have Ap

  • Lightroom 4 Publish Services will not connect to Flickr.

    I have been trying to set up Flickr in Lightroom 4 Publish Services but get an error message when I press "OK, I'll Authorise it" on the Flickr authorisation page.  The message is "The address wasn't understood.   Firefox dosen't know how to open thi