<jupyter_start><jupyter_text>https://kormilitzin.medium.com/med7-clinical-information-extraction-system-in-python-and-spacy-5e6f68ab1c68  **INSTALLING LLIBRARIES AND DEPENDENCIES**<jupyter_code>!sudo apt install tesseract-ocr
!pip install Image
!pip install camelot-py
!pip install camelot-py[cv]
!pip install camelot-py[all]
!pip install pytesseract
!pip install nltk
!pip install stopWords
!pip install pandas
!pip install spacy==2.3.5
!pip install https://huggingface.co/kormilitzin/en_core_med7_lg/resolve/main/en_core_med7_lg-any-py3-none-any.whl
!pip install tabula-py<jupyter_output>Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  libnvidia-common-470
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  tesseract-ocr-eng tesseract-ocr-osd
The following NEW packages will be installed:
  tesseract-ocr tesseract-ocr-eng tesseract-ocr-osd
0 upgraded, 3 newly installed, 0 to remove and 39 not upgraded.
Need to get 4,795 kB of archives.
After this operation, 15.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 tesseract-ocr-eng all 4.00~git24-0e00fe6-1.2 [1,588 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 tesseract-ocr-osd all 4.00~git24-0e00fe6-1.2 [2,989 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/universe amd64 tesseract-ocr amd64 4.00~git2288-10f4998a-2 [218 kB]
Fetched 4,795 kB in 1s (3,777 kB/s)
debconf: unable to initi[...]<jupyter_text>**LIBRARY FOR SPEECH TO TEXT CONVERSION**<jupyter_code>!pip install SpeechRecognition
!pip install pydub
!pip install nameparser
!pip install pysummarization<jupyter_output>Requirement already satisfied: pydub in /usr/local/lib/python3.7/dist-packages (0.25.1)
Requirement already satisfied: nameparser in /usr/local/lib/python3.7/dist-packages (1.1.1)
Collecting pysummarization
  Downloading pysummarization-1.1.8.tar.gz (51 kB)
[K     |████████████████████████████████| 51 kB 230 kB/s 
[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from pysummarization) (1.21.5)
Requirement already satisfied: nltk in /usr/local/lib/python3.7/dist-packages (from pysummarization) (3.2.5)
Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from nltk->pysummarization) (1.15.0)
Building wheels for collected packages: pysummarization
  Building wheel for pysummarization (setup.py) ... [?25l[?25hdone
  Created wheel for pysummarization: filename=pysummarization-1.1.8-py3-none-any.whl size=59453 sha256=b04be653bf0f2b2026f994e659f51e2817bd4897cd6e91a063a716706cab0603
  Stored in directory: /root/.cache/pip/wheels/da[...]<jupyter_text>**LIBRARIES FOR IMAGE TO TEXT CONVERSION**<jupyter_code>!apt install ghostscript python3-tk
!pip install pdf2image
!apt-get install poppler-utils<jupyter_output>Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-tk is already the newest version (3.6.9-1~18.04).
The following package was automatically installed and is no longer required:
  libnvidia-common-470
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
  fonts-droid-fallback fonts-noto-mono gsfonts libcupsfilters1 libcupsimage2
  libgs9 libgs9-common libijs-0.35 libjbig2dec0 poppler-data
Suggested packages:
  fonts-noto ghostscript-x poppler-utils fonts-japanese-mincho
  | fonts-ipafont-mincho fonts-japanese-gothic | fonts-ipafont-gothic
  fonts-arphic-ukai fonts-arphic-uming fonts-nanum
The following NEW packages will be installed:
  fonts-droid-fallback fonts-noto-mono ghostscript gsfonts libcupsfilters1
  libcupsimage2 libgs9 libgs9-common libijs-0.35 libjbig2dec0 poppler-data
0 upgraded, 11 newly installed, 0 to remove and 39 not upgraded.
Need to get 14.1 MB of archives.
After this operation, [...]<jupyter_text>*Camelot is a Python library that can help you extract tables from PDFs!*<jupyter_code>import camelot.io as camelot
import re
import pytesseract
import shutil
import os
import random
try:
    from PIL import Image
except ImportError:
    import Image
# from google.colab import files
# uploaded = files.upload()<jupyter_output><empty_output><jupyter_text>*Extracting the text from image 'image1.jpg' using pytesseract library and then printing it*<jupyter_code>extractedInformation = pytesseract.image_to_string(Image.open('image1.jpg'))
print(extractedInformation)<jupyter_output><empty_output><jupyter_text>FORMATTING THE TEXT FROM IMAGE *NLTK (Natural Language Toolkit) Library is a suite that contains libraries and programs for statistical language processing. It is one of the most powerful NLP libraries, which contains packages to make machines understand human language and reply to it with an appropriate response.*<jupyter_code>import nltk
nltk.download('stopwords')
nltk.download('punkt')
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
import re
 
stopWords = set(stopwords.words('english'))
sent = sent_tokenize(extractedInformation)
wordsFiltered=[]
for w in sent:
  wordsFiltered.append(w)
word=[]
patterns = "Drug"
for w in wordsFiltered:
  x= word_tokenize(w)
  for i in x:
    if re.match(patterns,i):
      print(x)<jupyter_output><empty_output><jupyter_text>CONVERTING TO DATAFRAME<jupyter_code>import camelot 
import pandas as pd
tables = camelot.read_pdf('/content/Coronary Angioplasty.pdf')

#tables[0].to_csv('camelot.csv')
#print(len(tables))
tables[0].df<jupyter_output><empty_output><jupyter_text>*spaCy  is a free, open-source library for advanced Natural Language Processing (NLP) in Python.**spaCy** is designed specifically for production use and helps you build applications that process and “understand” large volumes of text. It can be used to build information extraction or natural language understanding systems, or to pre-process text for deep learning.*<jupyter_code>import spacy

med7 = spacy.load("en_core_med7_lg")

# create distinct colours for labels
col_dict = {}
seven_colours = ['#e6194B', '#3cb44b', '#ffe119', '#ffd8b1', '#f58231', '#f032e6', '#42d4f4']
for label, colour in zip(med7.pipe_labels['ner'], seven_colours):
    col_dict[label] = colour

options = {'ents': med7.pipe_labels['ner'], 'colors':col_dict}

text = 'A patient was prescribed Magnesium hydroxide 400mg/5ml suspension PO of total 30ml bid for the next 5 days.'
doc = med7(text)

spacy.displacy.render(doc, style='ent', jupyter=True, options=options)

[(ent.text, ent.label_) for ent in doc.ents]
import tabula
dataframe = tabula.read_pdf('Coronary Angioplasty.pdf',pages="all")
dataframe<jupyter_output><empty_output><jupyter_text>EXTRACTING INFORMATION FROM TEXT FILE **'find_next' function uses regex expressions to search and extract different generalized information from the extracted text file**<jupyter_code>def find_next(words,data):
  text=""
  for word in words:
    if(words[0].lower()=='age'):
     text=re.search('\d+(?=[\s]?y | year)',data.lower())
    if(text):
      break
    text=re.search('(?<='+word.lower()+' : )(\S*)',data.lower())
    if(text):
      break
  return text
from pdf2image import convert_from_path
def extract_pdf2img(path):
  images = convert_from_path(path)
  for i in range(len(images)):
    images[i].save('page'+ str(i) +'.jpg', 'JPEG')
  return images

images=extract_pdf2img('/content/Coronary Angioplasty.pdf')
all_text_data=[]
file = open('report.txt','w')
for i in range(len(images)):
  extractedInformation = pytesseract.image_to_string(Image.open('/content/page'+str(i)+'.jpg'))
  sent = sent_tokenize(extractedInformation)
  wordsFiltered=[]
  for w in sent:
    file.write(w)
# all_text_data\
file.close()

# def find_next(words,data):
  # for word in words:
data='''
Coronary Angiography

Patient :<NAME> (68y 6m/ Male) _ Patient ID : UNM-2015-08-083152
IPD No. : IPD/2022/02/0005003 Admission Date : 20/02/2022

Date of Procedure : 23/02/2022 Angio No. : D-42737

Time of Procedure : 08:23

Primary Operator : Dr. <NAME> (G-19508) Professor in Cardiology
Secondary Operator : Dr. <NAME> (G-29637) Assistant Professor in Cardiology

 

Indication

- Ischemic Heart Disease Type : CSA

- PAMI: No

- ACS: No

Procedure & Description

- Procedure Type : Coronary Angiography

- Status of Procedure : Elective , Procedure Approach : Radial - Left

- Diagnostic Catheter : Left Coronary TIGER 5F ,Right Coronary TIGER 5F ,Guide wire Non-hydrophilic 03 5,Contrast
Agent Nonionic Low-osmolar,Contrast Volume 100 ml

- Anesthesia used : LOCAL
- Total Radiation Dose at Source : 219.0 mGy

Peripheral Stenosis

Coronary Anatomy

- Extent of Disease : Triple Vessel, Dominance : Right
LMCA | LMCA,Detail : Normal

Mid LAD, Stenosis Severity : 90 %,

Proximal LAD,Detail : Plaque

Distal Circumflex,Stenosis Severity : 80 %,
RCA | Distal RCA,Lesion's Remarks: IN STENT- 70%

 

LAD

 

 

 

Final Diagnosis
- OLD ACS, HTN, CAD-TVD, WITH RIGHT RENAL ARTERY, S/P PTCA TO RIGHT PTRA (27/11/2011), S/P
PCI TO RCA (INTEGRITY 27/11/2015)(2.75x18), P/W ACS NSTEMI,
- LV Function : Severe LVD
- TVD : Yes

- Remarks : RIGHT RENAL - 50% ISR IN STENT, LEFT RENAL- 60% STENOSIS

Advice :
PTCA TO LAD & RCA

Dr. <NAME> (G-19508) Professor in Cardiology :

 

Dr. <NAME> (G-29637) Assistant Professor in Cardiology :

 

Generated By : <NAME>

unmicrc/MRD/Rep-A/02/rev-02 1-Aug-17

 

Patient :<NAME> (68y 6m / Male) Patient ID : UNM-2015-08-083 152
IPD No: IPD/2022/02/0005003 Admission Date : 20/02/2022

Date of Procedure : 23/02/2022 Angio No. : D-42737

Time of Procedure : 08:23

 

unmicrc/MRD/Rep-A/02/rev-02 1-Aug-17
'''
# 

file = open('report.txt', 'r')
# x=""
# for each in file:
#     print(each)
#     words=['Procedure','Time of Procedure']
#     x=find_next(words,each)
# print(x)<jupyter_output><empty_output><jupyter_text>**final info is a dictionary in which every information is stored with its corresponding key**<jupyter_code>final_info={}<jupyter_output><empty_output><jupyter_text>**add_result function has three different arguments***   words- list of keywords to find.*   key- the name of key by which the information is stored.*   data- Whole text file in which keywords are searched and extracted<jupyter_code>def add_result(words,key,data):
  text=find_next(words,data)
  if(text):
    final_info[key]=text.group(0)
    return<jupyter_output><empty_output><jupyter_text>Adding every general paramater<jupyter_code>#Age
add_result(['Age'],"Age",data)
#Duration of Procedure
add_result(['Time of Procedure','Duration of Procedure'],'Duration of Procedure',data)
final_info
#Procedure
add_result(['Procedure Type'],'Procedure Type',data)
final_info
#Status Of Procedure
add_result(['Status Of Procedure'],'Status Of Procedure',data)
final_info
#Procedure Approach
add_result(['Procedure Approach'],'Procedure Approach',data)
final_info
#Ischemic Heart Disease Type
add_result(['Ischemic Heart Disease Type'],'Ischemic Heart Disease Type',data)
final_info
#advise
word='advise'
x=re.search('(?<='+word.lower()+')'+'[\r\n]+([^\r\n]+)',data.lower())
x
#comments
word='Comments'
x=re.search('(?<='+word.lower()+')'+'[\r\n]+([^\r\n]+)',data.lower())
x
def find_next2(words,data,key):
    for word in words:
        text=re.search('(?<='+word.lower()+': )(\S*)',data.lower())
        if(text):
            final_info[key]=text.group(0)
find_next2("ACS",data,"ACS")
find_next2("PAMI",data,"PAMI")
final_info
def find_remarks(words,key,data):
    for word in words:
        text=re.search('(?<='+word.lower()+' :'+')'+'([^\r\n]+)',data.lower())
        print(text)
        if(text):
            final_info[key]=text.group(0)

find_remarks(['remarks','comments'],'Remarks',data)
final_info
def find_advice(words,key,data):
    for word in words:
        text=re.search('(?<=Advice :)([\r\n])([^\r\n]+)',data)
        text1=re.search('(?<=Advice)([\r\n])([^\r\n]+)',data)
        #print(text)
        if(text):
            final_info[key]=text.group(0)
        if(text1):
            final_info[key]=text1.group(0)
find_advice(['advice','advise'],'Advice',data)
final_info
def name(word,key):
  file=open('/content/report.txt','r')
  Lines = file.readlines()
  
  count = 0
  # Strips the newline character
  for line in Lines:
      # line=line.lower()
      if word in line:
          # word=word.lower()
          x=re.findall(r"[A-Z][a-z]+,?\s+(?:[A-Z][a-z]*\.?\s*)?[A-Z][a-z]+", line)
          for j in x:
            final_info[key]=j
name("Patient","Patient")
name("Primary Operator","Doctor")
final_info
import spacy

med7 = spacy.load("en_core_med7_lg")

# create distinct colours for labels
col_dict = {}
seven_colours = ['#e6194B', '#3cb44b', '#ffe119', '#ffd8b1', '#f58231', '#f032e6', '#42d4f4']
for label, colour in zip(med7.pipe_labels['ner'], seven_colours):
    col_dict[label] = colour

options = {'ents': med7.pipe_labels['ner'], 'colors':col_dict}

file=open('/content/report.txt','r')
Lines = file.readlines()

for line in Lines:
  doc = med7(line)
  for ent in doc.ents:
    if(ent.label_.lower()=="drug"):
      final_info['Drug']=ent.text
  # print([(ent.text, ent.label_) for ent in doc.ents])
  # spacy.displacy.render(doc, style='ent', jupyter=True, options=options)
  # [(ent.text, ent.label_) for ent in doc.ents]
print(final_info)
import pandas as pd
df=pd.DataFrame(data=final_info,index=[0])
print (df)
df.to_excel('dict1.xlsx')
# Comments
# - Type Of Procedure : Multi Vessel Angioplasty
# - Lesion Summary : Lesions Attempted-1 , Lesions Stented-1 , Stent Used-1 , Lesions Successfully Dilated-1
# Final Diagnosis
# - Procedure Result : Successful PTCA to LAD done
# Advise
# -Regular Follow-up & Medicine : Staged PTCA to RCA<jupyter_output><empty_output><jupyter_text>EXTRACTING FROM SPEECH/AUDIO FILES<jupyter_code>#Speech to text 
#import libraries

import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile('/content/VOXTAB_Medical_audio.wav') as source:
    audio_text = r.listen(source)
    try:
        text = r.recognize_google(audio_text)
        print('Converting Audio to TEXT....')
        print(text)
    except:
         print('Run again, API Error !')
import speech_recognition as sr 
import os 
from pydub import AudioSegment
from pydub.silence import split_on_silence


r = sr.Recognizer()

def get_large_audio_transcription(path):
    """
    Splitting the large audio file into chunks
    and apply speech recognition on each of these chunks
    """
    sound = AudioSegment.from_wav(path)  
    chunks = split_on_silence(sound,
        min_silence_len = 500,
        silence_thresh = sound.dBFS-14,

        keep_silence=500,
    )
    folder_name = "audio-chunks"
    if not os.path.isdir(folder_name):
        os.mkdir(folder_name)
    whole_text = ""
    for i, audio_chunk in enumerate(chunks, start=1):
        chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
        audio_chunk.export(chunk_filename, format="wav")
        with sr.AudioFile(chunk_filename) as source:
            audio_listened = r.record(source)
            try:
                text = r.recognize_google(audio_listened)
            except sr.UnknownValueError as e:
                print("Error:", str(e))
            else:
                text = f"{text.capitalize()}. "
                print(chunk_filename, ":", text)
                whole_text += text
    return whole_text
text=get_large_audio_transcription('/content/JonesMedical.wav')
text
# inp = sr.AudioFile('/content/JonesMedical.wav')
# with inp as file:
#   audio = r.record(file)
#   print(r.recognize_google(audio))
#   # print(audio)
from nameparser import HumanName
# for line in text:
#   print(line)
text_sent=sent_tokenize(text)
# name=""
for sent in text_sent:
  name=HumanName(sent)
  print(name)
from pysummarization.nlpbase.auto_abstractor import AutoAbstractor
from pysummarization.tokenizabledoc.simple_tokenizer import SimpleTokenizer
from pysummarization.abstractabledoc.top_n_rank_abstractor import TopNRankAbstractor
auto_abstractor = AutoAbstractor()
auto_abstractor.tokenizable_doc = SimpleTokenizer()
auto_abstractor.delimiter_list = [".", "\n"]
abstractable_doc = TopNRankAbstractor()
result_dict = auto_abstractor.summarize(text, abstractable_doc)
for sentence in result_dict["summarize_result"]:
    print(sentence)<jupyter_output>Hi this is doctor <NAME>.

 Today's the 16th of june.

 I'm doing a referral after please.

 He could date today's date.

 Pocket private confidential full mister jason.

 <NAME> date of birth.

 22nd of june 1972 address one the house to the road.

 So you could see this 38 year old gentleman.

 jones is a nice young gentleman who tell it be since buying tight footwear has suffered from left medial foot arch pain.

 I need some food from tuesday or thursday.
{'fraction_non_alphanumeric': 0.12292694525352196, 'fraction_numerical': 0.03156393033347203, 'mean_word_length': 4.6173622704507515, 'pattern_counts': {'":': 1, '<': 65, '<?xml version=': 0, '>': 59, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '265808', 'license': 'MIT', 'n_tokens_mistral': 6318, 'n_tokens_neox': 5766, 'n_words': 1503, 'path': 'Healthcare.ipynb', 'repo_name': 'dhruvil-shah/Algorithmic-Toolbox-San-Diego', 'size': '16936'}