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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -2
app.py CHANGED
@@ -20,7 +20,11 @@ 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 algorithm'
 
 
 
 
24
  }
25
 
26
  # Color palette for keywords based on scores
@@ -109,6 +113,14 @@ class KeywordExtractionManager:
109
  return self.extract_rake_keywords(text, num_keywords, progress)
110
  elif model_name.startswith('textrank'):
111
  return self.extract_textrank_keywords(text, num_keywords, ngram_range, progress)
 
 
 
 
 
 
 
 
112
  else:
113
  raise ValueError(f"Unknown model: {model_name}")
114
 
@@ -393,7 +405,50 @@ class KeywordExtractionManager:
393
  except Exception as e:
394
  print(f"RAKE extraction failed: {type(e).__name__}: {str(e)}")
395
  return self.fallback_keyword_extraction(text, num_keywords)
396
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397
  def fallback_keyword_extraction(self, text, num_keywords=10):
398
  """Simple fallback keyword extraction using basic statistics"""
399
  print("Using fallback keyword extraction")
 
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
 
113
  return self.extract_rake_keywords(text, num_keywords, progress)
114
  elif model_name.startswith('textrank'):
115
  return self.extract_textrank_keywords(text, num_keywords, ngram_range, progress)
116
+ elif model_name.startswith('pke_'):
117
+ method = {
118
+ 'pke_topicrank': 'TopicRank',
119
+ 'pke_multipartiterank': 'MultipartiteRank',
120
+ 'pke_positionrank': 'PositionRank',
121
+ 'pke_textrank': 'TextRank',
122
+ }[model_name]
123
+ return self.extract_pke_keywords(text, num_keywords, method, progress)
124
  else:
125
  raise ValueError(f"Unknown model: {model_name}")
126
 
 
405
  except Exception as e:
406
  print(f"RAKE extraction failed: {type(e).__name__}: {str(e)}")
407
  return self.fallback_keyword_extraction(text, num_keywords)
408
+
409
+ def extract_pke_keywords(self, text, num_keywords, method, progress):
410
+ """Extract keyphrases using the pke library (paper's methods)."""
411
+ try:
412
+ import pke
413
+
414
+ if progress:
415
+ progress(0.5, desc=f"Processing with {method}...")
416
+
417
+ extractor_classes = {
418
+ 'TopicRank': pke.unsupervised.TopicRank,
419
+ 'MultipartiteRank': pke.unsupervised.MultipartiteRank,
420
+ 'PositionRank': pke.unsupervised.PositionRank,
421
+ 'TextRank': pke.unsupervised.TextRank,
422
+ }
423
+ if method not in extractor_classes:
424
+ return self.fallback_keyword_extraction(text, num_keywords)
425
+
426
+ extractor = extractor_classes[method]()
427
+ extractor.load_document(input=text, language='en')
428
+ extractor.candidate_selection()
429
+ extractor.candidate_weighting()
430
+
431
+ if progress:
432
+ progress(0.7, desc="Extracting keywords...")
433
+
434
+ keyphrases = extractor.get_n_best(n=num_keywords)
435
+
436
+ results = []
437
+ if keyphrases:
438
+ max_score = max(score for _, score in keyphrases) or 1.0
439
+ for phrase, score in keyphrases:
440
+ results.append({
441
+ 'keyword': phrase,
442
+ 'score': float(score) / float(max_score),
443
+ 'model': method,
444
+ })
445
+ print(f"{method} extracted {len(results)} keywords")
446
+ return results if results else self.fallback_keyword_extraction(text, num_keywords)
447
+
448
+ except Exception as e:
449
+ print(f"{method} extraction failed: {type(e).__name__}: {str(e)}")
450
+ return self.fallback_keyword_extraction(text, num_keywords)
451
+
452
  def fallback_keyword_extraction(self, text, num_keywords=10):
453
  """Simple fallback keyword extraction using basic statistics"""
454
  print("Using fallback keyword extraction")