SorrelC commited on
Commit
678d0df
·
verified ·
1 Parent(s): 6c8c95b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -35
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 with Cosine Similarity - Document similarity approach',
22
- 'rake_nltk': 'RAKE-NLTK - Rapid Automatic Keyword Extraction',
23
- 'textrank': 'TextRank - Graph-based ranking (approximation)',
24
- 'pke_topicrank': 'TopicRank (pke) - graph-based, paper method',
25
- 'pke_multipartiterank': 'MultipartiteRank (pke) - paper top performer',
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': '#00B894', # Green - High relevance
33
- 'medium': '#F9CA24', # Yellow - Medium relevance
34
- 'low': '#FF6B6B' # Red - Low relevance
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: #00B894; padding: 4px 12px; border-radius: 15px; color: white; font-weight: bold;'>
615
  High Relevance (70%+)
616
  </span>
617
- <span style='background-color: #F9CA24; padding: 4px 12px; border-radius: 15px; color: white; font-weight: bold;'>
618
  Medium Relevance (40-70%)
619
  </span>
620
- <span style='background-color: #FF6B6B; padding: 4px 12px; border-radius: 15px; color: white; font-weight: bold;'>
621
- Low Relevance (<40%)
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 = selected_model.replace('yake_', '').replace('tfidf_', 'TF-IDF ').replace('rake_', 'RAKE-').replace('textrank', 'TextRank').title()
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 uses modern keyword extraction methods including YAKE, TF-IDF, RAKE, and TextRank for comprehensive analysis.
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
- <dl style="margin: 0; font-size: 14px;">
756
- <div style="margin-bottom: 8px;">
757
- <dt style="font-weight: bold; display: inline; color: #FF6B6B;">YAKE:</dt>
758
- <dd style="display: inline; margin-left: 5px;">Statistical approach requiring no training - works well on short texts and multilingual content</dd>
 
759
  </div>
760
- <div style="margin-bottom: 8px;">
761
- <dt style="font-weight: bold; display: inline; color: #795548;">TF-IDF with Cosine Similarity:</dt>
762
- <dd style="display: inline; margin-left: 5px;">Document similarity approach - extracts keywords most similar to the overall document</dd>
763
  </div>
764
- <div style="margin-bottom: 8px;">
765
- <dt style="font-weight: bold; display: inline; color: #FF5722;">RAKE-NLTK:</dt>
766
- <dd style="display: inline; margin-left: 5px;">Classic keyword extraction algorithm - fast and reliable for phrase extraction</dd>
767
  </div>
768
- <div style="margin-bottom: 8px;">
769
- <dt style="font-weight: bold; display: inline; color: #607D8B;">TextRank:</dt>
770
- <dd style="display: inline; margin-left: 5px;">Graph-based ranking algorithm inspired by PageRank - good for extracting key concepts</dd>
 
 
 
 
 
 
 
 
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
- "rake_nltk",
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>RAKE-NLTK:</strong>
856
- <a href="https://github.com/csurfer/rake-nltk" target="_blank" style="color: #1976d2;">
857
- Rapid Automatic Keyword Extraction with NLTK ↗
 
 
 
 
 
 
 
 
 
 
858
  </a>
859
  </li>
860
  <li><strong>TextRank:</strong>
861
- <a href="https://web.eecs.umich.edu/~mihalcea/papers/mihalcea.emnlp04.pdf" target="_blank" style="color: #1976d2;">
862
- TextRank: Bringing Order into Text ↗
 
 
 
 
 
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 (&lt;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> &mdash; 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 &mdash; 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 &mdash; 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 &mdash; 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 &amp; 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 &amp; 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>