Detecting start of sentence.

Thread Solved
Reply

Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Detecting start of sentence.

 
0
  #1
Dec 9th, 2008
Once again, I can get part of my code to work, but not all. When the user inputs more than one sentence (ending with either a period, question mark or exclamation point), each sentence should be capitalized accordingly. However, it only works with the first sentence to work. Does anyone have any suggestions on how to correct this? Thx in advance.
  1. import javax.swing.JOptionPane;
  2. public class sentenceStructure
  3. {
  4. public static void main(String[] args)
  5. {
  6. //Get user input
  7. String s = JOptionPane.showInputDialog(null, "Enter more than one sentence.");
  8. String[] sarray = s.split("'.','?','!'");
  9.  
  10. for (String begin: sarray)
  11. {
  12.  
  13. String capitalize = begin.substring(0, 1).toUpperCase() +
  14. begin.substring(1, begin.length() - 1);
  15.  
  16. //Show input capitalized
  17. JOptionPane.showMessageDialog(null, capitalize);
  18. }
  19.  
  20. System.exit(0);
  21. }
  22. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 9
Reputation: legilimen is an unknown quantity at this point 
Solved Threads: 2
legilimen legilimen is offline Offline
Newbie Poster

Re: Detecting start of sentence.

 
0
  #2
Dec 10th, 2008
Put this line instead of yours

  1. String[] sarray = s.split("[\\.?!]");


Btw '.' is a regular expression which means "match any character"
So use the escape sequence.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,176
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Detecting start of sentence.

 
0
  #3
Dec 10th, 2008
The split() function of the String class accepts a Regular Expression as an argument, You seem to be under the impression that it accepts comma separated characters which is incorrect.
If you want to check more about Regular Expressions in Java, this is the best place.
Also note the double back slashes (\\) mentioned by legilimen, the first back slash tell the Java compiler that the following "\" is not an escape sequence ( like "\n" is used to indicate a new line). So in effect when the regex parser encounters it the expression is reduced to "\.?!", this "\" indicates to the regex parser that the following "." is not a regex quantifier.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Detecting start of sentence.

 
0
  #4
Dec 10th, 2008
Well, any other suggestions?

I tried what you said and get the following:

Input: hi. who are you? [Ok]
Message 1 Output: H [Ok]
Message 2 Output: who are yo
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,176
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Detecting start of sentence.

 
0
  #5
Dec 10th, 2008
To quote the Javadocs this is how substring(int,int) works :-
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex
So this:
  1. begin.substring(1, begin.length() - 1)
Would always skip the last character of your string.

Use this instead :-
  1. begin.substring(1)

Since you want the entire substring till the end.

You need to go through the Java docs of the String class first so that you know how to use these functions. You can check that out here.
Last edited by stephen84s; Dec 10th, 2008 at 3:36 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Detecting start of sentence.

 
0
  #6
Dec 10th, 2008
Am I on the right track with the following?

  1. String[] strarray = s.split("\\p{Space}");
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Detecting start of sentence.

 
0
  #7
Dec 10th, 2008
or, maybe I should be looking at string tokenizer?

Can anyone help?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Detecting start of sentence.

 
0
  #8
Dec 10th, 2008
You have two options:
- If your delimiters are simple single character sequences [. and !] in your case, you can try looping over the entire string and extracting the relevant parts using indexOf and substring
- If your delimiters end up being multi-character sequences you can try to use the hammer of text processing i.e. regular expressions.

A sample implementation:
  1. import java.util.regex.*;
  2.  
  3. public class SentenceTester {
  4.  
  5. public static void main(final String[] args) {
  6. splitTest();
  7. }
  8.  
  9. // Implement a splitting logic using regex which also works for
  10. // multi line strings.
  11. private static void splitTest() {
  12. String line = "hello there!!!!! how're you doing? " +
  13. "\ni am pretty sure you are doing well.\nright?";
  14.  
  15. // Pattern: Any-of-.?!{1, n} followed by whitespace{0, n}
  16. Pattern pat = Pattern.compile("[.?!]+\\s*", Pattern.DOTALL);
  17.  
  18. // Create matcher instance which will match the given regex with
  19. // the line in consideration.
  20. Matcher mat = pat.matcher(line);
  21.  
  22. int start = 0, end = 0;
  23. while(mat.find()) {
  24. start = end;
  25. // return a `1 based index' into the string where the pattern
  26. // match ends. Hence when `end' is 17 means character at
  27. // position 18 in the string.
  28. end = mat.end();
  29. System.out.println("#" + line.substring(start, end) + "#");
  30. }
  31. }
  32.  
  33. }

> So in effect when the regex parser encounters it the expression is reduced
> to "\.?!", this "\" indicates to the regex parser that the following "." is not a
> regex quantifier.

The quantifiers lose their special meaning when used inside bracketed character classes hence you don't need to escape the DOT; it is treated as a character literal.
Last edited by ~s.o.s~; Dec 10th, 2008 at 1:41 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Detecting start of sentence.

 
0
  #9
Dec 10th, 2008
Can you help me get started with loop for indexOf and substring?

I've tried so many string options in the last couple of days that my head is spinning...

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: Detecting start of sentence.

 
0
  #10
Dec 12th, 2008
Didn't see response
Last edited by clueless101; Dec 12th, 2008 at 12:53 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC