spark-nlp 6.1.3rc1__py2.py3-none-any.whl → 6.1.5__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of spark-nlp might be problematic. Click here for more details.

@@ -1,225 +0,0 @@
1
- # Copyright 2017-2023 John Snow Labs
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- """Contains classes for RoBertaForZeroShotClassification."""
15
-
16
- from sparknlp.common import *
17
-
18
-
19
- class RoBertaForZeroShotClassification(AnnotatorModel,
20
- HasCaseSensitiveProperties,
21
- HasBatchedAnnotate,
22
- HasClassifierActivationProperties,
23
- HasCandidateLabelsProperties,
24
- HasEngine):
25
- """RoBertaForZeroShotClassification using a `ModelForSequenceClassification` trained on NLI (natural language
26
- inference) tasks. Equivalent of `RoBertaForSequenceClassification` models, but these models don't require a hardcoded
27
- number of potential classes, they can be chosen at runtime. It usually means it's slower but it is much more
28
- flexible.
29
-
30
- Note that the model will loop through all provided labels. So the more labels you have, the
31
- longer this process will take.
32
-
33
- Any combination of sequences and labels can be passed and each combination will be posed as a premise/hypothesis
34
- pair and passed to the pretrained model.
35
-
36
- Pretrained models can be loaded with :meth:`.pretrained` of the companion
37
- object:
38
-
39
- >>> sequenceClassifier = RoBertaForZeroShotClassification.pretrained() \\
40
- ... .setInputCols(["token", "document"]) \\
41
- ... .setOutputCol("label")
42
-
43
- The default model is ``"roberta_base_zero_shot_classifier_nli"``, if no name is
44
- provided.
45
-
46
- For available pretrained models please see the `Models Hub
47
- <https://sparknlp.orgtask=Text+Classification>`__.
48
-
49
- To see which models are compatible and how to import them see
50
- `Import Transformers into Spark NLP 🚀
51
- <https://github.com/JohnSnowLabs/spark-nlp/discussions/5669>`_.
52
-
53
- ====================== ======================
54
- Input Annotation types Output Annotation type
55
- ====================== ======================
56
- ``DOCUMENT, TOKEN`` ``CATEGORY``
57
- ====================== ======================
58
-
59
- Parameters
60
- ----------
61
- batchSize
62
- Batch size. Large values allows faster processing but requires more
63
- memory, by default 8
64
- caseSensitive
65
- Whether to ignore case in tokens for embeddings matching, by default
66
- True
67
- configProtoBytes
68
- ConfigProto from tensorflow, serialized into byte array.
69
- maxSentenceLength
70
- Max sentence length to process, by default 128
71
- coalesceSentences
72
- Instead of 1 class per sentence (if inputCols is `sentence`) output 1
73
- class per document by averaging probabilities in all sentences, by
74
- default False
75
- activation
76
- Whether to calculate logits via Softmax or Sigmoid, by default
77
- `"softmax"`.
78
-
79
- Examples
80
- --------
81
- >>> import sparknlp
82
- >>> from sparknlp.base import *
83
- >>> from sparknlp.annotator import *
84
- >>> from pyspark.ml import Pipeline
85
- >>> documentAssembler = DocumentAssembler() \\
86
- ... .setInputCol("text") \\
87
- ... .setOutputCol("document")
88
- >>> tokenizer = Tokenizer() \\
89
- ... .setInputCols(["document"]) \\
90
- ... .setOutputCol("token")
91
- >>> sequenceClassifier = RoBertaForZeroShotClassification.pretrained() \\
92
- ... .setInputCols(["token", "document"]) \\
93
- ... .setOutputCol("label") \\
94
- ... .setCaseSensitive(True)
95
- >>> pipeline = Pipeline().setStages([
96
- ... documentAssembler,
97
- ... tokenizer,
98
- ... sequenceClassifier
99
- ... ])
100
- >>> data = spark.createDataFrame([["I loved this movie when I was a child.", "It was pretty boring."]]).toDF("text")
101
- >>> result = pipeline.fit(data).transform(data)
102
- >>> result.select("label.result").show(truncate=False)
103
- +------+
104
- |result|
105
- +------+
106
- |[pos] |
107
- |[neg] |
108
- +------+
109
- """
110
- name = "RoBertaForZeroShotClassification"
111
-
112
- inputAnnotatorTypes = [AnnotatorType.DOCUMENT, AnnotatorType.TOKEN]
113
-
114
- outputAnnotatorType = AnnotatorType.CATEGORY
115
-
116
- maxSentenceLength = Param(Params._dummy(),
117
- "maxSentenceLength",
118
- "Max sentence length to process",
119
- typeConverter=TypeConverters.toInt)
120
-
121
- configProtoBytes = Param(Params._dummy(),
122
- "configProtoBytes",
123
- "ConfigProto from tensorflow, serialized into byte array. Get with config_proto.SerializeToString()",
124
- TypeConverters.toListInt)
125
-
126
- coalesceSentences = Param(Params._dummy(), "coalesceSentences",
127
- "Instead of 1 class per sentence (if inputCols is '''sentence''') output 1 class per document by averaging probabilities in all sentences.",
128
- TypeConverters.toBoolean)
129
-
130
- def getClasses(self):
131
- """
132
- Returns labels used to train this model
133
- """
134
- return self._call_java("getClasses")
135
-
136
- def setConfigProtoBytes(self, b):
137
- """Sets configProto from tensorflow, serialized into byte array.
138
-
139
- Parameters
140
- ----------
141
- b : List[int]
142
- ConfigProto from tensorflow, serialized into byte array
143
- """
144
- return self._set(configProtoBytes=b)
145
-
146
- def setMaxSentenceLength(self, value):
147
- """Sets max sentence length to process, by default 128.
148
-
149
- Parameters
150
- ----------
151
- value : int
152
- Max sentence length to process
153
- """
154
- return self._set(maxSentenceLength=value)
155
-
156
- def setCoalesceSentences(self, value):
157
- """Instead of 1 class per sentence (if inputCols is '''sentence''') output 1 class per document by averaging
158
- probabilities in all sentences. Due to max sequence length limit in almost all transformer models such as RoBerta
159
- (512 tokens), this parameter helps to feed all the sentences into the model and averaging all the probabilities
160
- for the entire document instead of probabilities per sentence. (Default: true)
161
-
162
- Parameters
163
- ----------
164
- value : bool
165
- If the output of all sentences will be averaged to one output
166
- """
167
- return self._set(coalesceSentences=value)
168
-
169
- @keyword_only
170
- def __init__(self, classname="com.johnsnowlabs.nlp.annotators.classifier.dl.RoBertaForZeroShotClassification",
171
- java_model=None):
172
- super(RoBertaForZeroShotClassification, self).__init__(
173
- classname=classname,
174
- java_model=java_model
175
- )
176
- self._setDefault(
177
- batchSize=8,
178
- maxSentenceLength=128,
179
- caseSensitive=True,
180
- coalesceSentences=False,
181
- activation="softmax"
182
- )
183
-
184
- @staticmethod
185
- def loadSavedModel(folder, spark_session):
186
- """Loads a locally saved model.
187
-
188
- Parameters
189
- ----------
190
- folder : str
191
- Folder of the saved model
192
- spark_session : pyspark.sql.SparkSession
193
- The current SparkSession
194
-
195
- Returns
196
- -------
197
- RoBertaForZeroShotClassification
198
- The restored model
199
- """
200
- from sparknlp.internal import _RoBertaForZeroShotClassification
201
- jModel = _RoBertaForZeroShotClassification(folder, spark_session._jsparkSession)._java_obj
202
- return RoBertaForZeroShotClassification(java_model=jModel)
203
-
204
- @staticmethod
205
- def pretrained(name="roberta_base_zero_shot_classifier_nli", lang="en", remote_loc=None):
206
- """Downloads and loads a pretrained model.
207
-
208
- Parameters
209
- ----------
210
- name : str, optional
211
- Name of the pretrained model, by default
212
- "roberta_base_zero_shot_classifier_nli"
213
- lang : str, optional
214
- Language of the pretrained model, by default "en"
215
- remote_loc : str, optional
216
- Optional remote address of the resource, by default None. Will use
217
- Spark NLPs repositories otherwise.
218
-
219
- Returns
220
- -------
221
- RoBertaForZeroShotClassification
222
- The restored model
223
- """
224
- from sparknlp.pretrained import ResourceDownloader
225
- return ResourceDownloader.downloadModel(RoBertaForZeroShotClassification, name, lang, remote_loc)
@@ -1,174 +0,0 @@
1
- # Copyright 2017-2025 John Snow Labs
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- """Contains classes for Extractor."""
15
- from sparknlp.common import *
16
-
17
- class Extractor(AnnotatorModel):
18
- name = "Extractor"
19
-
20
- inputAnnotatorTypes = [AnnotatorType.DOCUMENT]
21
-
22
- outputAnnotatorType = AnnotatorType.CHUNK
23
-
24
- emailDateTimeTzPattern = Param(Params._dummy(),
25
- "emailDateTimeTzPattern",
26
- "Specifies the date-time pattern for email timestamps, including time zone formatting.",
27
- typeConverter=TypeConverters.toString)
28
-
29
- emailAddress = Param(
30
- Params._dummy(),
31
- "emailAddress",
32
- "Specifies the pattern for email addresses.",
33
- typeConverter=TypeConverters.toString
34
- )
35
-
36
- ipAddressPattern = Param(
37
- Params._dummy(),
38
- "ipAddressPattern",
39
- "Specifies the pattern for IP addresses.",
40
- typeConverter=TypeConverters.toString
41
- )
42
-
43
- ipAddressNamePattern = Param(
44
- Params._dummy(),
45
- "ipAddressNamePattern",
46
- "Specifies the pattern for IP addresses with names.",
47
- typeConverter=TypeConverters.toString
48
- )
49
-
50
- mapiIdPattern = Param(
51
- Params._dummy(),
52
- "mapiIdPattern",
53
- "Specifies the pattern for MAPI IDs.",
54
- typeConverter=TypeConverters.toString
55
- )
56
-
57
- usPhoneNumbersPattern = Param(
58
- Params._dummy(),
59
- "usPhoneNumbersPattern",
60
- "Specifies the pattern for US phone numbers.",
61
- typeConverter=TypeConverters.toString
62
- )
63
-
64
- imageUrlPattern = Param(
65
- Params._dummy(),
66
- "imageUrlPattern",
67
- "Specifies the pattern for image URLs.",
68
- typeConverter=TypeConverters.toString
69
- )
70
-
71
- textPattern = Param(
72
- Params._dummy(),
73
- "textPattern",
74
- "Specifies the pattern for text after and before.",
75
- typeConverter=TypeConverters.toString
76
- )
77
-
78
- extractorMode = Param(
79
- Params._dummy(),
80
- "extractorMode",
81
- "possible values: " +
82
- "email_date, email_address, ip_address, ip_address_name, mapi_id, us_phone_numbers, image_urls, bullets, text_after, text_before",
83
- typeConverter=TypeConverters.toString
84
- )
85
-
86
- def setEmailDateTimeTzPattern(self, value):
87
- """Sets specifies the date-time pattern for email timestamps, including time zone formatting.
88
-
89
- Parameters
90
- ----------
91
- value : str
92
- Specifies the date-time pattern for email timestamps, including time zone formatting.
93
- """
94
- return self._set(emailDateTimeTzPattern=value)
95
-
96
- def setEmailAddress(self, value):
97
- """Sets the pattern for email addresses.
98
-
99
- Parameters
100
- ----------
101
- value : str
102
- Specifies the pattern for email addresses.
103
- """
104
- return self._set(emailAddress=value)
105
-
106
- def setIpAddressPattern(self, value):
107
- """Sets the pattern for IP addresses.
108
-
109
- Parameters
110
- ----------
111
- value : str
112
- Specifies the pattern for IP addresses.
113
- """
114
- return self._set(ipAddressPattern=value)
115
-
116
- def setIpAddressNamePattern(self, value):
117
- """Sets the pattern for IP addresses with names.
118
-
119
- Parameters
120
- ----------
121
- value : str
122
- Specifies the pattern for IP addresses with names.
123
- """
124
- return self._set(ipAddressNamePattern=value)
125
-
126
- def setMapiIdPattern(self, value):
127
- """Sets the pattern for MAPI IDs.
128
-
129
- Parameters
130
- ----------
131
- value : str
132
- Specifies the pattern for MAPI IDs.
133
- """
134
- return self._set(mapiIdPattern=value)
135
-
136
- def setUsPhoneNumbersPattern(self, value):
137
- """Sets the pattern for US phone numbers.
138
-
139
- Parameters
140
- ----------
141
- value : str
142
- Specifies the pattern for US phone numbers.
143
- """
144
- return self._set(usPhoneNumbersPattern=value)
145
-
146
- def setImageUrlPattern(self, value):
147
- """Sets the pattern for image URLs.
148
-
149
- Parameters
150
- ----------
151
- value : str
152
- Specifies the pattern for image URLs.
153
- """
154
- return self._set(imageUrlPattern=value)
155
-
156
- def setTextPattern(self, value):
157
- """Sets the pattern for text after and before.
158
-
159
- Parameters
160
- ----------
161
- value : str
162
- Specifies the pattern for text after and before.
163
- """
164
- return self._set(textPattern=value)
165
-
166
- def setExtractorMode(self, value):
167
- return self._set(extractorMode=value)
168
-
169
- @keyword_only
170
- def __init__(self, classname="com.johnsnowlabs.nlp.annotators.Extractor", java_model=None):
171
- super(Extractor, self).__init__(
172
- classname=classname,
173
- java_model=java_model
174
- )