ArchitSharma commited on
Commit
06e3a16
·
1 Parent(s): 07c6381

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +133 -0
utils.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import requests
3
+ import docx2txt
4
+ from io import StringIO
5
+ from PyPDF2 import PdfFileReader
6
+
7
+ from bs4 import BeautifulSoup
8
+ from nltk.tokenize import sent_tokenize
9
+
10
+ emoji_pattern = re.compile(
11
+ "["
12
+ u"\U0001F600-\U0001F64F" # emoticons
13
+ u"\U0001F300-\U0001F5FF" # symbols & pictographs
14
+ u"\U0001F680-\U0001F6FF" # transport & map symbols
15
+ u"\U0001F1E0-\U0001F1FF" # flags (iOS)
16
+ u"\U00002702-\U000027B0"
17
+ u"\U000024C2-\U0001F251"
18
+ "]+",
19
+ flags=re.UNICODE,
20
+ )
21
+
22
+
23
+ def clean_text(x):
24
+ x = x.encode("ascii", "ignore").decode() # unicode
25
+ x = re.sub(r"https*\S+", " ", x) # url
26
+ x = re.sub(r"@\S+", " ", x) # mentions
27
+ x = re.sub(r"#\S+", " ", x) # hastags
28
+ x = re.sub(r"\s{2,}", " ", x) # over spaces
29
+ x = emoji_pattern.sub(r"", x) # emojis
30
+ x = re.sub("[^.,!?A-Za-z0-9]+", " ", x) # special charachters except .,!?
31
+
32
+ return x
33
+
34
+
35
+ def fetch_article_text(url: str):
36
+
37
+ r = requests.get(url)
38
+ soup = BeautifulSoup(r.text, "html.parser")
39
+ results = soup.find_all(["h1", "p"])
40
+ text = [result.text for result in results]
41
+ ARTICLE = " ".join(text)
42
+ ARTICLE = ARTICLE.replace(".", ".<eos>")
43
+ ARTICLE = ARTICLE.replace("!", "!<eos>")
44
+ ARTICLE = ARTICLE.replace("?", "?<eos>")
45
+ sentences = ARTICLE.split("<eos>")
46
+ current_chunk = 0
47
+ chunks = []
48
+ for sentence in sentences:
49
+ if len(chunks) == current_chunk + 1:
50
+ if len(chunks[current_chunk]) + len(sentence.split(" ")) <= 500:
51
+ chunks[current_chunk].extend(sentence.split(" "))
52
+ else:
53
+ current_chunk += 1
54
+ chunks.append(sentence.split(" "))
55
+ else:
56
+ print(current_chunk)
57
+ chunks.append(sentence.split(" "))
58
+
59
+ for chunk_id in range(len(chunks)):
60
+ chunks[chunk_id] = " ".join(chunks[chunk_id])
61
+
62
+ return ARTICLE, chunks
63
+
64
+
65
+ def preprocess_text_for_abstractive_summarization(tokenizer, text):
66
+ sentences = sent_tokenize(text)
67
+
68
+ # initialize
69
+ length = 0
70
+ chunk = ""
71
+ chunks = []
72
+ count = -1
73
+ for sentence in sentences:
74
+ count += 1
75
+ combined_length = (
76
+ len(tokenizer.tokenize(sentence)) + length
77
+ ) # add the no. of sentence tokens to the length counter
78
+
79
+ if combined_length <= tokenizer.max_len_single_sentence: # if it doesn't exceed
80
+ chunk += sentence + " " # add the sentence to the chunk
81
+ length = combined_length # update the length counter
82
+
83
+ # if it is the last sentence
84
+ if count == len(sentences) - 1:
85
+ chunks.append(chunk.strip()) # save the chunk
86
+
87
+ else:
88
+ chunks.append(chunk.strip()) # save the chunk
89
+
90
+ # reset
91
+ length = 0
92
+ chunk = ""
93
+
94
+ # take care of the overflow sentence
95
+ chunk += sentence + " "
96
+ length = len(tokenizer.tokenize(sentence))
97
+
98
+ return chunks
99
+
100
+
101
+ def read_pdf(file):
102
+ pdfReader = PdfFileReader(file)
103
+ count = pdfReader.numPages
104
+ all_page_text = ""
105
+ for i in range(count):
106
+ page = pdfReader.getPage(i)
107
+ all_page_text += page.extractText()
108
+
109
+ return all_page_text
110
+
111
+
112
+ def read_text_from_file(file):
113
+
114
+ # read text file
115
+ if file.type == "text/plain":
116
+ # To convert to a string based IO:
117
+ stringio = StringIO(file.getvalue().decode("utf-8"))
118
+
119
+ # To read file as string:
120
+ file_content = stringio.read()
121
+
122
+ # read pdf file
123
+ elif file.type == "application/pdf":
124
+ file_content = read_pdf(file)
125
+
126
+ # read docx file
127
+ elif (
128
+ file.type
129
+ == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
130
+ ):
131
+ file_content = docx2txt.process(file)
132
+
133
+ return file_content