-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
156 lines (125 loc) · 4.38 KB
/
utils.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
import pandas as pd
import numpy as np
import streamlit as st
from nltk import word_tokenize
from nltk.util import ngrams
from sklearn.feature_extraction.text import CountVectorizer
import string
import re
import nltk
from nltk.corpus import stopwords
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from nltk.stem import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
import requests
from bs4 import BeautifulSoup
from sklearn.metrics import pairwise_distances
import dill as pickle
nltk.download("stopwords")
stemmer = PorterStemmer()
vectorizer = TfidfVectorizer()
stpwrds = set(stopwords.words("english"))
additional_stopwords = set(('ie', 'eg', 'cf', 'etc', 'et', 'al'))
stpwrds.update(additional_stopwords)
def get_top_ngram(corpus, n=None):
vec = CountVectorizer(ngram_range=(n, n)).fit(corpus)
bag_of_words = vec.transform(corpus)
sum_words = bag_of_words.sum(axis=0)
words_freq = [(word, sum_words[0, idx])
for word, idx in vec.vocabulary_.items()]
words_freq =sorted(words_freq, key = lambda x: x[1], reverse=True)
return words_freq[:10]
def remove_latex(s):
regex = r"(\$+)(?:(?!\1)[\s\S])*\1"
subst = ""
result = re.sub(regex, subst, s, 0, re.MULTILINE)
return result
def remove_punctuation(s):
s = re.sub(r'\d+', '', s) # remove numbers
s = "".join([char.lower() for char in s if char not in string.punctuation]) # remove punctuations and convert characters to lower case
s = re.sub('\s+', ' ', s).strip() # substitute multiple whitespace with single whitespace
return s
def remove_linebreaks(s):
return s.replace("\n", " ")
def tokenize(s):
return word_tokenize(s, language="english")
def remove_stopwords(s):
return [w for w in s if not w in stpwrds]
def stem(s):
return " ".join([stemmer.stem(w.lower()) for w in s])
def vectorize(s):
return vectorizer.fit_transform(s)
def lemmatizer(s):
lemmatizer = nltk.stem.WordNetLemmatizer()
s = [lemmatizer.lemmatize(w.lower()) for w in s]
return s
def clean(s):
s = remove_latex(s)
s = remove_punctuation(s)
s = remove_linebreaks(s)
s = tokenize(s)
s = remove_stopwords(s)
# if lemma == True and stem==True:
# stem = False
# if lemma:
# s = lemmatizer(s)
# if stem:
s = stem(s)
return s
def show_wordcloud(data, maxwords):
cloud = WordCloud(
background_color='white',
max_words=maxwords,
max_font_size=30,
scale=3,
random_state=1)
output=cloud.generate(str(data))
fig = plt.figure(1, figsize=(12, 12))
plt.axis('off')
plt.imshow(output)
plt.show()
return fig
def plot_tsne():
fig = plt.figure()
ax = fig.add_subplot(111, aspect=1)
ax.plot(X_tsne[mask_astro][:, 0], X_tsne[mask_astro][:, 1], ".", alpha=0.5, c="C0", label="Astro")
ax.plot(X_tsne[mask_bio][:, 0], X_tsne[mask_bio][:, 1], ".", alpha=0.5, c="C1", label="Bio")
ax.set_xlabel("t-SNE 1")
ax.set_ylabel("t-SNE 2")
ax.legend()
fig.tight_layout()
def get_paper_information(paper_id:str) -> dict or str:
url = f'https://arxiv.org/abs/{paper_id}'
try:
req = requests.get(url)
req.raise_for_status()
except requests.exceptions.HTTPError as err:
return str(err)
soup = BeautifulSoup(req.text, 'html.parser')
content = soup.find('div', {'id':'abs'})
data = {}
data['title'] = content.find('h1', {'class': 'title mathjax'})
data['authors'] = content.find('div', {'class':'authors'})
data['abstract'] = content.find('blockquote', {'class', 'abstract mathjax'})
# cleaning html
for key, tag in data.items():
tag.span.decompose()
data[key] = tag.text.strip()
data['subject'] = soup.find('div', {'class':'browse'}).find('div', {'class':'current'}).text.strip()
return data
def give_recomm(data, vectorizer, df, n=5):
with open('X.pickle', 'rb') as f:
X = pickle.load(f)
new_input = clean(data)
new_input = vectorizer.transform([data])
# features = vectorizer.get_feature_names()
ndb_dist_i = pairwise_distances(X, new_input)[:, 0]
# sort_ind_i = ndb_dist_i.argsort()
newdf = df.copy(deep=True)
newdf.insert(1, "dist", ndb_dist_i)
newdf.sort_values("dist", ascending=True, inplace=True)
# st.write(sort_ind_i)
newdf = newdf.iloc[1:,]
st.write(newdf["title"].head(n))
return