This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
[HIVEMALL-122] Added tokenize_cn UDF #91
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5eb8037
Added tokenize_cn UDF, using org.apache.lucene.analysis.cn.smart.Smar…
partyyoung 1f81953
Added usage of tokenize_cn
partyyoung e24c4fc
the user guide: sec <2.3.English/Japanese Text Tokenizer> be renamed …
partyyoung efc3a6d
fixed some typos about NLP module
partyyoung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
nlp/src/main/java/hivemall/nlp/tokenizer/SmartcnUDF.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package hivemall.nlp.tokenizer; | ||
|
||
import hivemall.utils.hadoop.HiveUtils; | ||
import hivemall.utils.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import org.apache.hadoop.hive.ql.exec.Description; | ||
import org.apache.hadoop.hive.ql.exec.UDFArgumentException; | ||
import org.apache.hadoop.hive.ql.metadata.HiveException; | ||
import org.apache.hadoop.hive.ql.udf.UDFType; | ||
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; | ||
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; | ||
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; | ||
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; | ||
import org.apache.hadoop.io.Text; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; | ||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; | ||
import org.apache.lucene.analysis.util.CharArraySet; | ||
|
||
@Description(name = "tokenize_cn", value = "_FUNC_(String line [, const list<string> stopWords])" | ||
+ " - returns tokenized strings in array<string>") | ||
@UDFType(deterministic = true, stateful = false) | ||
public final class SmartcnUDF extends GenericUDF { | ||
|
||
private String[] _stopWordsArray; | ||
|
||
private transient SmartChineseAnalyzer _analyzer; | ||
|
||
@Override | ||
public ObjectInspector initialize(ObjectInspector[] arguments) | ||
throws UDFArgumentException { | ||
final int arglen = arguments.length; | ||
if (arglen < 1 || arglen > 2) { | ||
throw new UDFArgumentException( | ||
"Invalid number of arguments for `tokenize_cn`: " + arglen); | ||
} | ||
|
||
this._stopWordsArray = (arglen >= 2) ? HiveUtils | ||
.getConstStringArray(arguments[1]) : null; | ||
this._analyzer = null; | ||
|
||
return ObjectInspectorFactory | ||
.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector); | ||
} | ||
|
||
@Override | ||
public List<Text> evaluate(DeferredObject[] arguments) throws HiveException { | ||
SmartChineseAnalyzer analyzer = _analyzer; | ||
if (analyzer == null) { | ||
CharArraySet stopwords = stopWords(_stopWordsArray); | ||
analyzer = new SmartChineseAnalyzer(stopwords); | ||
this._analyzer = analyzer; | ||
} | ||
|
||
Object arg0 = arguments[0].get(); | ||
if (arg0 == null) { | ||
return null; | ||
} | ||
String line = arg0.toString(); | ||
|
||
final List<Text> results = new ArrayList<Text>(32); | ||
TokenStream stream = null; | ||
try { | ||
stream = analyzer.tokenStream("", line); | ||
if (stream != null) { | ||
analyzeTokens(stream, results); | ||
} | ||
} catch (IOException e) { | ||
IOUtils.closeQuietly(analyzer); | ||
throw new HiveException(e); | ||
} finally { | ||
IOUtils.closeQuietly(stream); | ||
} | ||
return results; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
IOUtils.closeQuietly(_analyzer); | ||
} | ||
|
||
@Nonnull | ||
private static CharArraySet stopWords(@Nonnull final String[] array) | ||
throws UDFArgumentException { | ||
if (array == null) { | ||
return SmartChineseAnalyzer.getDefaultStopSet(); | ||
} | ||
if (array.length == 0) { | ||
return CharArraySet.EMPTY_SET; | ||
} | ||
CharArraySet results = new CharArraySet(Arrays.asList(array), /* ignoreCase */ | ||
true); | ||
return results; | ||
} | ||
|
||
private static void analyzeTokens(@Nonnull TokenStream stream, | ||
@Nonnull List<Text> results) throws IOException { | ||
// instantiate an attribute placeholder once | ||
CharTermAttribute termAttr = stream | ||
.getAttribute(CharTermAttribute.class); | ||
stream.reset(); | ||
|
||
while (stream.incrementToken()) { | ||
String term = termAttr.toString(); | ||
results.add(new Text(term)); | ||
} | ||
} | ||
|
||
@Override | ||
public String getDisplayString(String[] children) { | ||
return "tokenize_cn(" + Arrays.toString(children) + ')'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
nlp/src/test/java/hivemall/nlp/tokenizer/SmartcnUDFTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package hivemall.nlp.tokenizer; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.hadoop.hive.ql.exec.UDFArgumentException; | ||
import org.apache.hadoop.hive.ql.metadata.HiveException; | ||
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; | ||
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject; | ||
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; | ||
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; | ||
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; | ||
import org.apache.hadoop.io.Text; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class SmartcnUDFTest { | ||
|
||
@Test | ||
public void testOneArgument() throws UDFArgumentException, IOException { | ||
GenericUDF udf = new SmartcnUDF(); | ||
ObjectInspector[] argOIs = new ObjectInspector[1]; | ||
// line | ||
argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector; | ||
udf.initialize(argOIs); | ||
udf.close(); | ||
} | ||
|
||
@Test | ||
public void testTwoArgument() throws UDFArgumentException, IOException { | ||
GenericUDF udf = new SmartcnUDF(); | ||
ObjectInspector[] argOIs = new ObjectInspector[2]; | ||
// line | ||
argOIs[0] = PrimitiveObjectInspectorFactory.javaStringObjectInspector; | ||
// stopWords | ||
argOIs[1] = ObjectInspectorFactory | ||
.getStandardConstantListObjectInspector( | ||
PrimitiveObjectInspectorFactory.javaStringObjectInspector, | ||
null); | ||
udf.initialize(argOIs); | ||
udf.close(); | ||
} | ||
|
||
@Test | ||
public void testEvaluateOneRow() throws IOException, HiveException { | ||
SmartcnUDF udf = new SmartcnUDF(); | ||
ObjectInspector[] argOIs = new ObjectInspector[1]; | ||
// line | ||
argOIs[0] = PrimitiveObjectInspectorFactory.writableStringObjectInspector; | ||
udf.initialize(argOIs); | ||
|
||
DeferredObject[] args = new DeferredObject[1]; | ||
args[0] = new DeferredObject() { | ||
public Text get() throws HiveException { | ||
return new Text( | ||
"Smartcn为Apache2.0协议的开源中文分词系统,Java语言编写,修改的中科院计算所ICTCLAS分词系统。"); | ||
} | ||
|
||
@Override | ||
public void prepare(int arg) throws HiveException { | ||
} | ||
}; | ||
List<Text> tokens = udf.evaluate(args); | ||
Assert.assertNotNull(tokens); | ||
udf.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also update
resources/ddl/define-udfs.td.hql
?