File size: 4,975 Bytes
9b9ea2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from textblob import TextBlob
import spacy
from spacy import displacy
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from pathlib import Path
import pytextrank

# Load the pre-trained NLP model
nlp = spacy.load("en_core_web_sm")
nlp.add_pipe('textrank')

# Sample text to analyze
text = """
Database that collects, administrative, clinical and demographic information on hospital discharges (including deaths, sign-outs and transfers). Some provinces and territories also use the DAD to capture day surgery.
         The discharge abstract database is a database for information on all AHS separations for acute care institutions, including discharges, deaths, sign-outs and transfers.
        Data on discharges, transfers and deaths of in-patients and day surgery patients from acute care hospitals in BC. All Canadian hospitals (except those in Quebec) submit their separations records directly to the Canadian Institute of Health information (CIHI) for inclusion in the Discharge Abstract Database (DAD). The database contains demographic, administrative and clinical data for hospital discharges (inpatient acute, chronic, rehabilitation) and day surgeries. A provincial data set, including various CIHI value-added elements (such as case mix groups, and resource intensity weights) is released on a monthly basis to the respective Ministries of Health. The DAD data files which Population Data BC receives include the CIHI variables. Population Data BC receives these data once per year.
        Health data maintained by Manitoba Health consisting of hospital forms/computerized records containing summaries of demographic and clinical information (e.g., gender, postal code, diagnoses and procedure codes) completed at the point of discharge from the hospital. Several hundred thousand abstracts per year are submitted for all separations from acute and chronic care facilities in Manitoba and for all Manitobans admitted to out-of-province facilities. The Hospital Abstracts Data includes records of both Manitoba residents and non-Manitoba residents hospitalized in Manitoba facilities and information about inpatient and day surgery services.
Patient discharge information from New Brunswick hospitals. Captures administrative, clinical and demographic information including discharges, deaths, sign-outs, and transfers.
The Provincial Discharge Abstract Database (PDAD) is the NLCHI dataset that contains demographic, clinical and administrative data collected at hospitals when patients are discharged from inpatient and surgical day care services and submitted to the CIHI Discharge Abstract Database. The PDAD captures information regarding hospitalizations of both residents of NL and non-residents receiving care in NL.
Contains information on each hospital admission recorded in a Nova Scotia hospital
The Discharge Abstract Database is a database for information on all separation from acute care institutions within a fiscal year (April 1st to March 31st). Data is received directly from acute care facilities or from their respective health/regional authority or ministry/department of health.
Captures administrative, clinical and demographic information on discharges for acute care facilities (including deaths, sign-outs and transfers).

"""

def get_top_key_phrases(text, top_n, save_output):
    # Process the text
    doc = nlp(text)
    # show the score of key phrases #
    phrases_ranking = {phrase.text: phrase.rank for phrase in doc._.phrases}
    phrases = list(zip(*phrases_ranking.items()))[0]
    scores = list(zip(*phrases_ranking.items()))[1]
    keyword_df = pd.DataFrame({'words': phrases[:top_n], 'scores': scores[:top_n]})
    plt.figure(figsize=(8, 24))
    sns.catplot(data=keyword_df, x='words', y='scores', kind='bar', palette='blend:#7AB,#EDA', aspect=1.5)
    plt.xticks(rotation=-10, fontsize=6)
    plt.savefig(save_output, dpi=300)

def display_key_phrases(text, save_output):
    doc = nlp(text)
    key_phrases = [{'start': chunk.start_char, 'end': chunk.end_char, 'label': str(round(phrase.rank, 2))} for phrase in doc._.phrases for chunk in phrase.chunks]

    # generate displacy html #
    max_rank = float(key_phrases[0]['label'])
    min_rank = float(key_phrases[-1]['label'])
    step = 50/max_rank
    colors = {str(phrase['label']): f'hsl(121, 100%, {100-float(phrase["label"])*step}%)' for phrase in key_phrases}
    options = {'ents': [color for color in colors.keys()], 'colors': colors}
    # Create a list of spans to highlight
    sentence = [
        {'text': text,
         'ents': key_phrases,
        'title': None}
    ]

    # Create a visualization of the text with highlighted key phrases
    svg = displacy.render(sentence, style="ent", options=options, manual=True, page=True)
    filename = Path(save_output)
    filename.open('w', encoding='utf-8').write(svg)
    return svg


if __name__ == '__main__':
    get_top_key_phrases(text, 10)
    display_key_phrases(text)