fix insert link naturally

This commit is contained in:
2025-05-04 13:40:28 +10:00
parent 3d0d320648
commit 2ecab209c5
2 changed files with 6 additions and 15 deletions
+5 -14
View File
@@ -607,25 +607,16 @@ def insert_link_naturally(summary, source_name, source_url):
target_para = random.choice(eligible_paragraphs)
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:
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)
link_pattern = f'<a href="{source_url}">{source_name}</a>'
words = sentence.split()
if len(words) < 5: # Ensure enough words for natural insertion
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 += '.'
# Insert the link at the end of the sentence
new_sentence = f"{sentence.rstrip('.')} according to {link_pattern}."
sentences[sentence_idx] = new_sentence
new_para = ' '.join(sentences)