| name | vocabulary-diversity |
| description | Measure diversity of noun words in clinical notes using NLTK POS tagging; computes unique/total noun ratio and top-5/10/20% coverage scores. No API key required. Use for LYDUS data quality assessment of vocabulary richness in unstructured text. |
| tier | community |
| category | lydus |
| parameters | {"quiq_path":{"description":"Path to QUIQ-format CSV file. Must contain note rows (Mapping_info_1 contains 'note').","type":"string"},"save_path":{"description":"Directory path to save output files and plots.","type":"string"},"top_n":{"description":"Number of top words to show in the histogram plot. Default 10.","type":"integer"}} |
Vocabulary Diversity
Measures the diversity of noun words in clinical notes extracted from a QUIQ-format table. High diversity means notes use varied vocabulary; low diversity means a small set of nouns dominates all documentation.
When to Use This Skill
- After QUIQ conversion, to assess how varied the noun vocabulary is in clinical notes
- To detect template-heavy or formulaic documentation (low vocabulary diversity)
- To identify the most frequently used clinical terms (coverage score analysis)
- As part of LYDUS quality management assessment
SQL Support
Not applicable. Requires NLTK word 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
- Word tokenization (
_noun_words): tokenize each note with NLTK word_tokenize
- Noun filter: keep only tokens with POS tag starting with
NN (noun, proper noun, etc.)
- Diversity metrics:
| Metric | Formula | Meaning |
|---|
Vocabulary Diversity (%) | unique_nouns / total_nouns ร 100 | ๋์์๋ก ๋ค์ํ ๋ช
์ฌ ์ดํ ์ฌ์ฉ |
Coverage Score (Top X%) | Top X% of unique noun types โ % of total occurrences | ์์ X% ์ดํ๊ฐ ์ ์ฒด ๋ช
์ฌ์ ๋ช % ์ฐจ์ง |
Coverage score example: Top 5% = the most-common 5% of unique noun types cover Y% of all noun occurrences. Low Y% โ diverse vocabulary; high Y% โ a few terms dominate.
Comparison with sentence-diversity
| Skill | Unit of analysis | POS filter |
|---|
| sentence-diversity | sentences | VB* (verbs) |
| vocabulary-diversity | words | NN* (nouns) |
NLTK Dependencies
The script auto-downloads required NLTK data on first run:
punkt / punkt_tab โ word tokenizer
averaged_perceptron_tagger / averaged_perceptron_tagger_eng โ POS tagger
Output
| File | Description |
|---|
vocabulary_diversity_summary.txt | Vocabulary Diversity (%), Coverage Scores for top 5/10/20% |
vocabulary_diversity_frequency.csv | Per-word: Count, Percentage (sorted by frequency) |
vocabulary_diversity_plot.png | Bar chart of top-N most frequent nouns |
How to Run
import pandas as pd
from scripts.vocabulary_diversity import get_vocabulary_diversity
quiq = pd.read_csv("/path/to/quiq.csv")
word_diversity, item_vs_pct, coverage_scores, freq_df = get_vocabulary_diversity(
quiq=quiq,
top_n=10
)
print(f"Vocabulary Diversity (%) = {word_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/vocabulary_diversity.py --config config.yaml
Critical Notes
-
note ํํฐ๋ง ์ ์ฉ โ Mapping_info_1์ note๊ฐ ํฌํจ๋ ๋ชจ๋ ํ์ด ๋์ (note_clinical, note_rad ๋ฑ).
-
Noun ๋จ์ด๋ง ์ง๊ณ โ NN* POS ํ๊ทธ๋ง ํฌํจ. ๋์ฌ, ํ์ฉ์ฌ, ๊ด์ฌ ๋ฑ์ ์ ์ธ. ์์ ๊ฐ์ฒด๋ช
(์ง๋จ๋ช
, ์ฝ๋ฌผ๋ช
๋ฑ)์ด ์ฃผ์ ๋ถ์ ๋์.
-
NLTK ์๋ ๋ค์ด๋ก๋ โ ์ฒซ ์คํ ์ punkt, averaged_perceptron_tagger๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ๋ค์ด๋ก๋๋จ.
-
์๋ณธ ์ฝ๋ ๊ฐ์ ์ฌํญ:
df_note['TEXT_words'] = ... SettingWithCopyWarning โ .copy() ํ ํ ๋น
assert len(df_note) > 0 โ ๋ช
์์ FAIL ๋ฉ์์ง ํ ๋น ๊ฒฐ๊ณผ ๋ฐํ (AssertionError ๋ฐฉ์ง)
total_words ๋ณ์ ์ด์ค ํ ๋น ํผ๋ โ total_count / counter ๋ช
ํํ ๋ถ๋ฆฌ
os.path.join ์ฌ์ฉ (f-string ๊ฒฝ๋ก ๋์ )
--config argparse์ required=True ์ถ๊ฐ
matplotlib.use('Agg') ์ถ๊ฐ (ํค๋๋ฆฌ์ค ํ๊ฒฝ)
-
Dependencies โ nltk, pandas, matplotlib
References
- LYDUS ํ์ง๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ํ์ฉ ๊ฐ์ด๋๋ผ์ธ (๋น๊ณต๊ฐ ๋ด๋ถ ๋ฌธ์)
- Original Python implementation: LYDUS_Vocabulary_Diversity.py (์ด์ฑ๋ฏผ ์์ฑ)
- Bird, S., Klein, E., & Loper, E. (2009). Natural Language Processing with Python. O'Reilly.