-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrel.py
More file actions
73 lines (59 loc) · 1.95 KB
/
Copy pathrel.py
File metadata and controls
73 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import requests
from bs4 import BeautifulSoup
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
def calculate_relevance(text1, text2):
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform([text1, text2])
cosine_sim = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])
return cosine_sim[0][0]
def get_title_from_url(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
return soup.title.string if soup.title else url
except:
return url
def get_content_from_url(url):
try:
response = requests.get(url)
return response.text
except:
return ""
# Read existing JSON files
with open('links.json', 'r') as f:
links_data = json.load(f)['links']
with open('questions.json', 'r') as f:
questions_data = json.load(f)['questions']
final_output = {
"questions": questions_data,
"urls": []
}
for url in links_data:
content = get_content_from_url(url)
relevant_links = []
for other_url in links_data:
if other_url != url:
other_content = get_content_from_url(other_url)
relevance_score = calculate_relevance(content, other_content)
relevant_links.append((other_url, relevance_score))
# Sort by relevance and take top 5
relevant_links.sort(key=lambda x: x[1], reverse=True)
top_5_links = relevant_links[:5]
formatted_links = [
{
"url": link[0],
"title": get_title_from_url(link[0])
}
for link in top_5_links
]
webpage_data = {
"url": url,
"relevant_links": formatted_links
}
final_output["urls"].append(webpage_data)
# Save the final output
with open('final_output.json', 'w') as f:
json.dump(final_output, f, indent=4)
print("Final output has been saved to final_output.json")