pyPaperFlow 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyPaperFlow/utils.py ADDED
@@ -0,0 +1,70 @@
1
+ from typing import *
2
+ import re
3
+
4
+
5
+ #############################################################
6
+ # 1, For Pubmed Parser
7
+ #############################################################
8
+
9
+ def extract_urls_from_text(text: str, source_tag: Literal["abstract", "full_text"]) -> List[Dict[str, str]]:
10
+ """
11
+ Description
12
+ -----------
13
+ Extract URLs from the given text, and attempt to categorize them.
14
+
15
+ Args
16
+ ----
17
+ text (str): The input text from which to extract URLs.
18
+ source (Literal["abstract", "full_text"]): The source of the text, either "abstract" or "full_text".
19
+
20
+ Returns
21
+ -------
22
+ List[Dict[str, str]]: A list of dictionaries, each containing:
23
+ - "url": The extracted URL.
24
+ - "source": The source of the URL extraction (e.g., "abstract", "full_text").
25
+ - "category": A simple category based on URL patterns (e.g., "GitHub", "Zenodo", etc.).
26
+
27
+ """
28
+ if not text:
29
+ return []
30
+
31
+ # Match URLs starting with http://, https://, ftp://, or www.
32
+ url_pattern = r'(https?://[^\s,;>)]+|www\.[^\s,;>)]+|ftp://[^\s,;>)]+)'
33
+
34
+ found_urls = re.findall(url_pattern, text)
35
+
36
+ results = []
37
+ seen = set() # deduplicate URLs
38
+
39
+ for url in found_urls:
40
+ # clearing trailing punctuation like . , ; ) >
41
+ url = url.rstrip('.,;)>')
42
+
43
+ if url in seen:
44
+ continue
45
+ seen.add(url)
46
+
47
+ # Simple categorization based on URL patterns
48
+ category = "General"
49
+ if "github.com" in url:
50
+ category = "GitHub"
51
+ elif "gitlab.com" in url:
52
+ category = "GitLab"
53
+ elif "zenodo.org" in url:
54
+ category = "Zenodo"
55
+ elif "figshare.com" in url:
56
+ category = "Figshare"
57
+ elif "huggingface.co" in url:
58
+ category = "HuggingFace"
59
+ elif "drive.google.com" in url:
60
+ category = "Google Drive"
61
+
62
+ results.append({
63
+ "url": url,
64
+ "source": source_tag, # for now we just label as abstract
65
+ "category": category
66
+ })
67
+
68
+ return results
69
+
70
+