CoolFace
Apppublic

CalebKoster/Translation_Note_Alignment

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
TranslationNoteFinder.cpython-39.pyc53 linesDownload Raw Back to __pycache__
1a

2yE�e6'�@s�ddlZddlZddlZddlmZddlmZddlZddlmZm	Z	m3Z4ddlmZddl
mZddlmZddlZddlmZGd	d5�d6�ZdS)�N)�detect)�TfidfVectorizer)�models�select�instruction)�OpenAI)�uroman)�ScriptureReference)�greek_to_hindic@s^eZdZejZdZdd�Zdd�Zdd�Z	dd	�Z7d8d�Zdd
�Zdd�Z
dd�Zdd�ZdS)�TranslationNoteFinderzbibles/grc-grctcgnt.txtcCs�||_tj|ddd�|_|j|_|jdt��7_tj|_|�|j	�|_9|�|�|_|j��d}t
d|���t|�|_t�|j�tj|jdd�|_|d	|_|�|�|_|�|�|_|��\|_|_t
d10|jdd�dd�f�����dS)Ni�)Zn_ctxZn_gpu_layerszBHere are some good examples of translating from Greek into Hindi: �ZzFirst line of NT: �tokenize)ZlangZ11processorsz12.tfidf.pklzportion of tfidf matrix �13)�translation_notes_pathrZLlamaCppZllm�lmr14r�verses�15load_bible�greek_bible_path�greek_bible_text�target_bible_text�16splitlines�printr�language�stanzaZdownloadZPipeline�nlpZtfidf_vectorizer_path�load_translation_notes�translation_notes�create_tfidf_vectorizer_matrix�tfidf_vectorizer�tfidf_matrix�todense)�selfrZbible_text_pathZ17model_pathZ
first_line_nt�r#�Jc:\Users\caleb\Bible Translation Project\guidance\TranslationNoteFinder.py�__init__s"1819zTranslationNoteFinder.__init__cCsNt|ddd��,}t�|�}td|d���Wd�n1s@0Y|S)N�r�utf-8��encodingzFirst translation note: r)�open�json�loadr)r"r�filerr#r#r$r1s200z,TranslationNoteFinder.load_translation_notescCs:t|ddd��}|��}Wd�n1s,0Y|S)Nr&r'r()r*�read)r"Z21bible_pathr-�22bible_textr#r#r$r:s&z TranslationNoteFinder.load_biblecCs�g}g}|��}t|dd�D]<\}}||jvrJ|rJd�|�}|�|�g}|�|���q|rrd�|�}|�|�td|d��dd����td|d��dd����|S)Nr)�start� z,First 10 words of first segmented document: rrz-First 10 words of second segmented document: )r�	enumerater�join�append�stripr�split)r"r/Z	documentsZcurrent_documentZverse_lines�i�lineZjoined_doc_stringr#r#r$�segment_corpusAs 2324252627z$TranslationNoteFinder.segment_corpuscCs|�|�}dd�|jD�}|S)NcSsg|]}|jD]28}|j�qqSr#)Zwords�text)�.0ZsentZwordr#r#r$�29<listcomp>g�z:TranslationNoteFinder.stanza_tokenizer.<locals>.<listcomp>)rZ	sentences)r"r:�docZtokensr#r#r$�stanza_tokenizercs30z&TranslationNoteFinder.stanza_tokenizercCs4td�t|jdd�}|�|j�}|�|�}||fS)Nz$Creating tfidf vectorizer and matrix)��)Z	tokenizerZngram_range)rrr?r9rZ
fit_transform)r"rZsegmented_corpusr r#r#r$rks3132z4TranslationNoteFinder.create_tfidf_vectorizer_matrixc	Csvttj����|�}|j��}|j|��}|�	�d}t33t||��}dd�|��D�}t34t
|��dd�dd��}|S)NrcSsi|]\}}|dkr||�qS)rr#�r;ZfeatureZscorer#r#r$�35<dictcomp>|r=zATranslationNoteFinder.get_tfidf_book_features.<locals>.<dictcomp>cSs|dS)Nrr#)�itemr#r#r$�<lambda>~r=z?TranslationNoteFinder.get_tfidf_book_features.<locals>.<lambda>T)�key�reverse)�list�SRZ36book_codes�keys�indexrZget_feature_names_outr r!�tolist�dict�zip�items�sorted)	r"Z	book_codeZ37book_indexZ
feature_namesZdenseZdocument_tfidf_scoresZfeature_scoresZfiltered_feature_scoresZsorted_feature_scoresr#r#r$�get_tfidf_book_featuresss38z-TranslationNoteFinder.get_tfidf_book_featurescCs�tddd�|��D����|d��}|jd|�d|�d�7_|jtdd�|��D�d	d39�7_td|jd	���|jd	��S)Nz0All ngrams in verse guidance is selecting from: cSsg|]}|�qSr#r#�r;rFr#r#r$r<�r=z=TranslationNoteFinder.best_ngram_for_note.<locals>.<listcomp>�40greek_termz(What is a good, succinct translation of z from Greek into �?cSsg|]}|�qSr#r#rRr#r#r$r<�r=�ngram)�namezBest ngram found for note: )rrJr5rr)r"�note�verse_ngramsrrSr#r#r$�best_ngram_for_note�s$z)TranslationNoteFinder.best_ngram_for_notec	st|�}|j��|jd}td|j��d���td|j��d���tdddd	��}t�|�}Wd�n1sz0Yg}td41|���|D]4}td|d���|d��|��vr�|�	|�q�td
dd�|D����|j42��|jd�td����d}td|���|jd}	|�|	�}43tdt
|44���dd�����fdd�|45��D�}tdt
|���dd����g}|D]X}|�|||�}
����|
���}|t|
�}|d}|d}|�	|
||||d���q�td|���|j|j|d�S)Nrz)Line 1 of Greek Bible (should be blank): rz0Line 23214 of Greek Bible (should be Matt 1:1): r
ztranslation_notes.jsonr&r'r(z@Let's see if there are any translation notes for this verse: 46	 zChecking for existence of: rSz0Greek terms for all translation notes in verse: cSsg|]}|d�qS)rSr#)r;rWr#r#r$r<�r=z5TranslationNoteFinder.verse_notes.<locals>.<listcomp>zTarget verse text: ZHindizLanguage of target verse text: �bookCodezFirst 30 n-grams of the book: �csi|]\}}|�vr||�qSr#r#rB�Ztarget_verse_textr#r$rC�r=z5TranslationNoteFinder.verse_notes.<locals>.<dictcomp>z9First five n-grams of the verse along with their scores: �Ztranslation_note)rU�	start_pos�end_posrS�47trans_notezVerse notes to be returned: )�	verse_ref�line_number�ngrams)rIrrrbrr*r+r,�lowerr4rZstructured_refrQrHrJrOrY�find�len)r"raZv_refZ
gk_verse_textr-rZtranslation_notes_in_verserWrrZZbook_ngramsrXrcrUr^r_rSr`r#r\r$�verse_notes�sT(4849��	�z!TranslationNoteFinder.verse_notesN)�__name__�50__module__�__qualname__rIZ51verse_onesrrr%rrr9r?rrQrYrgr#r#r#r$rs	"r)r+�pickle�reZ52langdetectrZsklearn.feature_extraction.textrZguidancerrrZopenairZromanizerr	rIrZTrainingDatar53rr#r#r#r$�<module>s