You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by re...@apache.org on 2015/07/29 18:47:24 UTC
svn commit: r1693315 - in /devicemap/trunk/clients/2.0/reference/src:
DeviceMapClient.java Transformer.java TransformerLowerCase.java
TransformerReplaceAll.java TransformerUpperCase.java
Author: rezan
Date: Wed Jul 29 16:47:23 2015
New Revision: 1693315
URL: http://svn.apache.org/r1693315
Log:
transformers
Added:
devicemap/trunk/clients/2.0/reference/src/Transformer.java
devicemap/trunk/clients/2.0/reference/src/TransformerLowerCase.java
devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java
devicemap/trunk/clients/2.0/reference/src/TransformerUpperCase.java
Modified:
devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
Modified: devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java?rev=1693315&r1=1693314&r2=1693315&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java Wed Jul 29 16:47:23 2015
@@ -32,7 +32,7 @@ public class DeviceMapClient {
private String domain;
private String domainVersion;
- private List<String> transformers;
+ private List<Transformer> transformers;
private List<String> tokenSeperators;
private int ngramConcatSize;
@@ -42,7 +42,7 @@ public class DeviceMapClient {
domain = null;
domainVersion = null;
- transformers = new ArrayList<String>();
+ transformers = new ArrayList<Transformer>();
tokenSeperators = new ArrayList<String>();
ngramConcatSize = 1;
@@ -78,13 +78,13 @@ public class DeviceMapClient {
if(get(inputParser, "transformers").isArray()) {
if(patch) {
- transformers = new ArrayList<String>();
+ transformers = new ArrayList<Transformer>();
}
for(Iterator<JsonNode> i = inputParser.get("transformers").iterator(); i.hasNext();) {
JsonNode transformer = i.next();
- transformers.add(transformer.get("type").asText());
+ transformers.add(getTransformer(transformer));
Main.log(" Found transformer: " + transformer.get("type"));
}
@@ -208,12 +208,35 @@ public class DeviceMapClient {
return nullNode;
}
+ public static Transformer getTransformer(JsonNode transformer) throws Exception {
+ String type = get(transformer, "type").asText();
+ JsonNode parameters = get(transformer, "parameters");
+
+ if(type.equals("LowerCase")) {
+ return new TransformerLowerCase();
+ } else if(type.equals("UpperCase")) {
+ return new TransformerUpperCase();
+ } else if(type.equals("ReplaceAll")) {
+ return new TransformerReplaceAll(parameters);
+ }
+
+ throw new Exception("Transformer not found: " + type);
+ }
+
public String classify(String text) {
if(text == null) {
text = "";
}
Main.log(" Classify: '" + text + "'");
+
+ String transformed = text;
+
+ for(Transformer transformer : transformers) {
+ transformed = transformer.transform(transformed);
+ }
+
+ Main.log(" Transformed: '" + transformed + "'");
return "";
}
Added: devicemap/trunk/clients/2.0/reference/src/Transformer.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/Transformer.java?rev=1693315&view=auto
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/Transformer.java (added)
+++ devicemap/trunk/clients/2.0/reference/src/Transformer.java Wed Jul 29 16:47:23 2015
@@ -0,0 +1,22 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+public interface Transformer {
+ public String transform(String input);
+}
Added: devicemap/trunk/clients/2.0/reference/src/TransformerLowerCase.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/TransformerLowerCase.java?rev=1693315&view=auto
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/TransformerLowerCase.java (added)
+++ devicemap/trunk/clients/2.0/reference/src/TransformerLowerCase.java Wed Jul 29 16:47:23 2015
@@ -0,0 +1,25 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+public class TransformerLowerCase implements Transformer {
+ @Override
+ public String transform(String input) {
+ return input.toLowerCase();
+ }
+}
Added: devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java?rev=1693315&view=auto
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java (added)
+++ devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java Wed Jul 29 16:47:23 2015
@@ -0,0 +1,43 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+import org.codehaus.jackson.JsonNode;
+
+public class TransformerReplaceAll implements Transformer {
+ private String find;
+ private String replaceWith;
+
+ public TransformerReplaceAll(JsonNode transformer) throws Exception {
+ if(transformer.get("find") == null || transformer.get("find").asText().isEmpty()) {
+ throw new Exception("ReplaceAll find not defined");
+ }
+
+ if(transformer.get("replaceWith") == null) {
+ throw new Exception("ReplaceAll replaceWith not defined");
+ }
+
+ find = transformer.get("find").asText();
+ replaceWith = transformer.get("replaceWith").asText();
+ }
+
+ @Override
+ public String transform(String input) {
+ return input.replace(find, replaceWith);
+ }
+}
Added: devicemap/trunk/clients/2.0/reference/src/TransformerUpperCase.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/TransformerUpperCase.java?rev=1693315&view=auto
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/TransformerUpperCase.java (added)
+++ devicemap/trunk/clients/2.0/reference/src/TransformerUpperCase.java Wed Jul 29 16:47:23 2015
@@ -0,0 +1,25 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+public class TransformerUpperCase implements Transformer {
+ @Override
+ public String transform(String input) {
+ return input.toUpperCase();
+ }
+}