Skip to content
Snippets Groups Projects
Commit fa6e5ae0 authored by Nils Christian Ehmke's avatar Nils Christian Ehmke
Browse files

Split the logic of the ToLowerCase filter into two stages

parent 86f8b94d
No related branches found
No related tags found
No related merge requests found
...@@ -20,12 +20,10 @@ import teetime.framework.OutputPort; ...@@ -20,12 +20,10 @@ import teetime.framework.OutputPort;
/** /**
* Receives a string and passes it on to the next stage only with lower case letters. * Receives a string and passes it on to the next stage only with lower case letters.
* Punctuation and similar characters will be removed. Only [a-zA-Z ] will be passed on.
* *
* @since 1.1 * @since 1.1
* *
* @author Nelson Tavares de Sousa * @author Nelson Tavares de Sousa
*
*/ */
public final class ToLowerCase extends AbstractConsumerStage<String> { public final class ToLowerCase extends AbstractConsumerStage<String> {
...@@ -33,12 +31,12 @@ public final class ToLowerCase extends AbstractConsumerStage<String> { ...@@ -33,12 +31,12 @@ public final class ToLowerCase extends AbstractConsumerStage<String> {
@Override @Override
protected void execute(final String element) { protected void execute(final String element) {
outputPort.send(element.replaceAll("[^a-zA-Z ]", "").toLowerCase()); this.outputPort.send(element.toLowerCase());
} }
public OutputPort<String> getOutputPort() { public OutputPort<String> getOutputPort() {
return outputPort; return this.outputPort;
} }
} }
...@@ -43,29 +43,32 @@ public final class WordCounter extends CompositeStage { ...@@ -43,29 +43,32 @@ public final class WordCounter extends CompositeStage {
// The connection of the different stages is realized within the construction of a instance of this class. // The connection of the different stages is realized within the construction of a instance of this class.
public WordCounter() { public WordCounter() {
lastStages.add(mapCounter); this.lastStages.add(this.mapCounter);
ToLowerCase toLowerCase = new ToLowerCase(); final ToLowerCase toLowerCase = new ToLowerCase();
connectStages(tokenizer.getOutputPort(), toLowerCase.getInputPort()); final WordcharacterFilter wordcharacterFilter = new WordcharacterFilter();
connectStages(toLowerCase.getOutputPort(), mapCounter.getInputPort());
connectStages(this.tokenizer.getOutputPort(), toLowerCase.getInputPort());
connectStages(toLowerCase.getOutputPort(), wordcharacterFilter.getInputPort());
connectStages(wordcharacterFilter.getOutputPort(), this.mapCounter.getInputPort());
} }
@Override @Override
protected Stage getFirstStage() { protected Stage getFirstStage() {
return tokenizer; return this.tokenizer;
} }
@Override @Override
protected Collection<? extends Stage> getLastStages() { protected Collection<? extends Stage> getLastStages() {
return lastStages; return this.lastStages;
} }
public InputPort<String> getInputPort() { public InputPort<String> getInputPort() {
return tokenizer.getInputPort(); return this.tokenizer.getInputPort();
} }
public OutputPort<CountingMap<String>> getOutputPort() { public OutputPort<CountingMap<String>> getOutputPort() {
return mapCounter.getOutputPort(); return this.mapCounter.getOutputPort();
} }
} }
/**
* Copyright (C) 2015 TeeTime (http://teetime.sourceforge.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package teetime.stage.string;
import teetime.framework.AbstractConsumerStage;
import teetime.framework.OutputPort;
/**
* Receives a string and passes it with removed punctuation and similar characters on to the next stage. Only [a-zA-Z ] will be passed on.
*
* @since 1.1
*
* @author Nelson Tavares de Sousa
*
*/
public final class WordcharacterFilter extends AbstractConsumerStage<String> {
private final OutputPort<String> outputPort = this.createOutputPort();
@Override
protected void execute(final String element) {
this.outputPort.send(element.replaceAll("[^a-zA-Z ]", ""));
}
public OutputPort<String> getOutputPort() {
return this.outputPort;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment