شارك البودكاست

تشغيل برنامج بايثون علي الويب | الجزء الرابع

my instgram: www.instagram.com/codices6/

my spotify: open.spotify.com/show/6SmOwZlltLrYBhdMa18AJ6

code :

from flask import Flask, render_template, requestimport requestsimport jsonimport nltkfrom nltk.tokenize import word_tokenize, sent_tokenizefrom nltk.corpus import stopwordsfrom nltk.stem import WordNetLemmatizerapp = Flask(__name__)# Set up your Google Custom Search API credentialsapi_key = “YOUR_API_KEY”search_engine_id = “YOUR_SEARCH_ENGINE_ID”# Function to preprocess textdef preprocess_text(text):# Tokenize the text tokens = word_tokenize(text)# Remove stopwordsstop_words = set(stopwords.words(‘english’))filtered_tokens = [token for token in tokens if token.lower() not in stop_words]# Lemmatize the tokenslemmatizer = WordNetLemmatizer()lemmatized_tokens = [lemmatizer.lemmatize(token) for token in filtered_tokens]return lemmatized_tokens# Function to perform a search and retrieve code snippetsdef search_code(query):url = f”https://www.googleapis.com/customsearch/v1?key={api_key}&cx={search_engine_id}&q={query}”response = requests.get(url)data = json.loads(response.text)code_snippets = []if ‘items’ in data:for item in data[‘items’]:if ‘snippet’ in item: code_snippets.append(item[‘snippet’])return code_snippets# Function to answer a programming questiondef answer_question(question):# Preprocess the questionquestion_tokens = preprocess_text(question)# Search for code snippetsquery = f”{question} code snippet”code_snippets = search_code(query)# Find the most relevant code snippet based on the questionmax_score = 0best_snippet = Nonefor snippet in code_snippets:snippet_tokens = preprocess_text(snippet)score = nltk.jaccard_distance(set(question_tokens), set(snippet_tokens)) if score > max_score:max_score = scorebest_snippet = snippetreturn best_snippet# Define a route for the web page@app.route(‘/’, methods=[‘GET’, ‘POST’])def home():# Check if a form was submittedif request.method == ‘POST’:# Get the programming question from the formquestion = request.form[‘question’]# Get the answer to the questionanswer = answer_question(question)# Render the HTML template with the answerreturn render_template(‘answer.html’, question=question, answer=answer)# Render the HTML template with an empty formreturn render_template(‘home.html’)

النشرة الأسبوعية مساءً كل يوم سبت من اختيار المحررين

نشرة أسبوعية مسائية من بودكاست فلسطين تصلُك إلى بريدك الإلكتروني، تُقدِّم أمتع وأفضل الحلقات من أكثر من ٣٠٠ برنامج بودكاست عربي نختارها لك لتستمع وتستمتع وتتعلّم.

Series Navigation<< تجربة انشاء نموذج ذكاء اصطناعي بسيط بلغة بايثون | الجزء الثالثاساسيات جافا اسكريبت | اخر حلقة في الموسم الاول >>