Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30836 > unrolled thread
| Started by | elodie.gillain@gmail.com |
|---|---|
| First post | 2015-01-24 09:21 -0800 |
| Last post | 2015-01-24 20:13 -0500 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
remove question marks and exclamation points elodie.gillain@gmail.com - 2015-01-24 09:21 -0800
Re: remove question marks and exclamation points Arne Vajhøj <arne@vajhoej.dk> - 2015-01-24 12:58 -0500
Re: remove question marks and exclamation points Arne Vajhøj <arne@vajhoej.dk> - 2015-01-24 12:59 -0500
Re: remove question marks and exclamation points Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-24 13:39 -0500
Re: remove question marks and exclamation points elodie.gillain@gmail.com - 2015-01-24 11:36 -0800
Re: remove question marks and exclamation points Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-24 20:13 -0500
| From | elodie.gillain@gmail.com |
|---|---|
| Date | 2015-01-24 09:21 -0800 |
| Subject | remove question marks and exclamation points |
| Message-ID | <68b0cd7e-0dff-4e0f-9007-34ed5fd3d08f@googlegroups.com> |
Hi everyone,
I wrote code to remove question marks and exclamation points from some text. However, one can find those in my output file. Interesting enough, the periods (full stops) were removed. Can anybody help me figure this out?
import java.util.Scanner;
public class TextTokenizer {
private Scanner scanner;
private String[] removableCharacters = { ".", "?", "!", ":", ";", "\"", "'" };
public TextTokenizer(char[] texts) {
scanner = new Scanner(new String(texts));
}
// Get the next word
public char[] nextWord() {
while(scanner.hasNext()) {
String word = scanner.next().trim();
if(word.isEmpty()) {
continue;
}
for(String character : removableCharacters) {
if(word.startsWith(character) || word.endsWith(character)) {
word = word.replace(character, "");
}
}
return word.toCharArray();
}
return null;
}
}
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-01-24 12:58 -0500 |
| Message-ID | <54c3dd43$0$289$14726298@news.sunsite.dk> |
| In reply to | #30836 |
On 1/24/2015 12:21 PM, elodie.gillain@gmail.com wrote:
> I wrote code to remove question marks and exclamation points from some text. However, one can find those in my output file. Interesting enough, the periods (full stops) were removed. Can anybody help me figure this out?
>
>
> import java.util.Scanner;
>
>
> public class TextTokenizer {
> private Scanner scanner;
> private String[] removableCharacters = { ".", "?", "!", ":", ";", "\"", "'" };
>
>
> public TextTokenizer(char[] texts) {
> scanner = new Scanner(new String(texts));
> }
>
> // Get the next word
> public char[] nextWord() {
>
> while(scanner.hasNext()) {
> String word = scanner.next().trim();
>
> if(word.isEmpty()) {
> continue;
> }
>
> for(String character : removableCharacters) {
> if(word.startsWith(character) || word.endsWith(character)) {
> word = word.replace(character, "");
> }
> }
>
> return word.toCharArray();
> }
>
> return null;
> }
> }
You remove chars in removableCharacters from all words (per Scanner
default config that may or may not do what you want) if and only of the
word either starts or end with such a character.
I find it difficult to see how the question of removing all ! and ? from
a text and that algorithm should be compatible.
Arne
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-01-24 12:59 -0500 |
| Message-ID | <54c3dd95$0$289$14726298@news.sunsite.dk> |
| In reply to | #30837 |
On 1/24/2015 12:58 PM, Arne Vajhøj wrote:
> On 1/24/2015 12:21 PM, elodie.gillain@gmail.com wrote:
>> I wrote code to remove question marks and exclamation points from some
>> text. However, one can find those in my output file. Interesting
>> enough, the periods (full stops) were removed. Can anybody help me
>> figure this out?
>>
>>
>> import java.util.Scanner;
>>
>>
>> public class TextTokenizer {
>> private Scanner scanner;
>> private String[] removableCharacters = { ".", "?", "!", ":", ";",
>> "\"", "'" };
>>
>>
>> public TextTokenizer(char[] texts) {
>> scanner = new Scanner(new String(texts));
>> }
>>
>> // Get the next word
>> public char[] nextWord() {
>>
>> while(scanner.hasNext()) {
>> String word = scanner.next().trim();
>>
>> if(word.isEmpty()) {
>> continue;
>> }
>>
>> for(String character : removableCharacters) {
>> if(word.startsWith(character) ||
>> word.endsWith(character)) {
>> word = word.replace(character, "");
>> }
>> }
>>
>> return word.toCharArray();
>> }
>>
>> return null;
>> }
>> }
>
> You remove chars in removableCharacters from all words (per Scanner
> default config that may or may not do what you want) if and only of the
> word either starts or end with such a character.
>
> I find it difficult to see how the question of removing all ! and ? from
> a text and that algorithm should be compatible.
If you just want to remove all ! and ? then try:
s.replaceAll("[!?]", "")
Arne
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-01-24 13:39 -0500 |
| Message-ID | <ma0osm$doj$1@dont-email.me> |
| In reply to | #30836 |
On 1/24/2015 12:21 PM, elodie.gillain@gmail.com wrote:
> Hi everyone,
>
> I wrote code to remove question marks and exclamation points from some text. However, one can find those in my output file. Interesting enough, the periods (full stops) were removed. Can anybody help me figure this out?
> [... code snipped; see up-thread ...]
Consider what your code does with an input line like
"What?" she exclaimed. "It wasn't his to begin with!"
You'll get the "word" sequence
"What?"
she
exclaimed.
"It
wasn't
his
to
begin
with!"
... from which you'll obtain
What?
she
exclaimed
It
wasn't
his
to
begin
with!
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | elodie.gillain@gmail.com |
|---|---|
| Date | 2015-01-24 11:36 -0800 |
| Message-ID | <97d9d1ef-2d15-4d22-a4fe-e42d07e6359d@googlegroups.com> |
| In reply to | #30836 |
Thanks for the help. I can fix the problem now.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-01-24 20:13 -0500 |
| Message-ID | <ma1fun$9i4$1@dont-email.me> |
| In reply to | #30836 |
On 1/24/2015 7:36 PM, Stefan Ram wrote:
> elodie.gillain@gmail.com writes:
>> I wrote code to remove question marks and exclamation points from some text.
>>[...]
> Also, it is always best to post an SSCCE.
Um, er, he did.
Well, very nearly. He omitted "import java.util.Scanner;" and
he didn't exhibit a main() method to drive his code, but other than
that I'd say what he showed us qualifies as an SSCCE. It's surely
close enough not to merit a demerit. It's *definitely* enough to
allow an attentive reader to spot the problem.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web