-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsih.py
More file actions
787 lines (640 loc) · 30.8 KB
/
sih.py
File metadata and controls
787 lines (640 loc) · 30.8 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
from flask import Flask, render_template, request, redirect, url_for, session,jsonify
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re
import pickle
import pandas as pd
import xgboost as xgb
from sklearn.model_selection import train_test_split
import warnings
import numpy as np
import random
from random import choice, sample
import PyPDF2
from docx import Document
model = pickle.load(open("sih1.pkl", "rb"))
app = Flask(__name__)
app.secret_key = 'xyzsdfg'
app.config['MYSQL_HOST'] = 'sql12.freesqldatabase.com'
app.config['MYSQL_USER'] = 'sql12771521'
app.config['MYSQL_PASSWORD'] = 's7fwEqDGS7'
app.config['MYSQL_DB'] = 'sql12771521'
mysql = MySQL(app)
@app.route('/')
# editing it now :-
# @app.route('/login01a', methods =['GET', 'POST'])
# def login01a():
# mesage = ''
# if request.method == 'POST' and 'email' in request.form and 'password' in request.form:
# email = request.form['email']
# password = request.form['password']
# cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
# cursor.execute('SELECT * FROM users WHERE email = % s AND password = % s', (email, password, ))
# user = cursor.fetchone()
# if user:
# session['loggedin'] = True
# # session['userid'] = user['userid']
# session['name'] = user['name']
# email_a = user['email']
# # branch= user['Branch']
# # roll_no= user['Roll_no']
# # score_10= user['10th_Score']
# # score_12= user['12th_Score']
# # cgpa= user['CGPA']
# # attendence= user['Attendance']
# # mesage = 'Logged in successfully !'
# #get data
# # cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
# # cursor.execute("SELECT name, email FROM user WHERE Branch='AIML';")
# # myresult=cursor.fetchall()
# return render_template('home.html', d=email_a)
# else:
# mesage = 'Please enter correct email / password !'
# return render_template('login.html', mesage = mesage)
# @app.route('/register01', methods =['GET', 'POST'])
# def register01():
# mesage = ''
# if request.method == 'POST' and 'name' in request.form and 'password' in request.form and 'email' in request.form :
# userName = request.form['name']
# password = request.form['password']
# email = request.form['email']
# try:
# conn = mysql.connect
# if conn:
# cursor = conn.cursor(MySQLdb.cursors.DictCursor)
# # cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
# cursor.execute('SELECT * FROM usersss WHERE email = % s', (email, ))
# account = cursor.fetchone()
# if account:
# mesage = 'Account already exists !'
# elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
# mesage = 'Invalid email address !'
# elif not userName or not password or not email:
# mesage = 'Please fill out the form !'
# else:
# cursor.execute('INSERT INTO users (name, email, password) VALUES (% s, % s, % s)', (userName, email, password ))
# conn.commit()
# cursor.close()
# mesage = 'You have successfully registered !'
# else:
# return "Connection not established."
# except Exception as e:
# return f"An error occurred: {str(e)}"
# elif request.method == 'POST':
# mesage = 'Please fill out the form !'
# return render_template('register01.html', mesage = mesage)
@app.route('/login', methods =['GET', 'POST'])
def login():
mesage = ''
if request.method == 'POST' and 'email' in request.form and 'password' in request.form:
email = request.form['email']
password = request.form['password']
cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM users WHERE email = % s AND password = % s', (email, password, ))
# cursor.execute(f'SELECT * FROM user WHERE email = "{email}" AND password = "{password}"')
user = cursor.fetchone()
if user:
session['loggedin'] = True
# session['userid'] = user['userid']
session['name'] = user['name']
email_a = user['email']
# branch= user['Branch']
# roll_no= user['Roll_no']
# score_10= user['10th_Score']
# score_12= user['12th_Score']
# cgpa= user['CGPA']
# attendence= user['Attendance']
# mesage = 'Logged in successfully !'
return render_template('home.html', d=email_a)
else:
mesage = 'Please enter correct email / password !'
return render_template('login.html', mesage = mesage)
# @app.route('/logout')
# def logout():
# session.pop('loggedin', None)
# # session.pop('userid', None)
# session.pop('email', None)
# # #yaha se
# # session.clear()
# # #yaha tak
# return redirect(url_for('login'))
@app.route('/register', methods =['GET', 'POST'])
def register():
mesage = ''
if request.method == 'POST' and 'name' in request.form and 'password' in request.form and 'email' in request.form :
userName = request.form['name']
password = request.form['password']
email = request.form['email']
try:
conn = mysql.connect
if conn:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
# cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM users WHERE email = % s', (email, ))
account = cursor.fetchone()
if account:
mesage = 'Account already exists !'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
mesage = 'Invalid email address !'
elif not userName or not password or not email:
mesage = 'Please fill out the form !'
else:
cursor.execute('INSERT INTO users (name, email, password) VALUES (% s, % s, % s)', (userName, email, password ))
# cursor.execute(f'INSERT INTO user (name, email, password) VALUES ({userName}, {email}, {password})')
conn.commit()
cursor.close()
mesage = 'You have successfully registered !'
else:
return "Connection not established."
except Exception as e:
return f"An error occurred: {str(e)}"
elif request.method == 'POST':
mesage = 'Please fill out the form !'
return render_template('register.html', mesage = mesage)
@app.route('/login01a', methods =['GET', 'POST'])
def login01a():
mesage = ''
if request.method == 'POST' and 'email' in request.form and 'password' in request.form:
email = request.form['email']
password = request.form['password']
cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM users WHERE email = % s AND password = % s', (email, password, ))
user = cursor.fetchone()
if user:
session['loggedin'] = True
# session['userid'] = user['userid']
session['name'] = user['name']
email_a = user['email']
# branch= user['Branch']
# roll_no= user['Roll_no']
# score_10= user['10th_Score']
# score_12= user['12th_Score']
# cgpa= user['CGPA']
# attendence= user['Attendance']
# mesage = 'Logged in successfully !'
#get data
# cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
# cursor.execute("SELECT name, email FROM user WHERE Branch='AIML';")
# myresult=cursor.fetchall()
return render_template('home01.html', d=email_a)
else:
mesage = 'Please enter correct email / password !'
return render_template('login01.html', mesage = mesage)
@app.route('/register01', methods =['GET', 'POST'])
def register01():
mesage = ''
if request.method == 'POST' and 'name' in request.form and 'password' in request.form and 'email' in request.form :
userName = request.form['name']
password = request.form['password']
email = request.form['email']
try:
conn = mysql.connect
if conn:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
# cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM users WHERE email = % s', (email, ))
account = cursor.fetchone()
if account:
mesage = 'Account already exists !'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
mesage = 'Invalid email address !'
elif not userName or not password or not email:
mesage = 'Please fill out the form !'
else:
cursor.execute('INSERT INTO users (name, email, password) VALUES (% s, % s, % s)', (userName, email, password ))
conn.commit()
cursor.close()
mesage = 'You have successfully registered !'
else:
return "Connection not established."
except Exception as e:
return f"An error occurred: {str(e)}"
elif request.method == 'POST':
mesage = 'Please fill out the form !'
return render_template('register01.html', mesage = mesage)
@app.route('/login01')
def login01():
return render_template('login01.html')
# yaha tak :)
@app.route('/home')
def home():
return render_template('home.html')
# @app.route('/test', methods =['GET', 'POST'])
# def test():
# data1=request.files['pdf']
# # pip install PyPDF2 python-docx nltk spacy scikit-learn
# def pdf_to_text(pdf_file):
# with open(pdf_file, 'rb') as file:
# reader = PyPDF2.PdfReader(file)
# text = ''
# for page in reader.pages:
# text += page.extract_text()
# return text
# from docx import Document
# def docx_to_text(docx_file):
# doc = Document(docx_file)
# return '\n'.join([para.text for para in doc.paragraphs])
# def extract_skills_section(text):
# lines = text.splitlines()
# skills_section = ""
# capture = False
# for line in lines:
# if "skills" in line.lower():
# capture = True
# elif capture:
# if line.strip() == "" or any(header in line.lower() for header in ["experience", "education", "work"]):
# break
# skills_section += line + " "
# return skills_section.strip()
# def extract_skills(skills_section):
# skills = skills_section.split(",") # Assume skills are comma-separated
# return [skill.strip() for skill in skills if skill.strip()]
# def process_resume(file):
# if file.endswith('.pdf'):
# text = pdf_to_text(file)
# elif file.endswith('.docx'):
# text = docx_to_text(file)
# else:
# raise ValueError("Unsupported file type")
# skills_section = extract_skills_section(text)
# skills = extract_skills(skills_section)
# skills_string = ' '.join(skills)
# # pr int(skills_string)
# return skills_string
# skills=process_resume(data1)
# return render_template('after.html', s=skills)
@app.route('/test01', methods=['POST'])
def test01():
if 'pdf' not in request.files:
# flash('No file part in the request')
return redirect(url_for('upload_page')) # Redirect to an upload page
file = request.files['pdf']
pos=request.form['position']
try:
conn = mysql.connect # Assuming you have mysql configured
if conn:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
query = "SELECT skills FROM openings WHERE position = %s"
cursor.execute(query, (pos,))
results = cursor.fetchone()
# results = cursor.fetchall()
cursor.close()
conn.commit()
if results and 'skills' in results:
sk = results['skills']
else:
sk = "" # Handle the case where no matching position is found
else:
return "Connection not established"
except Exception as e:
return f"An error occurred: {str(e)}"
if file.filename == '':
# flash('No selected file')
return redirect(url_for('upload_page'))
if file:
# Get the file extension
file_extension = file.filename.rsplit('.', 1)[1].lower()
def pdf_to_text(pdf_file):
reader = PyPDF2.PdfReader(pdf_file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def docx_to_text(docx_file):
doc = Document(docx_file)
return '\n'.join([para.text for para in doc.paragraphs])
def extract_skills_section(text):
lines = text.splitlines()
skills_section = ""
capture = False
for line in lines:
if "skills" in line.lower():
capture = True
elif capture:
if line.strip() == "" or any(header in line.lower() for header in ["experience", "education", "work"]):
break
skills_section += line + " "
return skills_section.strip()
# def extract_skills_section(text):
# lines = text.splitlines()
# capture = False
# skills_lines = []
# section_headers = ["experience", "education", "project", "certifications", "summary", "work", "internship", "achievement", "objective"]
# max_skills_lines = 10
# for i, line in enumerate(lines):
# stripped_line = line.strip()
# lower_line = stripped_line.lower()
# if "skill" in lower_line and len(lower_line) <= 30:
# capture = True
# continue
# if capture:
# if any(header in lower_line for header in section_headers) or len(skills_lines) >= max_skills_lines:
# break
# if 2 < len(stripped_line.split()) < 20:
# skills_lines.append(stripped_line)
# extracted = ' '.join(skills_lines)
# extracted = re.sub(r'[^a-zA-Z0-9,+.-]', ' ', extracted)
# parts = re.split(r',|•|-|•', extracted)
# skill_candidates = []
# for part in parts:
# clean = part.strip()
# if 1 < len(clean.split()) <= 5:
# skill_candidates.append(clean)
# final_skills = ', '.join(sorted(set(skill_candidates), key=skill_candidates.index))
# return final_skills
def extract_skills(skills_section):
skills = skills_section.split(",") # Assume skills are comma-separated
return [skill.strip() for skill in skills if skill.strip()]
def process_resume(file):
if file_extension == 'pdf':
text = pdf_to_text(file)
elif file_extension == 'docx':
text = docx_to_text(file)
else:
raise ValueError("Unsupported file type")
skills_section = extract_skills_section(text)
skills = extract_skills(skills_section)
skills_string = ', '.join(skills)
return skills_string
skills = process_resume(file)
# def normalize_skills_string(skills_string):
# Lowercase, split by comma, strip each skill
# return [skill.strip().lower() for skill in skills_string.split(',') if skill.strip()]
def normalize_skills_string(skills_string):
# Replace anything that's not a letter or + with space, then split
cleaned = re.sub(r'[^a-zA-Z+]', ' ', skills_string.lower())
return [skill.strip() for skill in cleaned.split() if skill.strip()]
# def replace_non_letters_with_comma(input_string):
# # Replace all characters that are not letters (a-z or A-Z) with a comma
# return re.sub(r'[^a-zA-Z+]', ',', input_string)
# skills=replace_non_letters_with_comma(skills)
candidate_skills_list = normalize_skills_string(skills)
position_skills_list = normalize_skills_string(sk)
print("Raw candidate resume skills string:", skills)
print("Raw position DB skills string:", sk)
# def find_common_skills(text1, text2):
# # Split the texts by commas and convert to sets
# skills1 = set(text1.split(','))
# skills2 = set(text2.split(','))
# # Find common skills
# common_skills = skills1.intersection(skills2)
# # Join the common skills into a string separated by commas
# return ','.join(common_skills)
def find_common_and_unmatched_skills(candidate_skills_list, position_skills_list):
candidate_skills = set(candidate_skills_list)
position_skills = set(position_skills_list)
matched_skills = candidate_skills & position_skills
unmatched_skills = position_skills - candidate_skills
return ', '.join(sorted(matched_skills)), ', '.join(sorted(unmatched_skills))
candidate_skills_list = normalize_skills_string(skills)
position_skills_list = normalize_skills_string(sk)
matched_skills, unmatched_skills = find_common_and_unmatched_skills(candidate_skills_list, position_skills_list)
return render_template(
'home01.html',
matched=matched_skills,
unmatched=unmatched_skills,
s1=candidate_skills_list,
s2=position_skills_list,
candidate=', '.join(candidate_skills_list),
position=', '.join(position_skills_list)
)
# matched_skills, unmatched_skills = find_common_and_unmatched_skills(skills, sk)
# return render_template('home01.html', matched=matched_skills, unmatched=unmatched_skills, candidate=skills, position=sk)
# skills1=find_common_skills(skills,sk)
# return render_template('home01.html', s=skills1,s1=skills,s2=sk)
@app.route('/test', methods=['POST'])
def test():
if 'pdf' not in request.files:
# flash('No file part in the request')
return redirect(url_for('upload_page')) # Redirect to an upload page
file = request.files['pdf']
pos=request.form['position']
try:
conn = mysql.connect # Assuming you have mysql configured
if conn:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
query = "SELECT skills FROM openings WHERE position = %s"
cursor.execute(query, (pos,))
results = cursor.fetchone()
# results = cursor.fetchall()
cursor.close()
conn.commit()
if results and 'skills' in results:
sk = results['skills']
else:
sk = "" # Handle the case where no matching position is found
else:
return "Connection not established"
except Exception as e:
return f"An error occurred: {str(e)}"
if file.filename == '':
# flash('No selected file')
return redirect(url_for('upload_page'))
if file:
# Get the file extension
file_extension = file.filename.rsplit('.', 1)[1].lower()
def pdf_to_text(pdf_file):
reader = PyPDF2.PdfReader(pdf_file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def docx_to_text(docx_file):
doc = Document(docx_file)
return '\n'.join([para.text for para in doc.paragraphs])
def extract_skills_section(text):
lines = text.splitlines()
skills_section = ""
capture = False
for line in lines:
if "skills" in line.lower():
capture = True
elif capture:
if line.strip() == "" or any(header in line.lower() for header in ["experience", "education", "work"]):
break
skills_section += line + " "
return skills_section.strip()
# def extract_skills_section(text):
# lines = text.splitlines()
# capture = False
# skills_lines = []
# section_headers = ["experience", "education", "project", "certifications", "summary", "work", "internship", "achievement", "objective"]
# max_skills_lines = 10
# for i, line in enumerate(lines):
# stripped_line = line.strip()
# lower_line = stripped_line.lower()
# if "skill" in lower_line and len(lower_line) <= 30:
# capture = True
# continue
# if capture:
# if any(header in lower_line for header in section_headers) or len(skills_lines) >= max_skills_lines:
# break
# if 2 < len(stripped_line.split()) < 20:
# skills_lines.append(stripped_line)
# extracted = ' '.join(skills_lines)
# extracted = re.sub(r'[^a-zA-Z0-9,+.-]', ' ', extracted)
# parts = re.split(r',|•|-|•', extracted)
# skill_candidates = []
# for part in parts:
# clean = part.strip()
# if 1 < len(clean.split()) <= 5:
# skill_candidates.append(clean)
# final_skills = ', '.join(sorted(set(skill_candidates), key=skill_candidates.index))
# return final_skills
def extract_skills(skills_section):
skills = skills_section.split(",") # Assume skills are comma-separated
return [skill.strip() for skill in skills if skill.strip()]
def process_resume(file):
if file_extension == 'pdf':
text = pdf_to_text(file)
elif file_extension == 'docx':
text = docx_to_text(file)
else:
raise ValueError("Unsupported file type")
skills_section = extract_skills_section(text)
skills = extract_skills(skills_section)
skills_string = ' '.join(skills)
return skills_string
skills = process_resume(file)
def replace_non_letters_with_comma(input_string):
# Replace all characters that are not letters (a-z or A-Z) with a comma
return re.sub(r'[^a-zA-Z+]', ',', input_string)
skills=replace_non_letters_with_comma(skills)
def find_common_skills(text1, text2):
# Split the texts by commas and convert to sets
skills1 = set(text1.split(','))
skills2 = set(text2.split(','))
# Find common skills
common_skills = skills1.intersection(skills2)
# Join the common skills into a string separated by commas
return ','.join(common_skills)
skills1=find_common_skills(skills,sk)
return render_template('home.html', s=skills1,s1=skills,s2=sk)
@app.route('/match_skills', methods=['POST'])
# def match_skills():
# def get_matching_professions(input_skills):
# input_skills_set = set(input_skills.lower().split(","))
# try:
# conn = mysql.connect # Assuming you have mysql configured
# if conn:
# cursor = conn.cursor(MySQLdb.cursors.DictCursor)
# cursor.execute("SELECT professor, skills FROM prof")
# results = cursor.fetchall()
# cursor.close()
# conn.commit()
# else:
# return "Connection not established"
# except Exception as e:
# return f"An error occurred: {str(e)}"
# matches = []
# for row in results:
# profession = row['professor']
# skills = row['skills']
# profession_skills_set = set(skills.lower().split(","))
# match_score = len(input_skills_set.intersection(profession_skills_set))
# if match_score > 0:
# matches.append((profession, match_score))
# matches.sort(key=lambda x: x[1], reverse=True) # Sort by match score
# # Convert the top 5 matches into a concatenated string
# top_matches = " ".join([f"{profession}({score})" for profession, score in matches[:5]])
# return top_matches
# # def get_matching_professions(input_skills):
# # input_skills_set = set(input_skills.lower().split(","))
# # # conn = sqlite3.connect('database.db') # Connect to your database
# # # cursor = conn.cursor()
# # # cursor.execute("SELECT profession_name, skills FROM prof")
# # # results = cursor.fetchall()
# # try:
# # conn = mysql.connect
# # if conn:
# # cursor = conn.cursor(MySQLdb.cursors.DictCursor)
# # # cursor = mysql.connect.cursor(MySQLdb.cursors.DictCursor)
# # cursor.execute("SELECT profession_name, skills FROM prof")
# # results = cursor.fetchall()
# # conn.commit()
# # cursor.close()
# # # return render_template("success.html")
# # # message="success"
# # else:
# # return "connection not extablished"
# # except Exception as e:
# # return f"An error occurred: {str(e)}"
# # matches = []
# # for profession, skills in results:
# # profession_skills_set = set(skills.lower().split(","))
# # match_score = len(input_skills_set.intersection(profession_skills_set))
# # if match_score > 0:
# # matches.append((profession, match_score))
# # matches.sort(key=lambda x: x[1], reverse=True) # Sort by match score
# # return matches[:5] # Return top 5
# input_skills = request.form['skills']
# top_matches = get_matching_professions(input_skills)
# return render_template('after1.html', s=top_matches)
# def match_skills():
# def get_matching_professions(input_skills):
# input_skills_set = set(input_skills.lower().split(","))
# try:
# conn = mysql.connect # Assuming you have mysql configured
# if conn:
# cursor = conn.cursor(MySQLdb.cursors.DictCursor)
# cursor.execute("SELECT professor, skills FROM prof")
# results = cursor.fetchall()
# cursor.close()
# conn.commit()
# else:
# return "Connection not established"
# except Exception as e:
# return f"An error occurred: {str(e)}"
# matches = []
# for row in results:
# profession = row['professor']
# skills = row['skills']
# profession_skills_set = set(skills.lower().split(","))
# matched_skills = input_skills_set.intersection(profession_skills_set)
# match_score = len(matched_skills)
# if match_score > 0:
# matches.append((profession, match_score, matched_skills))
# # Sort matches by score in descending order
# matches.sort(key=lambda x: x[1], reverse=True)
# # Format the top 5 matches
# top_matches = " ".join([f"{profession}({score}) - Matched skills: {', '.join(matched_skills)}"
# for profession, score, matched_skills in matches[:5]])
# return top_matches
# input_skills = request.form['skills']
# top_matches = get_matching_professions(input_skills)
# return render_template('after1.html', s=top_matches)
def match_skills():
def get_matching_professions(input_skills):
input_skills_set = set(input_skills.lower().split(","))
try:
conn = mysql.connect # Assuming you have mysql configured
if conn:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT professor, skills FROM prof")
results = cursor.fetchall()
cursor.close()
conn.commit()
else:
return "Connection not established"
except Exception as e:
return f"An error occurred: {str(e)}"
matches = []
for row in results:
profession = row['professor']
skills = row['skills']
profession_skills_set = set(skills.lower().split(","))
matched_skills = input_skills_set.intersection(profession_skills_set)
match_score = len(matched_skills)
if match_score > 0:
matches.append((profession, match_score, matched_skills))
# Sort matches by score in descending order
matches.sort(key=lambda x: x[1], reverse=True)
# Format the top 5 matches
top_matches = "<br>".join([f"{profession} - Score: {score} - Matched skills: {', '.join(matched_skills)}"
for profession, score, matched_skills in matches[:5]])
return top_matches
input_skills = request.form['skills']
top_matches = get_matching_professions(input_skills)
return render_template('after1.html', s=top_matches)
if __name__ == "__main__":
app.run('0.0.0.0',port=8080,debug=True)