fix insert link naturally

main
Shane 7 months ago
parent 3d0d320648
commit 2ecab209c5
  1. 2
      foodie_config.py
  2. 19
      foodie_utils.py

@ -245,7 +245,7 @@ RSS_FEED_NAMES = {
"https://www.eater.com/rss/full.xml": ("Eater", "https://www.eater.com/"),
"https://www.nrn.com/rss.xml": ("Nation's Restaurant News", "https://www.nrn.com/"),
"https://rss.nytimes.com/services/xml/rss/nyt/DiningandWine.xml": ("The New York Times", "https://www.nytimes.com/section/food"),
"https://www.theguardian.com/food/rss": ("The Guardian Food", "https://www.theguardian.com/food")
"https://www.theguardian.com/food/rss": ("The Guardian", "https://www.theguardian.com/food")
}
RECIPE_KEYWORDS = ["recipe", "cook", "bake", "baking", "cooking", "ingredient", "method", "mix", "stir", "preheat", "dinners", "make", "dish", "healthy"]

@ -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)

Loading…
Cancel
Save