pytextprep-hm25 0.1.0__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.
pytextprep/__init__.py ADDED
@@ -0,0 +1,36 @@
1
+ from .pipeline import Pipeline
2
+
3
+ from .steps import cleaning
4
+ from .steps import tokenize
5
+ from .steps import filtering
6
+ from .steps import slang
7
+ from .steps import generate_ngrams
8
+ from .steps import accents
9
+ from .steps import lemmatizer
10
+ from .steps import normalize_arabic
11
+
12
+ from .steps.cleaning import (
13
+ lowercase,
14
+ normalize_whitespace,
15
+ remove_urls,
16
+ remove_emails,
17
+ remove_punctuation,
18
+ normalize_repeating_chars,
19
+ remove_mentions,
20
+ remove_digits,
21
+ remove_hashtags,
22
+ remove_html,
23
+ handle_emojis,
24
+ )
25
+
26
+ from .steps.filtering import remove_stopwords,expand_contractions
27
+ from .steps.tokenize import word_tokenize, sent_tokenize
28
+ from .steps.slang import normalize_slang
29
+ from .steps.generate_ngrams import ngrams,char_ngrams
30
+ from .steps.accents import remove_accents
31
+ from .steps.normalize_arabic import normalize_arabic
32
+ from .steps.lemmatizer import lemmatize
33
+ from .cleaner import TextCleaner
34
+
35
+
36
+ __version__ = "0.1.0"
pytextprep/cleaner.py ADDED
@@ -0,0 +1,25 @@
1
+
2
+ from functools import wraps
3
+
4
+ from .registry import get_step, available_steps
5
+
6
+
7
+ class TextCleaner:
8
+
9
+ def __init__(self, language="en"):
10
+ self.language = language
11
+
12
+ def __getattr__(self, name):
13
+
14
+ if name not in available_steps():
15
+ raise AttributeError(
16
+ f"{type(self).__name__!s} has no attribute {name!r}"
17
+ )
18
+
19
+ func = get_step(name)
20
+
21
+ @wraps(func)
22
+ def step_method(*args, **kwargs):
23
+ return func(*args, **kwargs)
24
+
25
+ return step_method
@@ -0,0 +1,9 @@
1
+ from pytextprep import remove_urls, remove_punctuation, normalize_repeating_chars
2
+
3
+ raw_input = " Check out https://example.com!! I looove NLP #2026 😊... "
4
+
5
+ step1 = remove_urls(raw_input)
6
+ step2 = remove_punctuation(step1)
7
+ step3 = normalize_repeating_chars(step2)
8
+
9
+ print(step3)
@@ -0,0 +1,16 @@
1
+ from pytextprep import Pipeline
2
+
3
+ nlp_pipeline = Pipeline([
4
+ ("remove_urls", {}),
5
+ ("expand_contractions", {}),
6
+ ("remove_punctuation", {"keep_emojis": False}),
7
+ ("remove_stopwords", {"custom_words": ["check"]}),
8
+ ("lowercase", {}),
9
+ ("lemmatize", {}),
10
+ ])
11
+
12
+ raw_text = "I don't like spam emails! Visit https://test.com right NOW."
13
+ processed_text = nlp_pipeline.transform(raw_text)
14
+
15
+ print("Raw: ", raw_text)
16
+ print("Processed:", processed_text)
@@ -0,0 +1,6 @@
1
+ from pytextprep import Pipeline
2
+
3
+ arabic = Pipeline([("normalize_arabic", {}),
4
+ ("remove_punctuation", {})])
5
+ print(arabic.transform("أَهْلاً بِكُم في الـــمـعهد!!"))
6
+
@@ -0,0 +1,9 @@
1
+ class PyTextPrepError(Exception):
2
+ """Base exception for pytextprep library"""
3
+ pass
4
+ class UnknownStepError(PyTextPrepError):
5
+ """Raised when a step name not in the registry"""
6
+ pass
7
+ class InvalidStepParamsError(PyTextPrepError):
8
+ """Raised when wrong parameter is passed"""
9
+ pass
pytextprep/pipeline.py ADDED
@@ -0,0 +1,79 @@
1
+ import difflib
2
+ import inspect
3
+ from .registry import get_step,available_steps
4
+ from .exceptions import UnknownStepError,InvalidStepParamsError
5
+
6
+ class Pipeline:
7
+ """
8
+ Pipeline to run steps in sequence
9
+ Example:
10
+
11
+ nlp_pipeline = Pipeline([
12
+ ("remove_urls", {}),
13
+ ("expand_contractions", {}),
14
+ ("remove_punctuation", {"keep_emojis": False}),
15
+ ("remove_stopwords", {"custom_words": ["check"]}),
16
+ ("lowercase", {}),
17
+
18
+ ])
19
+ processed_text = nlp_pipeline.transform("I don't like spam emails! Visit https://test.com right NOW.")
20
+ """
21
+ def __init__(self, steps:list[tuple[str, dict]])->None:
22
+ self.steps=steps.copy()
23
+ self.validated_steps = []
24
+ for step_name,param in self.steps:
25
+ if not isinstance(param, dict):
26
+ raise InvalidStepParamsError(f"Parameters for {step_name!r} must be a dictionary")
27
+
28
+ if step_name in available_steps():
29
+ func=get_step(step_name)
30
+ sig=inspect.signature(func)
31
+ for parameter in param:
32
+ if parameter not in sig.parameters:
33
+ raise InvalidStepParamsError(f"{step_name}() got an unexpected parameter {parameter!r}")
34
+ try:
35
+ sig.bind("sample text", **param)
36
+ self.validated_steps.append((step_name,param.copy(),func))
37
+ except TypeError as e:
38
+ raise InvalidStepParamsError(f"{step_name}() {e}") from None
39
+
40
+ else:
41
+ result = difflib.get_close_matches(step_name,available_steps(),n=1)
42
+ if result:
43
+ raise UnknownStepError(f"{step_name!r} is not a step. Did you mean {result[0]!r}?")
44
+ else:
45
+ raise UnknownStepError(f"{step_name!r} is not a step.")
46
+
47
+ def __repr__(self)->str:
48
+ """Return a readable representation of the pipeline"""
49
+ return f"Pipeline(steps={self.steps!r})"
50
+
51
+ def transform (self,text:str)->str:
52
+ """Apply all pipeline steps to one text.
53
+
54
+ Example:
55
+ pipeline.transform("HELLO")
56
+
57
+ """
58
+ if not isinstance(text, str):
59
+ raise TypeError(f"expected str, got {type(text).__name__}")
60
+ user_text=text
61
+ for x_tuple in self.validated_steps:
62
+ step_name, param, func=x_tuple
63
+ user_text=func(user_text,**param)
64
+ return user_text
65
+
66
+ def transform_batch(self,text:list[str])->list[str]:
67
+ """Apply the pipeline to a list of texts.
68
+
69
+ Example:
70
+ pipeline.transform_batch(["I don't like spam emails! Visit https://test.com right NOW.", "GOODBYE"])
71
+ """
72
+ if not isinstance(text, list):
73
+ raise TypeError(f"expected list, got {type(text).__name__}")
74
+
75
+ user_text_list=[]
76
+ for i in text:
77
+ user_text_list.append(self.transform(i))
78
+ return user_text_list
79
+
pytextprep/registry.py ADDED
@@ -0,0 +1,30 @@
1
+
2
+ from functools import wraps
3
+ from typing import Callable, Any,List
4
+
5
+ def requires_str(func:Callable) -> Callable:
6
+ @wraps(func)
7
+ def checker(text:str, *args, **kwargs)->Any:
8
+ if not isinstance(text, str):
9
+ raise TypeError(f"expected str, got {type(text).__name__}")
10
+ return func(text, *args, **kwargs)
11
+
12
+ return checker
13
+
14
+
15
+
16
+ REGISTRY = {}
17
+
18
+ def register_step(name:str) -> Callable:
19
+
20
+ def decorator(func: Callable) -> Callable:
21
+ REGISTRY[name] = func
22
+ return func
23
+
24
+ return decorator
25
+
26
+ def get_step(name:str)->Callable:
27
+ return REGISTRY[name]
28
+
29
+ def available_steps()->List[str]:
30
+ return list(REGISTRY.keys())
@@ -0,0 +1,55 @@
1
+ wouldn't would not
2
+ wouldn't've would not have
3
+ you're you are
4
+ you've you have
5
+ aren't are not
6
+ there's there is
7
+ can't cannot
8
+ they'd they had
9
+ they'll they will
10
+ they're they are
11
+ they've they have
12
+ couldn't could not
13
+ didn't did not
14
+ doesn't does not
15
+ don't do not
16
+ hadn't had not
17
+ hasn't has not
18
+ haven't have not
19
+ he'd he had
20
+ he'll he will
21
+ he's he is
22
+ i'll I will
23
+ i've I have
24
+ isn't is not
25
+ let's let us
26
+ mightn't might not
27
+ mustn't must not
28
+ shan't shall not
29
+ she'd she had
30
+ she'll she will
31
+ she's she is
32
+ shouldn't should not
33
+ that's that is
34
+ we'd we had
35
+ we're we are
36
+ we've we have
37
+ weren't were not
38
+ what'll what will
39
+ what're what are
40
+ what's what is
41
+ what've what have
42
+ where's where is
43
+ who'd who had
44
+ who'll who will
45
+ who're who are
46
+ who's who is
47
+ who've who have
48
+ won't will not
49
+ you'd you had
50
+ you'll you will
51
+ i'd I had
52
+ it's it is
53
+ it'll it will
54
+ wasn't was not
55
+ i'm I am
@@ -0,0 +1,318 @@
1
+ a
2
+ about
3
+ above
4
+ across
5
+ after
6
+ afterwards
7
+ again
8
+ against
9
+ all
10
+ almost
11
+ alone
12
+ along
13
+ already
14
+ also
15
+ although
16
+ always
17
+ am
18
+ among
19
+ amongst
20
+ amoungst
21
+ amount
22
+ an
23
+ and
24
+ another
25
+ any
26
+ anyhow
27
+ anyone
28
+ anything
29
+ anyway
30
+ anywhere
31
+ are
32
+ around
33
+ as
34
+ at
35
+ back
36
+ be
37
+ became
38
+ because
39
+ become
40
+ becomes
41
+ becoming
42
+ been
43
+ before
44
+ beforehand
45
+ behind
46
+ being
47
+ below
48
+ beside
49
+ besides
50
+ between
51
+ beyond
52
+ bill
53
+ both
54
+ bottom
55
+ but
56
+ by
57
+ call
58
+ can
59
+ cannot
60
+ cant
61
+ co
62
+ con
63
+ could
64
+ couldnt
65
+ cry
66
+ de
67
+ describe
68
+ detail
69
+ do
70
+ done
71
+ down
72
+ due
73
+ during
74
+ each
75
+ eg
76
+ eight
77
+ either
78
+ eleven
79
+ else
80
+ elsewhere
81
+ empty
82
+ enough
83
+ etc
84
+ even
85
+ ever
86
+ every
87
+ everyone
88
+ everything
89
+ everywhere
90
+ except
91
+ few
92
+ fifteen
93
+ fifty
94
+ fill
95
+ find
96
+ fire
97
+ first
98
+ five
99
+ for
100
+ former
101
+ formerly
102
+ forty
103
+ found
104
+ four
105
+ from
106
+ front
107
+ full
108
+ further
109
+ get
110
+ give
111
+ go
112
+ had
113
+ has
114
+ hasnt
115
+ have
116
+ he
117
+ hence
118
+ her
119
+ here
120
+ hereafter
121
+ hereby
122
+ herein
123
+ hereupon
124
+ hers
125
+ herself
126
+ him
127
+ himself
128
+ his
129
+ how
130
+ however
131
+ hundred
132
+ i
133
+ ie
134
+ if
135
+ in
136
+ inc
137
+ indeed
138
+ interest
139
+ into
140
+ is
141
+ it
142
+ its
143
+ itself
144
+ keep
145
+ last
146
+ latter
147
+ latterly
148
+ least
149
+ less
150
+ ltd
151
+ made
152
+ many
153
+ may
154
+ me
155
+ meanwhile
156
+ might
157
+ mill
158
+ mine
159
+ more
160
+ moreover
161
+ most
162
+ mostly
163
+ move
164
+ much
165
+ must
166
+ my
167
+ myself
168
+ name
169
+ namely
170
+ neither
171
+ never
172
+ nevertheless
173
+ next
174
+ nine
175
+ no
176
+ nobody
177
+ none
178
+ noone
179
+ nor
180
+ not
181
+ nothing
182
+ now
183
+ nowhere
184
+ of
185
+ off
186
+ often
187
+ on
188
+ once
189
+ one
190
+ only
191
+ onto
192
+ or
193
+ other
194
+ others
195
+ otherwise
196
+ our
197
+ ours
198
+ ourselves
199
+ out
200
+ over
201
+ own
202
+ part
203
+ per
204
+ perhaps
205
+ please
206
+ put
207
+ rather
208
+ re
209
+ same
210
+ see
211
+ seem
212
+ seemed
213
+ seeming
214
+ seems
215
+ serious
216
+ several
217
+ she
218
+ should
219
+ show
220
+ side
221
+ since
222
+ sincere
223
+ six
224
+ sixty
225
+ so
226
+ some
227
+ somehow
228
+ someone
229
+ something
230
+ sometime
231
+ sometimes
232
+ somewhere
233
+ still
234
+ such
235
+ system
236
+ take
237
+ ten
238
+ than
239
+ that
240
+ the
241
+ their
242
+ them
243
+ themselves
244
+ then
245
+ thence
246
+ there
247
+ thereafter
248
+ thereby
249
+ therefore
250
+ therein
251
+ thereupon
252
+ these
253
+ they
254
+ thick
255
+ thin
256
+ third
257
+ this
258
+ those
259
+ though
260
+ three
261
+ through
262
+ throughout
263
+ thru
264
+ thus
265
+ to
266
+ together
267
+ too
268
+ top
269
+ toward
270
+ towards
271
+ twelve
272
+ twenty
273
+ two
274
+ un
275
+ under
276
+ until
277
+ up
278
+ upon
279
+ us
280
+ very
281
+ via
282
+ was
283
+ we
284
+ well
285
+ were
286
+ what
287
+ whatever
288
+ when
289
+ whence
290
+ whenever
291
+ where
292
+ whereafter
293
+ whereas
294
+ whereby
295
+ wherein
296
+ whereupon
297
+ wherever
298
+ whether
299
+ which
300
+ while
301
+ whither
302
+ who
303
+ whoever
304
+ whole
305
+ whom
306
+ whose
307
+ why
308
+ will
309
+ with
310
+ within
311
+ without
312
+ would
313
+ yet
314
+ you
315
+ your
316
+ yours
317
+ yourself
318
+ yourselves
@@ -0,0 +1,22 @@
1
+ import unicodedata
2
+ from pytextprep.registry import requires_str,register_step
3
+
4
+ @register_step("remove_accents")
5
+ @requires_str
6
+ def remove_accents(text:str)->str:
7
+ """
8
+ Remove accents from characters in the given text.
9
+ Uses Unicode NFKD normalization and removes combining marks.
10
+
11
+ Example:
12
+ "Crème brûlée, naïve café" → "Creme brulee, naive cafe"
13
+ """
14
+ text = unicodedata.normalize("NFKD", text)
15
+ result=""
16
+ for char in text:
17
+ if not unicodedata.combining(char):
18
+ result+=char
19
+ return result
20
+
21
+
22
+