-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseful_functions.py
420 lines (285 loc) · 11.2 KB
/
useful_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
263
264
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
415
416
417
418
419
420
import csv
import glob
import itertools
import os
import random
import string
import time
from string import punctuation
import pickle
import fitz
import matplotlib.pyplot as plt
import nltk
import numpy as np
import pandas as pd
import seaborn as sns
import spacy
from nltk.corpus import stopwords
from nltk.stem.porter import *
from nltk.stem.porter import PorterStemmer
from transformers import \
DistilBertTokenizer # required for this function only "custom_padding()"
nltk.download("stopwords")
nltk.download("punkt")
plt.style.use("ggplot")
def sorting_1(*args):
"""
You are given a string and an int.
Your task is to print all possible size replacement combinations of the string in lexicographic sorted order.
Example usage:
Inpute: sorting_1("ABDULLAH",2)
"""
albo = [
"".join(i)
for i in itertools.combinations_with_replacement(sorted(args[0]), int(args[1]))
]
print(albo)
for i in albo:
print(i)
# return i
def list_to_str(*args: list) -> str:
"""
Convert list to strings with cartesian product
Example usage:
Input: list_to_str([1, 2, 3], [4, 5, 6], ["ABD", "ZUB"])
"""
return " ".join(str(e) for e in (list(itertools.product(*args, repeat=1))))
def timer(function):
"""
A simple timer decorator to wrap around any function to get the execution time.
Example usage:
@UsefulFunctions.timer
def a_function(s):
for i in range(s):
pass
a_function(99999)
"""
def wrapper(*args, **kwargs):
before = time.time()
function_call = function(*args, **kwargs)
after = time.time()
fname = function.__name__
print(f"{fname} took {after-before} for execution")
return function_call
return wrapper
def join_list(deli, lst) -> str:
"""
Join a list of string to a string using your own delimeter.
Example usage:
a_list = ["12", "13", "3", "44", "s"]
print(uf.join_list(" ", a_list))
output: "12 13 44 s"
"""
str_returned = deli.join(lst)
return str_returned
def str_preproces_1(input_string: str) -> str:
"""
Return pre-processed string from input string
Example usage:
a_non_ascii_digit string = "He43llo ÀÈÌÒÙỲǸẀWo323rld"
print(uf.str_preprocess(a_non_ascii_digit))
output: "Hello World"
"""
return "".join(c for c in input_string if c.isascii() and not c.isdigit())
def str_count(corpus: str, search_string: str) -> int:
"""
Return frequency of a string present in the current corpus.
Example usage:
corpus = "your corpus here"
count = uf.str_count(corpus, "HERE")
output = 1
"""
count = corpus.count(search_string)
return count
def str_remove_lowercase(x: list) -> list:
"""
Return a list containing string if not lower case, using filter function
Example usage:
string_list =["ABC", "DEF", "GEMM", "ababv", "asdwd", "ABscw"]
print(uf.str_remove_uppercase(string_list))
output = ['ABC', 'DEF', 'GEMM']
Notes: Filter -> Constructs iterator from those elements of iterable for
which function returns true
"""
return list(filter(lambda x: x.isupper(), x))
def str_remove_uppercase(x: list) -> list:
"""
Return a list containing string of lowercase strings using filterfalse() function
Example usage:
string_list =["ABC", "DEF", "GEMM", "ababv", "asdwd", "ABscw"]
print(uf.str_remove_uppercase(string_list))
output =['ababv', 'asdwd', 'ABscw']
Notes: itertools.filterfalse -> Return the elements which
returns False (opposite of filter function)
"""
return list(itertools.filterfalse(lambda x: x.isupper(), x))
def standard_deviation(number: list) -> float:
"""
Return the standard deviation of a list of numbers
Example usage:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
print(uf.standard_deviation(numbers))
output = 3.162
"""
return round((np.std(number)), 3)
def pdf_to_str(document_path: str) -> str:
"""
Convert pdf documents to strings.
Example usage:
to_str = uf.pdf_to_str("rand.pdf")
output: a long string of all the pdf contents! :)
"""
with fitz.open(document_path) as doc:
text = str()
for page in doc:
text += page.getText()
return text
def class_distribution(column_name, class_label_series):
"""
Output a plot showing the class distribution.
Example usage:
file_location = "FILE_LOCATION"
df_original = pd.read_csv(file_location)
df_class_label = df_original[["class_label"]]
uf.class_distribution("class_label", df_class_label)
"""
sns.countplot(x=column_name, data=class_label_series)
plt.show()
nlp = spacy.load("en_core_web_sm")
def stemming(your_string: str) -> list:
"""
Output a list of stemmed stings. Algorithm used PotterStemmer
Example usage:
your_string = "The best definition of man is: a being that walks on two legs and is ungrateful"
stemming(your_string)
output: ['the', 'best', 'definit', 'of', 'man', 'is', ':', 'a', 'be', 'that', 'walk', 'on', 'two', 'leg', 'and', 'is', 'ungrat']
"""
doc = nlp(your_string)
return [PorterStemmer().stem(token.text) for token in doc]
def random_int_generator(low: int, high: int, total_number_you_want: int) -> list:
"""
Return a list of integer number based on uers given limit
low: lower int, including.
high: higher int, excluding
total_number_you_want: Total unique random int you want
Example usage:
>> your_rand_int_list = random_int_generator(1, 55, 6)
>> print(your_rand_int_list)
>> [46, 47, 17, 51, 21, 22]
Source: https://stackoverflow.com/a/22842533/12946268
"""
def random_gen(lower, higher):
while True:
yield random.randrange(lower, higher)
gen = random_gen(low, high)
items = set()
for x in itertools.takewhile(lambda x: len(items) < total_number_you_want, gen):
items.add(x)
return list(items)
deutsche_pipeline = spacy.load("de_core_news_sm")
def deutsche_remove_stop_words_and_punc(text: str, print_text=False) -> str:
"""
Returns a string with stop words and punctuation removed for Deutsche text
text: input Deutsche text
Example usage:
>> remove_stop_words_and_punct_de("Die etymologischen Vorformen von deutsch bedeuteten ursprünglich „zum Volk gehörig“")
>> print(remove_stop_words_and_punct_de)
>> etymologischen Vorformen deutsch bedeuteten ursprünglich Volk gehörig
Source: https://github.com/cj2001/nodes2021_kg_workshop/blob/main/notebooks/00-populate_basic_graph.ipynb
"""
result = list()
raw_doc = deutsche_pipeline(text)
for token in raw_doc:
if print_text:
print(token, token.is_stop)
print("|-----------------|")
if not token.is_stop and not token.is_punct:
result.append(str(token))
result = " ".join(result)
return result
model_name = "distilbert-base-uncased"
tokenizer = DistilBertTokenizer.from_pretrained(model_name)
def custom_padding(sentence_1: str, sentence_2: str):
"""
For this required packages is "transformer"
Compare two sentence and then pad (post) the one that has less tokens with the
pad_token_id (defined by the model)
model used for this example: "distilbert-base-uncased"
Example usage:
>>>sentence_1 = "I’ve been waiting for uploading this on github for a long time"
>>>sentence_2 = "This is not that bad!"
>>>print(custom_padding(sentence_1, sentence_2))
>>> Sentence 1: [1045, 1521, 2310, 2042, 3403, 2005, 2039, 18570, 2023,
2006, 21025, 2705, 12083, 2005, 1037, 2146, 2051], Sentence 2: [2023, 2003,
2025, 2008, 2919, 999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
"""
s1_token = tokenizer.tokenize(sentence_1)
s2_token = tokenizer.tokenize(sentence_2)
s1_id = tokenizer.convert_tokens_to_ids(s1_token)
s2_id = tokenizer.convert_tokens_to_ids(s2_token)
if len(s2_id) < len(s1_id):
for _ in range(len(s1_id) - len(s2_id)):
s2_id.append(tokenizer.pad_token_id)
else:
for _ in range(len(s2_id) - len(s1_id)):
s1_id.append(tokenizer.pad_token_id)
return f"Sentence 1: {s1_id}, Sentence 2: {s2_id}"
def remove_stopwords_german(text):
'''
Remove stopwards from german text.
This can also be used to clean texts in csv format
Examople usage for a single text:
>>>remove_stopwords_german("Jeder Satz wurde per Crowdsourcing entweder als unterstützendes Argument, als angreifendes Argument oder als kein Argument in Bezug auf das Thema kommentiert")
>>>Satz wurde per Crowdsourcing entweder unterstützendes Argument angreifendes Argument Argument Bezug Thema kommentiert
Example usage of text cleaning in csv format. It will remove the stopwards for all the item in the column "sentence"
>>> train_raw['data'] = train_raw['sentence'].apply(remove_stopwords_german)
Source: https://towardsdatascience.com/cross-topic-argument-mining-learning-how-to-classify-texts-1d9e5c00c4cc
'''
stpword = stopwords.words("german")
no_punctuation = [char for char in text if char not in string.punctuation]
no_punctuation = "".join(no_punctuation)
return " ".join(
[word for word in no_punctuation.split() if word.lower() not in stpword]
)
def combining_csv(current_dir):
'''
Combined several csv file to one csv (must same header).
Please change the delimeter and other arguments according to need.
Example Usage:
>>>combine_csv("/content/drive/MyDrive/MyDatasetCollection/argument_mining/train")
This will create a combined csv file with name "combined_csv.tsv" on the provided location
Reference:
https://www.freecodecamp.org/news/how-to-combine-multiple-csv-files-with-8-lines-of-code-265183e0854/
[For the argument passed for pd.read_csv]: https://stackoverflow.com/questions/39303912/tfidfvectorizer-in-scikit-learn-valueerror-np-nan-is-an-invalid-document
'''
os.chdir(current_dir)
extension = 'tsv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
print(all_filenames)
combined_csv = pd.concat([pd.read_csv(f, header=0, delimiter="\t",
quoting=csv.QUOTE_NONE, encoding='utf-8') for f in all_filenames])
combined_csv.to_csv(current_dir + "/combined_csv.tsv", index=False, encoding='utf-8-sig')
def save_pickle_object(data, path: str):
'''
Path format: "/content/demo/pickleobject/"
use case:
>>>save_pickle_object(data, "/content/demo/pickleobject/")
the data object will be saved in the sub directory mentioned in here it is
pickleobject.
Therefore the file path will look like this inside the pickleobject folder
-> "/content/demo/pickleobject/data.pickle"
'''
with open(path + '/data.pickle', 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
def load_pickle_object(path: str):
'''
Provide the path where the pickle object resides.
Return will be the dumped pickle object.
use case:
>>>pklobject = load_pickle_object("/content/demo/pickleobject/data.pickle")
>>>type(pklobject)
>>>dict
'''
with open(path, 'rb') as f:
data = pickle.load(f)
return data