| name | sentence-diversity |
| description | Measure diversity of verb-containing sentences extracted from clinical notes (Mapping_info_1 contains 'note') in a QUIQ-format table. Uses NLTK POS tagging to identify sentences with verbs, then computes unique/total ratio and coverage scores. No API key required. Use for LYDUS data quality assessment of note text diversity. |
| tier | community |
| category | lydus |
| parameters | {"quiq_path":{"description":"Path to QUIQ-format CSV file. Must contain rows where Mapping_info_1 contains 'note'.","type":"string"},"save_path":{"description":"Directory path to save output files.","type":"string"},"top_n":{"description":"Number of top sentences to show in histogram (default 10).","type":"integer"}} |
Sentence Diversity
Measures the diversity of verb-containing sentences in clinical notes extracted from a QUIQ-format table. High diversity means notes use varied phrasing; low diversity means many sentences are copy-pasted or templated.
When to Use This Skill
- After QUIQ conversion, to assess how varied the language is in clinical notes
- To detect template-heavy documentation (low sentence diversity)
- To identify the most frequently repeated sentences (coverage score analysis)
- As part of LYDUS quality management assessment
SQL Support
Not applicable. Requires NLTK sentence tokenization and POS tagging.
Filtering Logic
| Condition | Value |
|---|
Mapping_info_1 | contains note (case-insensitive) โ covers note_clinical, note_rad, etc. |
Value | used as the note text |
Algorithm
- Sentence segmentation (
_custom_sent_tokenize): split on 2+ consecutive newlines or non-word characters
- Verb filter (
_verb_sentences): keep only sentences that contain at least one word with POS tag starting with VB (verb)
- Diversity metrics:
| Metric | Formula | Meaning |
|---|
Sentence Diversity (%) | unique_sentences / total_sentences ร 100 | ๋์์๋ก ๋ค์ํ ํํ |
Coverage Score (Top X%) | Top X% of unique sentence types โ % of total | ์์ X% ํํ์ด ์ ์ฒด์ ๋ช % ์ฐจ์ง |
Coverage score example: Top 5% = the most-common 5% of unique sentence types cover Y% of all sentence occurrences. Low Y% โ diverse; high Y% โ a few sentences dominate.
NLTK Dependencies
The script auto-downloads required NLTK data on first run:
punkt / punkt_tab โ sentence/word tokenizer
averaged_perceptron_tagger / averaged_perceptron_tagger_eng โ POS tagger
Output
| File | Description |
|---|
sentence_diversity_summary.txt | Sentence Diversity (%), Coverage Scores for top 5/10/20% |
sentence_diversity_frequency.csv | Per-sentence: Count, Percentage (sorted by frequency) |
sentence_diversity_plot.png | Bar chart of top-N most frequent sentences |
How to Run
import pandas as pd
from scripts.sentence_diversity import get_sentence_diversity
quiq = pd.read_csv("/path/to/quiq.csv")
sen_diversity, item_vs_percentage, coverage_scores, freq_df = get_sentence_diversity(
quiq=quiq,
top_n=10
)
print(f"Sentence Diversity (%) = {sen_diversity}")
for k, v in coverage_scores.items():
print(f" Top {k}%: {v}")
As a script with config
quiq_path: /path/to/quiq.csv
save_path: /path/to/output
top_n: 10
python scripts/sentence_diversity.py --config config.yaml
Critical Notes
-
note ํํฐ๋ง ์ ์ฉ โ Mapping_info_1์ note๊ฐ ํฌํจ๋ ๋ชจ๋ ํ์ด ๋์ (note_clinical, note_rad ๋ฑ).
-
Verb ๋ฌธ์ฅ๋ง ์ง๊ณ โ ๋์ฌ๊ฐ ์๋ ๋จํธ์ ํํ(ํ ํญ๋ชฉ, ์์น ๋์ด ๋ฑ)์ ์ ์ธ. ์ฃผ๋ก ์์ ํ ๋ฌธ์ฅ๋ง ๋ถ์.
-
NLTK ์๋ ๋ค์ด๋ก๋ โ ์ฒซ ์คํ ์ punkt, averaged_perceptron_tagger๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ๋ค์ด๋ก๋๋จ.
-
top_n vs. item_vs_percentage โ item_vs_percentage๋ ๋ชจ๋ unique ๋ฌธ์ฅ์ ๋น๋ ์์ผ๋ก ํฌํจ (histogram ์ฉ). top_n์ ์๊ฐํ์์ ๋ช ๊ฐ๋ง ํ์ํ ์ง ๊ฒฐ์ .
-
์๋ณธ ์ฝ๋ ๊ฐ์ ์ฌํญ:
df_note['TEXT_Verb'] = ... SettingWithCopyWarning โ .copy() ํ ํ ๋น
_verb_sentence โ _verb_sentences (๋ณต์ํ, ๋ฐํ๊ฐ ๋ช
ํ)
- NLTK ๋ฐ์ดํฐ ์๋ ๋ค์ด๋ก๋ ๋ก์ง ์ถ๊ฐ
os.path.join ์ฌ์ฉ (๋ฌธ์์ด ์ฐ๊ฒฐ ๋์ )
--config argparse์ required=True ์ถ๊ฐ
-
Dependencies โ nltk, pandas, matplotlib
References
- LYDUS ํ์ง๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ํ์ฉ ๊ฐ์ด๋๋ผ์ธ (๋น๊ณต๊ฐ ๋ด๋ถ ๋ฌธ์)
- Original Python implementation: LYDUS_Sentence_Diversity.py (์ด์ฑ๋ฏผ ์์ฑ)
- Bird, S., Klein, E., & Loper, E. (2009). Natural Language Processing with Python. O'Reilly.