|
|
|
@ -607,25 +607,16 @@ def insert_link_naturally(summary, source_name, source_url): |
|
|
|
target_para = random.choice(eligible_paragraphs) |
|
|
|
target_para = random.choice(eligible_paragraphs) |
|
|
|
sentences = re.split(r'(?<=[.!?])\s+', target_para.strip()) |
|
|
|
sentences = re.split(r'(?<=[.!?])\s+', target_para.strip()) |
|
|
|
|
|
|
|
|
|
|
|
eligible_sentences = [(i, s) for i, s in enumerate(sentences) if i < len(sentences)-1 and s.strip()] |
|
|
|
eligible_sentences = [(i, s) for i, s in enumerate(sentences) if s.strip()] |
|
|
|
if not eligible_sentences: |
|
|
|
if not eligible_sentences: |
|
|
|
eligible_sentences = [(i, s) for i, s in enumerate(sentences) if s.strip()] |
|
|
|
logging.error("No eligible sentences found for link insertion.") |
|
|
|
|
|
|
|
return summary |
|
|
|
|
|
|
|
|
|
|
|
sentence_idx, sentence = random.choice(eligible_sentences) |
|
|
|
sentence_idx, sentence = random.choice(eligible_sentences) |
|
|
|
link_pattern = f'<a href="{source_url}">{source_name}</a>' |
|
|
|
link_pattern = f'<a href="{source_url}">{source_name}</a>' |
|
|
|
|
|
|
|
|
|
|
|
words = sentence.split() |
|
|
|
# Insert the link at the end of the sentence |
|
|
|
if len(words) < 5: # Ensure enough words for natural insertion |
|
|
|
new_sentence = f"{sentence.rstrip('.')} according to {link_pattern}." |
|
|
|
new_sentence = f"{sentence.rstrip('.')} according to {link_pattern}." |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
split_point = random.randint(2, len(words)-3) # Split further into the sentence |
|
|
|
|
|
|
|
# Remove trailing punctuation from the first part and ensure proper grammar |
|
|
|
|
|
|
|
first_part = ' '.join(words[:split_point]).rstrip(',') |
|
|
|
|
|
|
|
second_part = ' '.join(words[split_point:]).lstrip(',') |
|
|
|
|
|
|
|
new_sentence = f"{first_part} according to {link_pattern} {second_part}" |
|
|
|
|
|
|
|
# Ensure the sentence ends with a period |
|
|
|
|
|
|
|
if not new_sentence.endswith('.'): |
|
|
|
|
|
|
|
new_sentence += '.' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sentences[sentence_idx] = new_sentence |
|
|
|
sentences[sentence_idx] = new_sentence |
|
|
|
new_para = ' '.join(sentences) |
|
|
|
new_para = ' '.join(sentences) |
|
|
|
|