o
    4f                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ ed d dl	m
Z
 G dd dZedkrgee jdkrDed	 e d
 e jd
 Zee jd Ze ZeeZeeeZeee dS dS )    N)	stopwordsr   )mainc                   @   sH   e Zd Zdd Zdd Zedd Zedd Zed	d
 Zdd Z	dS )QuestionGeneratorc                 C   s   t  | _d S )N)r   QGenqg)self r   L/var/www/chatrigo.edurigo1.com/get_recommendations/testing/main_version_3.py__init__   s   zQuestionGenerator.__init__c                 C   s   ||d}| j |}|d d | }g }t|D ]$\}}|d }	|	|d  |d |d |d |	|d d}
||
 qd|iS )	N)
input_textmax_questions	questionsoptionsanswer   question_statementquestion_type)idr   r   r   r   )r   predict_mcq	enumerateappend)r   r   num_questionspayloadoutputr   modified_questionsiquestion_datar   questionr   r   r	   generate_questions   s   
z$QuestionGenerator.generate_questionsc                 C   s6   t | d}| W  d    S 1 sw   Y  d S )Nr)openread)	file_pathfr   r   r	   extract_text_from_txt&   s   $z'QuestionGenerator.extract_text_from_txtc                 C   s:   t | }d}|jD ]}|j r||j d 7 }q
|S )N  )docxDocument
paragraphstextstrip)r"   docr*   	paragraphr   r   r	   extract_text_from_docx+   s   


z(QuestionGenerator.extract_text_from_docxc                 C   sl   d}t | d%}t|}t|jD ]}||}||  d 7 }qW d    |S 1 s/w   Y  |S )Nr%   rbr&   )r    PyPDF2PdfFileReaderrangenumPagesgetPageextractTextr+   )r"   r*   pdf_file
pdf_readerpage_numpager   r   r	   extract_text_from_pdf4   s   


z'QuestionGenerator.extract_text_from_pdfc                 C   sR   t j|d  }|dkr| |S |dkr| |S |dkr%| |S td)Nr   z.txtz.docxz.pdfzUnsupported file format)ospathsplitextlowerr$   r.   r:   
ValueError)r   r"   file_extensionr   r   r	   extract_text_from_file>   s   


z(QuestionGenerator.extract_text_from_fileN)
__name__
__module____qualname__r
   r   staticmethodr$   r.   r:   rA   r   r   r   r	   r      s    


	r   __main__   z<Usage: python main_version_3.py <input_file> <num_questions>r      )sysjsonr;   r'   r0   nltknltk.corpusr   downloadQuestgenr   r   rB   lenargvprintexit
input_fileintr   	generatorrA   r   r   r   dumpsr   r   r   r	   <module>   s*    
=


