web analytics
Unlock the Magic of Your kitchen with Our Cookbook!

By: Nancy in CA

admin
By admin
6 Min Read
Disclosure: This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!

In reply to Liz.

Deb, we live close to the section of California coast that includes “The artichoke capitol of the world”. When I was growing up, we’d drive down to Half Moon Bay to buy them from farm stands. Alas, artichokes are not inexpensive here, either (what is these days?) I’m entirely inclined to having a big artichoke and a glass of wine for dinner. I will have a go at steaming them in halves, sticking with my lemoned-up mayo. Something in me rejects the idea of butter, garlic, etc. as…cluttered?

version ai :

python
import requests
from bs4 import BeautifulSoup, NavigableString
import re

def rephrase_simple(text):
“””
A basic text rephrasing function using word substitutions and minor sentence restructuring.
This aims to change the wording without losing the original meaning, for demonstration purposes.
“””
text = text.strip()
if not text:
return “”

# Common phrases and words substitution
text = re.sub(r'\bI love\b', 'I deeply appreciate', text, flags=re.IGNORECASE)
text = re.sub(r'\bIt\'s\b', 'It is', text)
text = re.sub(r'\bI like\b', 'I truly enjoy', text, flags=re.IGNORECASE)
text = re.sub(r'\brecipe\b', 'culinary guide', text, flags=re.IGNORECASE)
text = re.sub(r'\bcook\b', 'prepare', text, flags=re.IGNORECASE)
text = re.sub(r'\bdelicious\b', 'delectable', text, flags=re.IGNORECASE)
text = re.sub(r'\bquick\b', 'rapid', text, flags=re.IGNORECASE)
text = re.sub(r'\beasy\b', 'straightforward', text, flags=re.IGNORECASE)
text = re.sub(r'\bsimple\b', 'uncomplicated', text, flags=re.IGNORECASE)
text = re.sub(r'\bgreat\b', 'fantastic', text, flags=re.IGNORECASE)
text = re.sub(r'\bamazing\b', 'astonishing', text, flags=re.IGNORECASE)
text = re.sub(r'\bperfect\b', 'ideal', text, flags=re.IGNORECASE)
text = re.sub(r'\bmake sure\b', 'ensure', text, flags=re.IGNORECASE)
text = re.sub(r'\btry this\b', 'give this a go', text, flags=re.IGNORECASE)
text = re.sub(r'\bcan\'t wait\b', 'eagerly anticipate', text, flags=re.IGNORECASE)
text = re.sub(r'\bso much\b', 'a great deal', text, flags=re.IGNORECASE)
text = re.sub(r'\bget started\b', 'commence', text, flags=re.IGNORECASE)
text = re.sub(r'\bthing\b', 'item', text, flags=re.IGNORECASE)
text = re.sub(r'\bstuff\b', 'items', text, flags=re.IGNORECASE)
text = re.sub(r'\bjust\b', 'merely', text, flags=re.IGNORECASE)
text = re.sub(r'\blittle\b', 'small', text, flags=re.IGNORECASE)
text = re.sub(r'\bbig\b', 'large', text, flags=re.IGNORECASE)
text = re.sub(r'\bvery\b', 'exceptionally', text, flags=re.IGNORECASE)
text = re.sub(r'\band\b', 'as well as', text, flags=re.IGNORECASE)
text = re.sub(r'\bbut\b', 'however', text, flags=re.IGNORECASE)
text = re.sub(r'\bso\b', 'consequently', text, flags=re.IGNORECASE)

# Some basic sentence restructuring or embellishment
if text.startswith("This is a"):
    text = f"Behold, this is a{text[len('This is a'):]}"
elif text.startswith("I can't believe"):
    text = f"It's quite incredible that{text[len('I can\'t believe'):]}"

return text

def rewrite_text_content_recursive(element):
“””
Recursively processes an element:

  • If it’s a NavigableString (text node), it rewrites it using rephrase_simple.
  • If it’s one of the specified sensitive tags (like img, script, style), it returns
    the original element directly to preserve its exact structure and attributes.
  • For all other tags, it creates a new tag, copies its attributes, and then
    recursively processes its children.
    “””

    If it’s a string, rewrite it

    if isinstance(element, NavigableString):
    return rephrase_simple(element)

# If it's not a tag (e.g., Comment, Doctype), return it as is
if not hasattr(element, 'name'):
    return element

# Tags to be copied directly without *any* modification (including children processing)
# This list includes images, scripts, styles, and other tags whose content or
# attributes are critical and should not be rephrased.
if element.name in ['img', 'script', 'style', 'noscript', 'video', 'audio', 'iframe', 'svg', 'math', 'form', 'input', 'textarea', 'select', 'option', 'canvas', 'embed', 'object', 'link', 'meta']:
    # Return the original element as is. This ensures all its original attributes,
    # internal structure, and text content (e.g., script code) are preserved.
    return element

# For other tags, create a new tag and process its children
new_tag = BeautifulSoup('', 'html.parser').new_tag(element.name)
for attr, value in element.attrs.items():
    new_tag[attr] = value

for child in element.children:
    processed_child = rewrite_text_content_recursive(child)
    if processed_child is not None:
        new_tag.append(processed_child)

return new_tag

def get_rewritten_article(url):
“””
Fetches the article content from the given URL, rewrites its text content
while preserving images and other sensitive HTML structures, and returns
the rewritten HTML.
“””
try:
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36’
}
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
except requests.exceptions.RequestException as e:
return f”Error fetching the URL: {e}”

soup = BeautifulSoup(response.text, 'html.parser')

# Smitten Kitchen articles typically have the main content in a div with class 'entry-content'.
article_content = soup.find('div', class_='entry-content')

if not article_content:
    # Fallback to more general tags if 'entry-content' is not found
    article_content = soup.find('article')
    if not article_content:
        article_content = soup.find('main')
        if not article_content:
            return "Could not find the main article content on the page."

# Create a new BeautifulSoup object to build the rewritten content
rewritten_soup = BeautifulSoup('', 'html.parser')

# Process all direct children of the identified article_content
for element in article_content.children:
    rewritten_element = rewrite_text_content_recursive(element)
    if rewritten_element is not None:
        rewritten_soup.append(rewritten_element)

# Return the HTML string of the rewritten content without any extra explanation.
return str(rewritten_soup)

The URL of the article to process

url = “https://smittenkitchen.com/2024/04/steamed-artichokes/

Get the rewritten article HTML

final_article_html = get_rewritten_article(url)

Print the final rewritten HTML article

print(final_article_html)

Share This Article
Leave a Comment