How to Build a Text Analysis W…

configparser is a built-in Python module used for handling configuration files, which are usually in INI file format.

1. Create INI File

Create a INI file in the root path of your project and set up the file name. For example, “WJ_Z_config.ini”.

2. Assign Values

[DEFAULT]
fortest = 0

[FILE_SIZE_LIMIT]
PDF_size_limit = 5 * 1024 * 1024
PDF_page_limit_jd = 5
PDF_page_limit_resume = 2
word_count_lower_limit_jd = 200
word_count_lower_limit_resume = 100
word_count_upper_limit_jd = 2000
word_count_upper_limit_resume = 1000

3. Using configparser:

  • You can read, write, and modify INI files using configparser.
  • The library allows for accessing configuration settings, casting them to Python’s native types, and even writing back any changes to the file.
import configparser

config = configparser.ConfigParser()
config.read('WJ_Z_config.ini')

CONFIG_PDF_size_limit = int(config.get('FILE_SIZE_LIMIT', 'PDF_size_limit'))
CONFIG_PDF_page_limit_jd = int(config.get('FILE_SIZE_LIMIT', 'PDF_page_limit_jd'))
CONFIG_PDF_page_limit_resume = int(config.get('FILE_SIZE_LIMIT', 'PDF_page_limit_resume'))
CONFIG_word_count_lower_limit_jd = int(config.get('FILE_SIZE_LIMIT', 'word_count_lower_limit_jd'))
CONFIG_word_count_upper_limit_jd = int(config.get('FILE_SIZE_LIMIT', 'word_count_upper_limit_jd'))
CONFIG_word_count_lower_limit_resume = int(config.get('FILE_SIZE_LIMIT', 'word_count_lower_limit_resume'))
CONFIG_word_count_upper_limit_resume = int(config.get('FILE_SIZE_LIMIT', 'word_count_upper_limit_resume'))

The current code:

def is_valid_file_size(file_content):
# Check if the file size is within the allowed limit
return len(file_content) <= 5 * 1024 * 1024

The updated one:

def is_valid_file_size(file_content):
# Check if the file size is within the allowed limit
return len(file_content) <= CONFIG_PDF_size_limit

Updating the whole function:

@app.route('/index', methods=['GET', 'POST'])
def index_post():
print("Request received with method:", request.method)
if request.method == 'POST':
# Handling JD text or file submission
jd_text = request.form.get('jd_text')
jd_file = request.files.get('jd_file')
if jd_text:
session['jd_text'] = jd_text
elif jd_file and allowed_file(jd_file.filename):
file_content = jd_file.read()

if not is_valid_file_size(file_content):
size_limit_mb = CONFIG_PDF_size_limit // (1024 * 1024)
return f"Uploaded file is too large (max {size_limit_mb} MB)", 400

if len(file_content) == 0:
return "Uploaded file is empty", 400

pdf = PyPDF2.PdfReader(io.BytesIO(file_content))
if len(pdf.pages) > CONFIG_PDF_page_limit_jd:
return f"JD PDF is too long to handle, upload a shorter one (max {CONFIG_PDF_page_limit_jd} pages)", 400

extracted_text = extract_text(file_content)
word_count = len(extracted_text.split())
if word_count < CONFIG_word_count_lower_limit_jd:
return f"Job Description is too short, please upload a longer one (min {CONFIG_word_count_lower_limit_jd} words)", 400
if word_count > CONFIG_word_count_upper_limit_jd:
return f"Job Description is too long, please upload a shorter one (max {CONFIG_word_count_upper_limit_jd} words)", 400
session['jd_text'] = extracted_text
return "JD Saved"

# Handling resume text or file submission
resume_text = request.form.get('resume_text')
resume_file = request.files.get('resume_file')
if resume_text:
session['resume_text'] = resume_text
elif resume_file and allowed_file(resume_file.filename):
file_content = resume_file.read() # Read the content of the uploaded file
if not is_valid_file_size(file_content):
size_limit_mb = CONFIG_PDF_size_limit // (1024 * 1024)
return f"Uploaded file is too large (max {size_limit_mb} MB)", 400

pdf = PyPDF2.PdfReader(io.BytesIO(file_content))
if len(pdf.pages) > CONFIG_PDF_page_limit_resume:
return f"Resume PDF is too long to handle, upload a shorter one (max {CONFIG_PDF_page_limit_resume} pages)", 400

extracted_text = extract_text(file_content) # Pass the file content to the function
word_count = len(extracted_text.split())
if word_count < CONFIG_word_count_lower_limit_resume:
return f"Resume is too short, please upload again (min {CONFIG_word_count_lower_limit_resume} words)", 400
if word_count > CONFIG_word_count_upper_limit_resume:
return f"Resume is too long, please upload again (max {CONFIG_word_count_upper_limit_resume} words)", 400

session['resume_text'] = extracted_text
return "Resume Saved"

# Redirect to the result page if both JD and resume are present
if 'jd_text' in session and 'resume_text' in session:
return redirect(url_for('result_resume_assessment'))

# Render the initial form
return render_template('index.html')

Source link

You May Also Like