MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / _extract_heuristic_keywords

Function _extract_heuristic_keywords

agents/reading-agent/main.py:3806–3875  ·  view source on GitHub ↗

Heuristic keyword extraction (fallback when LLM doesn't return keywords). Picks 5-8 short noun phrases from title + abstract by frequency, excluding stopwords. Each phrase is 1-3 lowercase words.

(
    paper: Dict[str, Any],
    abstract: str,
    parsed_pdf: Optional[Dict[str, Any]] = None,
)

Source from the content-addressed store, hash-verified

3804
3805def _extract_heuristic_keywords(
3806 paper: Dict[str, Any],
3807 abstract: str,
3808 parsed_pdf: Optional[Dict[str, Any]] = None,
3809) -> List[str]:
3810 """Heuristic keyword extraction (fallback when LLM doesn't return keywords).
3811
3812 Picks 5-8 short noun phrases from title + abstract by frequency,
3813 excluding stopwords. Each phrase is 1-3 lowercase words.
3814 """
3815 title = _clean_text(paper.get("title")) or ""
3816 abstract_text = _clean_text(abstract) or ""
3817 pdf_text = ""
3818 if parsed_pdf:
3819 pdf_text = _clean_text(parsed_pdf.get("abstract") or parsed_pdf.get("body_text") or "")
3820 corpus = " ".join(filter(None, [title, abstract_text, pdf_text[:2000]])).lower()
3821 if not corpus:
3822 return []
3823
3824 stop = {
3825 "the", "a", "an", "and", "or", "of", "to", "in", "on", "for", "with",
3826 "by", "is", "are", "was", "were", "be", "been", "being", "this", "that",
3827 "these", "those", "we", "our", "ours", "i", "me", "my", "they", "them",
3828 "their", "it", "its", "as", "at", "from", "into", "than", "but",
3829 "can", "could", "may", "might", "must", "should", "would", "will",
3830 "have", "has", "had", "do", "does", "did", "not", "no", "yes", "if",
3831 "while", "such", "more", "most", "less", "least", "very", "much",
3832 "however", "thus", "hence", "also", "only", "even", "still", "yet",
3833 "show", "shows", "showed", "shown", "use", "used", "using", "uses",
3834 "propose", "proposed", "proposes", "proposing", "present", "presents",
3835 "presented", "presenting", "introduce", "introduces", "introduced",
3836 "achieve", "achieves", "achieved", "based", "method", "methods",
3837 "approach", "approaches", "model", "models", "result", "results",
3838 "paper", "papers", "study", "studies", "studied", "work", "works",
3839 "experiment", "experiments", "task", "tasks", "data", "dataset",
3840 "datasets", "table", "tables", "figure", "figures", "section",
3841 "sections", "appendix", "et", "al", "etc", "ie", "eg",
3842 }
3843 tokens = re.findall(r"[a-z][a-z0-9\-]+", corpus)
3844 tokens = [t for t in tokens if t not in stop and len(t) > 2]
3845
3846 bigrams = [f"{tokens[i]} {tokens[i + 1]}" for i in range(len(tokens) - 1)]
3847 bigrams = [b for b in bigrams if not any(w in stop for w in b.split())]
3848
3849 counts: Dict[str, int] = {}
3850 for token in tokens:
3851 counts[token] = counts.get(token, 0) + 1
3852 bigram_counts: Dict[str, int] = {}
3853 for bigram in bigrams:
3854 bigram_counts[bigram] = bigram_counts.get(bigram, 0) + 1
3855
3856 picked: List[str] = []
3857 for bigram, count in sorted(bigram_counts.items(), key=lambda kv: (-kv[1], kv[0])):
3858 if count < 2:
3859 break
3860 picked.append(bigram)
3861 if len(picked) >= 5:
3862 break
3863

Callers 1

Calls 2

getMethod · 0.80
_clean_textFunction · 0.70

Tested by

no test coverage detected