-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13783 from JohnSnowLabs/bug/SPARKNLP-832-MultiDat…
…eMatcher-doesn-t-return-multiple-dates SPARKNLP-832-MultiDateMatcher-doesn-t-return-multiple-dates
- Loading branch information
Showing
13 changed files
with
221 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2017-2023 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. | ||
|
||
"""Allowed strategies for RuleFactory applications regarding replacement""" | ||
|
||
|
||
class MatchStrategy(object): | ||
"""Object that contains constants for how for matched strategies used in RuleFactory. | ||
Possible values are: | ||
================================== =============================================================================== | ||
Value Description | ||
================================== =============================================================================== | ||
``MatchStrategy.MATCH_ALL`` This strategy matches all occurrences of all rules in the given text. | ||
``MatchStrategy.MATCH_FIRST`` This strategy matches only the first occurrence of each rule in the given text. | ||
``MatchStrategy.MATCH_COMPLETE`` This strategy matches only the first occurrence of each rule in the given text. | ||
================================== =============================================================================== | ||
""" | ||
MATCH_ALL = "MATCH_ALL" | ||
MATCH_FIRST = "MATCH_FIRST" | ||
MATCH_COMPLETE = "MATCH_COMPLETE" |
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,50 @@ | ||
# Copyright 2017-2022 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. | ||
import unittest | ||
|
||
import pytest | ||
|
||
from sparknlp.annotator import * | ||
from sparknlp.base import * | ||
from pyspark.sql.functions import size | ||
from test.util import SparkContextForTest | ||
|
||
|
||
@pytest.mark.fast | ||
class MultiDateMatcherTestSpec(unittest.TestCase): | ||
|
||
def setUp(self): | ||
text = """ | ||
Lease Period Monthly Installment of Base Rent. | ||
January 1, 2021 –December 31, 2021 $20,304.85 . | ||
January 1, 2022 –December 31, 2022 $20,914.00 . | ||
""" | ||
self.data = SparkContextForTest.spark.createDataFrame([[text]]).toDF("text") | ||
|
||
def runTest(self): | ||
document_assembler = DocumentAssembler() \ | ||
.setInputCol("text") \ | ||
.setOutputCol("document") | ||
date_matcher = MultiDateMatcher() \ | ||
.setInputCols(["document"]) \ | ||
.setOutputCol("date") \ | ||
.setOutputFormat("yyyy/MM/dd") \ | ||
.setRelaxedFactoryStrategy(MatchStrategy.MATCH_ALL) | ||
|
||
pipeline = Pipeline(stages=[document_assembler, date_matcher]) | ||
model = pipeline.fit(self.data) | ||
result = model.transform(self.data) | ||
|
||
actual_dates = result.select(size("date.result")).collect()[0][0] | ||
self.assertEquals(actual_dates, 4) |
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
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
37 changes: 37 additions & 0 deletions
37
src/main/scala/com/johnsnowlabs/nlp/util/io/MatchStrategy.scala
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,37 @@ | ||
/* | ||
* Copyright 2017-2023 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.util.io | ||
|
||
import com.johnsnowlabs.nlp.util.regex.RuleFactory | ||
|
||
/** Allowed strategies for [[RuleFactory]] applications regarding replacement */ | ||
object MatchStrategy extends Enumeration { | ||
|
||
implicit def str2frmt(v: String): Format = { | ||
v.toUpperCase match { | ||
case "MATCH_ALL" => MATCH_ALL | ||
case "MATCH_FIRST" => MATCH_FIRST | ||
case "MATCH_COMPLETE" => MATCH_COMPLETE | ||
case _ => | ||
throw new MatchError( | ||
s"Invalid MatchStrategy. Must be either of ${this.values.mkString("|")}") | ||
} | ||
} | ||
|
||
type Format = Value | ||
val MATCH_ALL, MATCH_FIRST, MATCH_COMPLETE = Value | ||
} |
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