NLTK-based Stages
PdPipeline stages dependent on the nltk Python library.
Please note that the nltk Python package must be installed for the stages in this module to work.
When attempting to load stages from this module, pdpipe will first attempt to import nltk. If it fails, it will issue a warning, will not import any of the pipeline stages that make up this module, and continue to load other pipeline stages.
Classes
TokenizeText
Bases: MapColVals
A pipeline stage that tokenizes a text column into token lists.
Note: The nltk package must be installed for this pipeline stage to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
single label, list-like of callable
|
Column labels in the DataFrame to be transformed. Alternatively, this
parameter can be assigned a callable returning an iterable of labels
from an input pandas.DataFrame. See |
required |
drop |
bool, default True
|
If set to True, the source columns are dropped after being tokenized, and the resulting tokenized columns retain the names of the source columns. Otherwise, tokenized columns gain the suffix '_tok'. |
True
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> df = pd.DataFrame(
... [[3.2, "Kick the baby!"]], [1], ['freq', 'content'])
>>> tokenize_stage = pdp.TokenizeText('content')
>>> tokenize_stage(df)
freq content
1 3.2 [Kick, the, baby, !]
Source code in pdpipe/nltk_stages.py
UntokenizeText
Bases: MapColVals
A pipeline stage that joins token lists to whitespace-separated strings.
Target columns must be series of token lists; i.e. every cell in the series is an iterable of string tokens.
Note: The nltk package must be installed for this pipeline stage to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
single label, list-like of callable
|
Column labels in the DataFrame to be transformed. Alternatively, this
parameter can be assigned a callable returning an iterable of labels
from an input pandas.DataFrame. See |
required |
drop |
bool, default True
|
If set to True, the source columns are dropped after being untokenized, and the resulting columns retain the names of the source columns. Otherwise, untokenized columns gain the suffix '_untok'. |
True
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> data = [[3.2, ['Shake', 'and', 'bake!']]]
>>> df = pd.DataFrame(data, [1], ['freq', 'content'])
>>> untokenize_stage = pdp.UntokenizeText('content')
>>> untokenize_stage(df)
freq content
1 3.2 Shake and bake!
Source code in pdpipe/nltk_stages.py
RemoveStopwords
Bases: MapColVals
A pipeline stage that removes stopwords from a tokenized list.
Target columns must be series of token lists; i.e. every cell in the series is an iterable of string tokens.
Note: The nltk package must be installed for this pipeline stage to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
language |
str or array-like
|
If a string is given, interpreted as the language of the stopwords, and should then be one of the languages supported by the NLTK Stopwords Corpus. If a list is given, it is assumed to be the list of stopwords to remove. |
required |
columns |
single label, list-like or callable
|
Column labels in the DataFrame to be transformed. Alternatively, this
parameter can be assigned a callable returning an iterable of labels
from an input pandas.DataFrame. See |
required |
drop |
bool, default True
|
If set to True, the source columns are dropped after stopword removal, and the resulting columns retain the names of the source columns. Otherwise, resulting columns gain the suffix '_nostop'. |
True
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>> import pandas as pd; import pdpipe as pdp;
>> data = [[3.2, ['kick', 'the', 'baby']]]
>> df = pd.DataFrame(data, [1], ['freq', 'content'])
>> remove_stopwords = pdp.RemoveStopwords('english', 'content')
>> remove_stopwords(df)
freq content
1 3.2 [kick, baby]
Source code in pdpipe/nltk_stages.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
SnowballStem
Bases: MapColVals
A pipeline stage that stems tokens in a list using the Snowball stemmer.
Target columns must be series of token lists; i.e. every cell in the series is an iterable of string tokens.
Note: The nltk package must be installed for this pipeline stage to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stemmer_name |
str
|
The name of the Snowball stemmer to use. Should be one of the Snowball stemmers implemented by nltk. E.g. 'EnglishStemmer'. |
required |
columns |
single label, list-like or callable
|
Column labels in the DataFrame to be transformed. Alternatively, this
parameter can be assigned a callable returning an iterable of labels
from an input pandas.DataFrame. See |
required |
drop |
bool, default True
|
If set to True, the source columns are dropped after stemming, and the resulting columns retain the names of the source columns. Otherwise, resulting columns gain the suffix '_stem'. |
True
|
min_len |
int, optional
|
If provided, tokens shorter than this length are not stemmed. |
None
|
max_len |
int, optional
|
If provided, tokens longer than this length are not stemmed. |
None
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
stemmer |
nltk.stem.snowball.SnowballStemmer
|
The Snowball stemmer instance used by this pipeline stage. |
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> data = [[3.2, ['kicking', 'boats']]]
>>> df = pd.DataFrame(data, [1], ['freq', 'content'])
>>> remove_stopwords = pdp.SnowballStem('EnglishStemmer', 'content')
>>> remove_stopwords(df)
freq content
1 3.2 [kick, boat]
Source code in pdpipe/nltk_stages.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
Attributes
stemmer = SnowballStem.__safe_stemmer_by_name(stemmer_name)
instance-attribute
DropRareTokens
Bases: ColumnsBasedPipelineStage
A pipeline stage that drop rare tokens from token lists.
Target columns must be series of token lists; i.e. every cell in the series is an iterable of string tokens.
Note: The nltk package must be installed for this pipeline stage to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
single label, list-like or callable
|
Column labels in the DataFrame to be transformed. Alternatively, this
parameter can be assigned a callable returning an iterable of labels
from an input pandas.DataFrame. See |
required |
threshold |
int
|
The rarity threshold to use. Only tokens appearing more than this number of times in a column will remain in token lists in that column. |
required |
drop |
bool, default True
|
If set to True, the source columns are dropped after being transformed, and the resulting columns retain the names of the source columns. Otherwise, the new columns gain the suffix '_norare'. |
True
|
**kwargs |
object
|
All PdPipelineStage constructor parameters are supported. |
{}
|
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> data = [[7, ['a', 'a', 'b']], [3, ['b', 'c', 'd']]]
>>> df = pd.DataFrame(data, columns=['num', 'chars'])
>>> rare_dropper = pdp.DropRareTokens('chars', 1)
>>> rare_dropper(df)
num chars
0 7 [a, a, b]
1 3 [b]
Source code in pdpipe/nltk_stages.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
|