38894-vm/workers.py
Pragati Verma 9d2adf338d
Version 1.0 ready (#1)
* FEAT: Basic app completed & file uploading added

* Added pdf/txt support and quiz template

* FEAT: PDF/TEXT uploading and getting file contents done

* Updated .gitignore

* Modified quiz template

* Fix UI - Added CSS to upload page

* Feat Quiz UI - Added CSS to Quiz Page

* Feat: Question Extraction - Extract questions from raw text

* Feat: added incorrect answer generation

* Feat: Tied together question extraction and incorrect anwer generation

* Fix: handle word not in vocabulary error

* Fix: added document as argument to get_questions_dict

* Feat: Integrated uploading file and question generation; needs fixes

* Feat Quiz UI - added correct answer UI

* Feat: Clean text for better processing

* Fix UI - added gradient and spacing

* Feat UI - Added uploading animation

* Fixed options issue on quiz screen

* Fix UI - added fonts, favicon etc

* Fix - Formatted code

* Feat UI - added fork me on github ribbon

* Feat: Added quiz result functionality

* Fix UI - added text to landing page

* Fix UI - added CSS to submit button

* Feat UI - added CSS to results page

Co-authored-by: user86 <kshitij.kotasthane@gmail.com>
Co-authored-by: telescopic <vigneshsuresh1775@gmail.com>
2020-10-11 23:35:13 +05:30

39 lines
1.1 KiB
Python

from PyPDF2 import PdfFileReader
from question_generation_main import QuestionGeneration
def pdf2text(file_path: str, file_exten: str) -> str:
""" Converts a given file to text content """
_content = ''
# Identify file type and get its contents
if file_exten == 'pdf':
with open(file_path, 'rb') as pdf_file:
_pdf_reader = PdfFileReader(pdf_file)
for p in range(_pdf_reader.numPages):
_content += _pdf_reader.getPage(p).extractText()
# _content = _pdf_reader.getPage(0).extractText()
print('PDF operation done!')
elif file_exten == 'txt':
with open(file_path, 'r') as txt_file:
_content = txt_file.read()
print('TXT operation done!')
return _content
def txt2questions(doc: str, n=5, o=4) -> dict:
""" Get all questions and options """
qGen = QuestionGeneration(n, o)
q = qGen.generate_questions_dict(doc)
for i in range(len(q)):
temp = []
for j in range(len(q[i + 1]['options'])):
temp.append(q[i + 1]['options'][j + 1])
# print(temp)
q[i + 1]['options'] = temp
return q