Update app.py
Browse files
app.py
CHANGED
|
@@ -18,20 +18,18 @@ print("NLTK data downloaded.")
|
|
| 18 |
# Reliable model names and descriptions - replaced KeyBERT with working alternatives
|
| 19 |
KEYWORD_MODELS = {
|
| 20 |
'yake_yake': 'YAKE - Yet Another Keyword Extractor (statistical)',
|
| 21 |
-
'tfidf_cosine': 'TF-IDF
|
| 22 |
-
'
|
| 23 |
-
'
|
| 24 |
-
'
|
| 25 |
-
'
|
| 26 |
-
'pke_positionrank': 'PositionRank (pke) - paper method',
|
| 27 |
-
'pke_textrank': 'TextRank (pke) - true graph-based TextRank',
|
| 28 |
}
|
| 29 |
|
| 30 |
# Color palette for keywords based on scores
|
| 31 |
SCORE_COLORS = {
|
| 32 |
-
'high': '#
|
| 33 |
-
'medium': '#
|
| 34 |
-
'low': '#
|
| 35 |
}
|
| 36 |
|
| 37 |
# Additional colors for variety
|
|
@@ -611,14 +609,14 @@ def create_legend_html():
|
|
| 611 |
<div style='margin: 15px 0; padding: 15px; background-color: #f8f9fa; border-radius: 8px;'>
|
| 612 |
<h4 style='margin: 0 0 15px 0;'>🎨 Relevance Score Legend</h4>
|
| 613 |
<div style='display: flex; flex-wrap: wrap; gap: 15px;'>
|
| 614 |
-
<span style='background-color: #
|
| 615 |
High Relevance (70%+)
|
| 616 |
</span>
|
| 617 |
-
<span style='background-color: #
|
| 618 |
Medium Relevance (40-70%)
|
| 619 |
</span>
|
| 620 |
-
<span style='background-color: #
|
| 621 |
-
Low Relevance (
|
| 622 |
</span>
|
| 623 |
</div>
|
| 624 |
</div>
|
|
@@ -661,7 +659,7 @@ def process_text(text, selected_model, num_keywords, ngram_min, ngram_max, progr
|
|
| 661 |
|
| 662 |
# Create summary
|
| 663 |
avg_score = sum(k['score'] for k in keywords) / len(keywords)
|
| 664 |
-
model_display =
|
| 665 |
summary = f"""
|
| 666 |
## 📊 Analysis Summary
|
| 667 |
- **Keywords extracted:** {len(keywords)}
|
|
@@ -680,7 +678,7 @@ def create_interface():
|
|
| 680 |
gr.Markdown("""
|
| 681 |
# Keyword Extraction Explorer Tool
|
| 682 |
|
| 683 |
-
Extract the most important keywords and phrases from your text using various algorithms! This tool
|
| 684 |
|
| 685 |
### How to use:
|
| 686 |
1. **📝 Enter your text** in the text area below
|
|
@@ -752,24 +750,34 @@ def create_interface():
|
|
| 752 |
ℹ️ Model Descriptions
|
| 753 |
</summary>
|
| 754 |
<div style="margin-top: 10px; padding: 10px;">
|
| 755 |
-
<
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
<
|
|
|
|
| 759 |
</div>
|
| 760 |
-
<div style="margin-bottom:
|
| 761 |
-
<dt style="font-weight: bold; display: inline; color: #
|
| 762 |
-
<dd style="display: inline; margin-left: 5px;">
|
| 763 |
</div>
|
| 764 |
-
<div style="margin-bottom:
|
| 765 |
-
<dt style="font-weight: bold; display: inline; color: #
|
| 766 |
-
<dd style="display: inline; margin-left: 5px;">
|
| 767 |
</div>
|
| 768 |
-
<div style="margin-bottom:
|
| 769 |
-
<dt style="font-weight: bold; display: inline; color: #
|
| 770 |
-
<dd style="display: inline; margin-left: 5px;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
</div>
|
| 772 |
</dl>
|
|
|
|
| 773 |
</div>
|
| 774 |
</details>
|
| 775 |
""")
|
|
@@ -820,7 +828,7 @@ def create_interface():
|
|
| 820 |
],
|
| 821 |
[
|
| 822 |
"Charles Darwin arrived at the Galápagos Islands aboard HMS Beagle in September 1835. During his five-week visit, Darwin collected specimens of finches, tortoises, and mockingbirds. His observations of these species' variations across different islands later contributed to his theory of evolution by natural selection, published in 'On the Origin of Species' in 1859.",
|
| 823 |
-
"
|
| 824 |
10,
|
| 825 |
1,
|
| 826 |
3
|
|
@@ -852,14 +860,29 @@ def create_interface():
|
|
| 852 |
Term Frequency-Inverse Document Frequency ↗
|
| 853 |
</a>
|
| 854 |
</li>
|
| 855 |
-
<li><strong>
|
| 856 |
-
<a href="https://
|
| 857 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
</a>
|
| 859 |
</li>
|
| 860 |
<li><strong>TextRank:</strong>
|
| 861 |
-
<a href="https://
|
| 862 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 863 |
</a>
|
| 864 |
</li>
|
| 865 |
</ul>
|
|
|
|
| 18 |
# Reliable model names and descriptions - replaced KeyBERT with working alternatives
|
| 19 |
KEYWORD_MODELS = {
|
| 20 |
'yake_yake': 'YAKE - Yet Another Keyword Extractor (statistical)',
|
| 21 |
+
'tfidf_cosine': 'TF-IDF - Term frequency-inverse document frequency (statistical)',
|
| 22 |
+
'pke_topicrank': 'TopicRank - graph-based keyphrase ranking',
|
| 23 |
+
'pke_multipartiterank': 'MultipartiteRank - graph-based, topic-aware ranking',
|
| 24 |
+
'pke_positionrank': 'PositionRank - graph-based, position-weighted ranking',
|
| 25 |
+
'pke_textrank': 'TextRank - graph-based ranking inspired by PageRank',
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
# Color palette for keywords based on scores
|
| 29 |
SCORE_COLORS = {
|
| 30 |
+
'high': '#0E9F6E', # Green - High relevance
|
| 31 |
+
'medium': '#B45309', # Amber/brown - Medium relevance (dark enough for white text)
|
| 32 |
+
'low': '#DC2626' # Red - Low relevance
|
| 33 |
}
|
| 34 |
|
| 35 |
# Additional colors for variety
|
|
|
|
| 609 |
<div style='margin: 15px 0; padding: 15px; background-color: #f8f9fa; border-radius: 8px;'>
|
| 610 |
<h4 style='margin: 0 0 15px 0;'>🎨 Relevance Score Legend</h4>
|
| 611 |
<div style='display: flex; flex-wrap: wrap; gap: 15px;'>
|
| 612 |
+
<span style='background-color: #0E9F6E; padding: 4px 12px; border-radius: 15px; color: white; font-weight: bold;'>
|
| 613 |
High Relevance (70%+)
|
| 614 |
</span>
|
| 615 |
+
<span style='background-color: #B45309; padding: 4px 12px; border-radius: 15px; color: white; font-weight: bold;'>
|
| 616 |
Medium Relevance (40-70%)
|
| 617 |
</span>
|
| 618 |
+
<span style='background-color: #DC2626; padding: 4px 12px; border-radius: 15px; color: white; font-weight: bold;'>
|
| 619 |
+
Low Relevance (<40%)
|
| 620 |
</span>
|
| 621 |
</div>
|
| 622 |
</div>
|
|
|
|
| 659 |
|
| 660 |
# Create summary
|
| 661 |
avg_score = sum(k['score'] for k in keywords) / len(keywords)
|
| 662 |
+
model_display = KEYWORD_MODELS.get(selected_model, selected_model).split(' - ')[0]
|
| 663 |
summary = f"""
|
| 664 |
## 📊 Analysis Summary
|
| 665 |
- **Keywords extracted:** {len(keywords)}
|
|
|
|
| 678 |
gr.Markdown("""
|
| 679 |
# Keyword Extraction Explorer Tool
|
| 680 |
|
| 681 |
+
Extract the most important keywords and phrases from your text using various algorithms! This tool compares statistical methods (YAKE, TF-IDF) with graph-based methods (TopicRank, MultipartiteRank, PositionRank, TextRank) so you can see how each approaches the same text.
|
| 682 |
|
| 683 |
### How to use:
|
| 684 |
1. **📝 Enter your text** in the text area below
|
|
|
|
| 750 |
ℹ️ Model Descriptions
|
| 751 |
</summary>
|
| 752 |
<div style="margin-top: 10px; padding: 10px;">
|
| 753 |
+
<p style="font-size: 14px; margin: 0 0 12px 0;">All six methods here are <strong>statistical or graph-based</strong> — they read only your text, need no training data, and run entirely on your machine. They differ in how they decide which words matter.</p>
|
| 754 |
+
<dl style="margin: 0; font-size: 14px; line-height: 1.5;">
|
| 755 |
+
<div style="margin-bottom: 10px;">
|
| 756 |
+
<dt style="font-weight: bold; display: inline; color: #0E9F6E;">YAKE:</dt>
|
| 757 |
+
<dd style="display: inline; margin-left: 5px;">Scores each word using simple text features — how often it appears, where it sits, whether it's capitalised, and what surrounds it. Good all-rounder, works on short texts and many languages.</dd>
|
| 758 |
</div>
|
| 759 |
+
<div style="margin-bottom: 10px;">
|
| 760 |
+
<dt style="font-weight: bold; display: inline; color: #0E9F6E;">TF-IDF:</dt>
|
| 761 |
+
<dd style="display: inline; margin-left: 5px;">Picks out words that are common in <em>your</em> text but rare in general — the ones that make it distinctive. A long-standing, dependable baseline.</dd>
|
| 762 |
</div>
|
| 763 |
+
<div style="margin-bottom: 10px;">
|
| 764 |
+
<dt style="font-weight: bold; display: inline; color: #1976d2;">TopicRank:</dt>
|
| 765 |
+
<dd style="display: inline; margin-left: 5px;">Groups similar words into topics first, then ranks the topics and picks one phrase to represent each. Reduces near-duplicates, so results cover a broader spread of themes.</dd>
|
| 766 |
</div>
|
| 767 |
+
<div style="margin-bottom: 10px;">
|
| 768 |
+
<dt style="font-weight: bold; display: inline; color: #1976d2;">MultipartiteRank:</dt>
|
| 769 |
+
<dd style="display: inline; margin-left: 5px;">A refinement of TopicRank that keeps individual phrases as the unit while still respecting topics. Tends to give the most balanced, precise keyphrases (the strongest performer in our tests).</dd>
|
| 770 |
+
</div>
|
| 771 |
+
<div style="margin-bottom: 10px;">
|
| 772 |
+
<dt style="font-weight: bold; display: inline; color: #1976d2;">PositionRank:</dt>
|
| 773 |
+
<dd style="display: inline; margin-left: 5px;">Gives extra weight to words that appear early in the text, on the idea that important terms are often introduced up front. Useful for structured writing like articles and abstracts.</dd>
|
| 774 |
+
</div>
|
| 775 |
+
<div style="margin-bottom: 10px;">
|
| 776 |
+
<dt style="font-weight: bold; display: inline; color: #1976d2;">TextRank:</dt>
|
| 777 |
+
<dd style="display: inline; margin-left: 5px;">Builds a network of words that appear near each other and ranks them using the same idea as Google's PageRank — a word is important if other important words connect to it.</dd>
|
| 778 |
</div>
|
| 779 |
</dl>
|
| 780 |
+
<p style="font-size: 13px; margin: 12px 0 0 0; color: #555;"><em>TopicRank, MultipartiteRank, PositionRank and TextRank are graph-based methods provided by the <a href="https://github.com/boudinfl/pke" target="_blank" style="color: #1976d2;">pke</a> library.</em></p>
|
| 781 |
</div>
|
| 782 |
</details>
|
| 783 |
""")
|
|
|
|
| 828 |
],
|
| 829 |
[
|
| 830 |
"Charles Darwin arrived at the Galápagos Islands aboard HMS Beagle in September 1835. During his five-week visit, Darwin collected specimens of finches, tortoises, and mockingbirds. His observations of these species' variations across different islands later contributed to his theory of evolution by natural selection, published in 'On the Origin of Species' in 1859.",
|
| 831 |
+
"pke_multipartiterank",
|
| 832 |
10,
|
| 833 |
1,
|
| 834 |
3
|
|
|
|
| 860 |
Term Frequency-Inverse Document Frequency ↗
|
| 861 |
</a>
|
| 862 |
</li>
|
| 863 |
+
<li><strong>TopicRank:</strong>
|
| 864 |
+
<a href="https://aclanthology.org/I13-1062/" target="_blank" style="color: #1976d2;">
|
| 865 |
+
Bougouin et al. (2013) ↗
|
| 866 |
+
</a>
|
| 867 |
+
</li>
|
| 868 |
+
<li><strong>MultipartiteRank:</strong>
|
| 869 |
+
<a href="https://aclanthology.org/N18-2105/" target="_blank" style="color: #1976d2;">
|
| 870 |
+
Boudin (2018) ↗
|
| 871 |
+
</a>
|
| 872 |
+
</li>
|
| 873 |
+
<li><strong>PositionRank:</strong>
|
| 874 |
+
<a href="https://aclanthology.org/P17-1102/" target="_blank" style="color: #1976d2;">
|
| 875 |
+
Florescu & Caragea (2017) ↗
|
| 876 |
</a>
|
| 877 |
</li>
|
| 878 |
<li><strong>TextRank:</strong>
|
| 879 |
+
<a href="https://aclanthology.org/W04-3252/" target="_blank" style="color: #1976d2;">
|
| 880 |
+
Mihalcea & Tarau (2004) ↗
|
| 881 |
+
</a>
|
| 882 |
+
</li>
|
| 883 |
+
<li><strong>pke library</strong> (TopicRank, MultipartiteRank, PositionRank, TextRank):
|
| 884 |
+
<a href="https://github.com/boudinfl/pke" target="_blank" style="color: #1976d2;">
|
| 885 |
+
Python Keyphrase Extraction ↗
|
| 886 |
</a>
|
| 887 |
</li>
|
| 888 |
</ul>
|