Skip to content

Commit ea5bd52

Browse files
Update ml-code-optimiser.py
1 parent 4c6b23c commit ea5bd52

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/backend/neploy-build-python/ml-code-optimiser.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,18 @@
3838
with open("test.py", "r") as f:
3939
code = f.read()
4040

41-
# Predict the improvement suggestions for the code
42-
new_code_vectorized = vectorizer.transform([code])
43-
prediction = model.predict(new_code_vectorized)[0]
44-
45-
# Print the improvement suggestions
46-
if prediction == "optimize":
47-
print("The code needs to be optimized.")
48-
49-
# Get the top 3 suggestions
50-
top_3_suggestions = model.predict_proba(new_code_vectorized).argsort()[-3:][::-1]
51-
52-
# Print the suggestions
53-
for index in top_3_suggestions:
54-
snippet, probability = model.predict_proba(new_code_vectorized)[0][index]
55-
print(f"{index + 1}: {snippet} ({probability * 100:.2f}%)")
56-
else:
57-
print("The code is fine.")
58-
41+
# Split the code into lines
42+
lines = code.split("\n")
43+
44+
# Predict the improvement suggestions for each line of code
45+
for line in lines:
46+
new_code_vectorized = vectorizer.transform([line])
47+
prediction = model.predict(new_code_vectorized)[0]
48+
49+
# Print the improvement suggestions
50+
if prediction == "optimize":
51+
print("The following are the suggested optimization snippets for line {}:".format(line))
52+
for snippet in model.predict_proba(new_code_vectorized)[0]:
53+
print(snippet)
54+
else:
55+
print("The code in line {} is fine.".format(line))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy