-
Notifications
You must be signed in to change notification settings - Fork 732
Multicolumns approach #6446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maziyarpanahi
merged 2 commits into
release/333-release-candidate
from
multicolumns-approach
Nov 17, 2021
Merged
Multicolumns approach #6446
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
38 changes: 38 additions & 0 deletions
38
src/main/scala/com/johnsnowlabs/nlp/HasMultipleInputAnnotationCols.scala
This file contains hidden or 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,38 @@ | ||
/* | ||
* Copyright 2017-2021 John Snow Labs | ||
* | ||
* 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 com.johnsnowlabs.nlp | ||
|
||
/** | ||
* Trait used to create annotators with input columns of variable length. | ||
* */ | ||
trait HasMultipleInputAnnotationCols extends HasInputAnnotationCols { | ||
|
||
/** Annotator reference id. The Annotator type is the same for any of the input columns*/ | ||
val inputAnnotatorType: String | ||
|
||
lazy override val inputAnnotatorTypes: Array[String] = getInputCols.map(_ =>inputAnnotatorType) | ||
|
||
/** | ||
* Columns that contain annotations necessary to run this annotator | ||
* AnnotatorType is the same for all input columns in that annotator. | ||
*/ | ||
override def setInputCols(value: Array[String]): this.type = { | ||
set(inputCols, value) | ||
} | ||
|
||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...test/scala/com/johnsnowlabs/nlp/annotators/multipleannotations/MultiAnnotationsSpec.scala
This file contains hidden or 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,52 @@ | ||
package com.johnsnowlabs.nlp.annotators.multipleannotations | ||
|
||
import com.johnsnowlabs.nlp.{DocumentAssembler, LightPipeline, SparkAccessor} | ||
import com.johnsnowlabs.tags.FastTest | ||
import org.apache.spark.ml.Pipeline | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import com.johnsnowlabs.nlp.Annotation | ||
import org.junit.Assert.assertEquals | ||
|
||
class MultiAnnotationsSpec extends AnyFlatSpec { | ||
import SparkAccessor.spark.implicits._ | ||
|
||
"An multiple anootator chunks" should "transform data " taggedAs FastTest in { | ||
val data = SparkAccessor.spark.sparkContext.parallelize(Seq("Example text")).toDS().toDF("text") | ||
|
||
val documentAssembler = new DocumentAssembler() | ||
.setInputCol("text") | ||
.setOutputCol("document") | ||
|
||
val documentAssembler2 = new DocumentAssembler() | ||
.setInputCol("text") | ||
.setOutputCol("document2") | ||
|
||
val documentAssembler3 = new DocumentAssembler() | ||
.setInputCol("text") | ||
.setOutputCol("document3") | ||
|
||
val multipleColumns = new MultiColumnApproach().setInputCols("document","document2","document3").setOutputCol("multiple_document") | ||
|
||
val pipeline = new Pipeline() | ||
.setStages(Array( | ||
documentAssembler, | ||
documentAssembler2, | ||
documentAssembler3, | ||
multipleColumns | ||
)) | ||
|
||
val pipelineModel = pipeline.fit(data) | ||
|
||
val annotations = Annotation.collect(pipelineModel.transform(data),"multiple_document").flatten | ||
assertEquals(annotations.length,3) | ||
|
||
val result = new LightPipeline(pipelineModel).annotate("My document") | ||
|
||
|
||
assertEquals(result("multiple_document").size,3) | ||
|
||
} | ||
|
||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/test/scala/com/johnsnowlabs/nlp/annotators/multipleannotations/MultiColumnApproach.scala
This file contains hidden or 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,34 @@ | ||
package com.johnsnowlabs.nlp.annotators.multipleannotations | ||
|
||
import com.johnsnowlabs.nlp.{AnnotatorApproach, HasMultipleInputAnnotationCols} | ||
import com.johnsnowlabs.nlp.AnnotatorType.{CHUNK, DOCUMENT} | ||
import org.apache.spark.ml.PipelineModel | ||
import org.apache.spark.ml.util.Identifiable | ||
import org.apache.spark.sql.Dataset | ||
|
||
|
||
class MultiColumnApproach(override val uid: String) extends AnnotatorApproach[MultiColumnsModel] with HasMultipleInputAnnotationCols{ | ||
|
||
def this() = this(Identifiable.randomUID("multiplecolums")) | ||
override val description: String = "Example multiple columns" | ||
|
||
/** | ||
* Input annotator types: DOCUMEN | ||
* | ||
*/ | ||
override val outputAnnotatorType: AnnotatorType = DOCUMENT | ||
/** | ||
* Output annotator type:DOCUMENT | ||
* | ||
*/ | ||
override val inputAnnotatorType: AnnotatorType = DOCUMENT | ||
|
||
|
||
|
||
override def train(dataset: Dataset[_], recursivePipeline: Option[PipelineModel]): MultiColumnsModel = { | ||
|
||
new MultiColumnsModel().setInputCols($(inputCols)).setOutputCol($(outputCol)) | ||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/test/scala/com/johnsnowlabs/nlp/annotators/multipleannotations/MultiColumnsModel.scala
This file contains hidden or 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,33 @@ | ||
package com.johnsnowlabs.nlp.annotators.multipleannotations | ||
|
||
import com.johnsnowlabs.nlp.AnnotatorType.{CHUNK, DOCUMENT} | ||
import com.johnsnowlabs.nlp._ | ||
import org.apache.spark.ml.util.Identifiable | ||
|
||
|
||
class MultiColumnsModel(override val uid: String) extends AnnotatorModel[MultiColumnsModel] | ||
with HasMultipleInputAnnotationCols | ||
with HasSimpleAnnotate[MultiColumnsModel]{ | ||
|
||
def this() = this(Identifiable.randomUID("MERGE")) | ||
|
||
/** | ||
* Input annotator types: DOCUMEN | ||
* | ||
*/ | ||
override val outputAnnotatorType: AnnotatorType = DOCUMENT | ||
/** | ||
* Output annotator type:DOCUMENT | ||
* | ||
*/ | ||
override val inputAnnotatorType: AnnotatorType = DOCUMENT | ||
|
||
|
||
override def annotate(annotations: Seq[Annotation]): Seq[Annotation] = { | ||
annotations | ||
} | ||
|
||
|
||
} | ||
|
||
|
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.
@maziyarpanahi @xusliebana , don't you guys think that the name for this trait could be
HasVariableCountInputAnnotationCols
HasInputAnnotationCols already supports multiple input columns, the main change I see here is that the number of columns will no longer be defined in a 'declarative' fashion inside the library, but in a more dynamic way by the user, during annotator setup.
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.
@albertoandreottiATgmail Yes, that totally makes sense! I'll leave it to you and @xusliebana to rename this and the unit tests. (@xusliebana you can directly fix the changes inside release/333-release-candidate branch)
@albertoandreottiATgmail Could you please also review removing
final
fromfinal def setInputCols()
? I am not sure why we usedfinal
so I want to be sure removing it won't be risky in some scenarios.