Skip to content

Commit 845782e

Browse files
Stackoverflow tag predictor
0 parents  commit 845782e

File tree

4 files changed

+2209
-0
lines changed

4 files changed

+2209
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

app.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import pandas as pd
2+
import numpy as np
3+
import re
4+
from tqdm import tqdm
5+
import warnings
6+
import nltk
7+
nltk.download('punkt')
8+
warnings.filterwarnings("ignore")
9+
from sklearn.feature_extraction.text import CountVectorizer
10+
from sklearn.feature_extraction.text import TfidfVectorizer
11+
from nltk.corpus import stopwords
12+
from nltk.tokenize import word_tokenize
13+
from nltk.stem.snowball import SnowballStemmer
14+
from sklearn.model_selection import train_test_split
15+
from sklearn.multiclass import OneVsRestClassifier
16+
from sklearn.linear_model import SGDClassifier
17+
from sklearn.linear_model import LogisticRegression
18+
from sklearn import metrics
19+
import joblib
20+
from joblib import load
21+
from flask_ngrok import run_with_ngrok
22+
from flask import Flask, request, render_template
23+
app = Flask(__name__)
24+
25+
# Stopwords list
26+
stop_words = {'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"}
27+
28+
# This will work as a text spliter
29+
def text_splitter(text):
30+
return text.split()
31+
32+
# To load model, Vectorizer and tag list
33+
model = joblib.load("stackoverflow_model_100k.pkl")
34+
vectorizer = joblib.load("stackoverflow_tfidf_vectorizer_100k.pkl")
35+
tags = pd.read_csv("tag_list.csv")
36+
tag_list = tags["Tags"].to_list()
37+
38+
# Data Preprocessing for new question
39+
def text_preprocessing(title,question):
40+
# Stemmer is converting like: eating --> eat or running --> run
41+
stemmer = SnowballStemmer("english")
42+
43+
# To replace <code>...</code> with space " "
44+
question=re.sub('<code>(.*?)</code>', '', question, flags=re.MULTILINE|re.DOTALL)
45+
46+
# To remove all HTML tags from questions
47+
html_cleaner = re.compile('<.*?>')
48+
question = re.sub(html_cleaner, ' ', str(question.encode("utf-8")))
49+
50+
title=title.encode('utf-8')
51+
52+
# Concatinate title and question
53+
question=str(title)+" "+str(question)
54+
55+
# To remove all characters instead of alphabets
56+
question=re.sub(r'[^A-Za-z]+',' ',question)
57+
58+
# First, we convert all text to lowercase and Word tokenize will convert
59+
# "This is data preprocessing" --> ["This","is","data","preprocessing"]
60+
words=word_tokenize(str(question.lower()))
61+
62+
# Removing all single letter and stopwords from question except letter 'c' because c is a prog. language
63+
# After removing all single words join then with space
64+
question= ' '.join(str(stemmer.stem(j)) for j in words if j not in stop_words and (len(j)!=1 or j=='c'))
65+
return question
66+
67+
# To predict results
68+
def predict1(title,question):
69+
70+
# Step 1: Text preprocessing
71+
fine_text = text_preprocessing(title,question)
72+
73+
# Step 2: Make transform of your text to features
74+
vector = vectorizer.transform([fine_text])
75+
76+
# Step 3: Predict outputs
77+
prediction = model.predict(vector)
78+
79+
# Get the array of the prediction
80+
prediction_results = prediction.toarray()[0]
81+
final_tags = []
82+
83+
# Find relevent tags according to the results
84+
for i in range(5500):
85+
if prediction_results[i]==1:
86+
final_tags.append(tag_list[i])
87+
return final_tags
88+
89+
# Home page
90+
@app.route('/')
91+
def homepage():
92+
return render_template('index.html')
93+
94+
# To get input from frontend and send the results to frontend
95+
@app.route('/', methods= ["POST"])
96+
def background_process():
97+
if request.method == 'POST':
98+
title = request.form.get('title')
99+
question = request.form.get('question')
100+
result = predict1(title,question)
101+
if len(result) == 0:
102+
result = ["Not Find Any Results"]
103+
if title:
104+
result1 = tuple(result)
105+
return render_template('index.html',result1 = result)
106+
else:
107+
return render_template('index.html',error='Please provide atleast title to get results')
108+
109+
# To start the application
110+
if __name__ == "__main__":
111+
app.run(debug=True,host="0.0.0.0",port="5000")

0 commit comments

Comments
 (0)