Skip to content

Commit 11473b4

Browse files
committed
drafts ideas
1 parent c2ec9e4 commit 11473b4

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import ast
2+
import json
3+
from pathlib import Path
4+
5+
"""
6+
def mark_for_review(message):
7+
return message
8+
9+
# Example error messages
10+
error_message = mark_for_review("Invalid input: '{input_value}'")
11+
not_found_message = mark_for_review("File '{filename}' was not found")
12+
"""
13+
14+
15+
def extract_unreviewed_messages(code_file, output_file, condition=None):
16+
"""
17+
Extracts messages from mark_for_review calls that match a condition on the version tag.
18+
19+
Parameters:
20+
code_file (str): Path to the Python source code file.
21+
output_file (str): Path to save the extracted messages as a JSON file.
22+
condition (callable, optional): A function to evaluate the version tag. If None, extracts messages with no version tag.
23+
"""
24+
with Path.open(code_file) as f:
25+
tree = ast.parse(f.read())
26+
27+
messages = {}
28+
29+
# Walk through the AST to find calls to `mark_for_review`
30+
for node in ast.walk(tree):
31+
if (
32+
isinstance(node, ast.Call)
33+
and getattr(node.func, "id", None) == "mark_for_review"
34+
):
35+
if len(node.args) >= 1 and isinstance(node.args[0], ast.Constant):
36+
original_message = node.args[0].value
37+
version = None
38+
39+
# Check if a second argument (version) exists
40+
if len(node.args) > 1 and isinstance(node.args[1], ast.Constant):
41+
version = node.args[1].value
42+
43+
# Apply the filter condition
44+
if condition is None or condition(version):
45+
key = f"{node.lineno}_{node.col_offset}" # Unique key based on location
46+
messages[key] = {"message": original_message, "version": version}
47+
48+
# Save extracted messages to a JSON file
49+
with Path.open(output_file, "w") as f:
50+
json.dump(messages, f, indent=4)
51+
52+
53+
# Example usage
54+
# Condition: Extract messages with no version or explicitly unreviewed (e.g., version is None or "unreviewed")
55+
def is_unreviewed(version):
56+
return version is None or version == "unreviewed"
57+
58+
59+
# extract_unreviewed_messages("your_script.py", "unreviewed_messages.json", condition=is_unreviewed)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import json
2+
import os
3+
from pathlib import Path
4+
5+
import openai
6+
7+
# Set your OpenAI API key
8+
openai.api_key = os.environ["OPENAPI_API_KEY"]
9+
10+
11+
def load_rules(rules_file):
12+
"""Load rules for message refinement from a JSON file."""
13+
with Path.open(rules_file) as f:
14+
return json.load(f)
15+
16+
17+
def load_messages(messages_file):
18+
"""Load messages from the messages.json file."""
19+
with Path.open(messages_file) as f:
20+
return json.load(f)
21+
22+
23+
def send_to_chatgpt(messages, rules):
24+
"""Send messages and rules to ChatGPT and request alternatives."""
25+
refined_messages = {}
26+
for key, details in messages.items():
27+
message = details["message"]
28+
version = details.get("version", "unknown")
29+
30+
# Prepare the prompt
31+
prompt = f"""
32+
You are a language expert. Apply the following rules to suggest alternatives for the given message:
33+
34+
Rules:
35+
- Tone: {rules['tone']}
36+
- Style: {rules['style']}
37+
- Length: {rules['length']}
38+
- Placeholders: {rules['placeholders']}
39+
40+
Original Message: "{message}"
41+
42+
Provide three alternatives based on the above rules. Ensure placeholders remain intact.
43+
"""
44+
45+
# Send the request to ChatGPT
46+
response = openai.Completion.create(
47+
engine="text-davinci-003", prompt=prompt, max_tokens=150, temperature=0.7
48+
)
49+
50+
# Parse the response
51+
alternatives = response.choices[0].text.strip()
52+
refined_messages[key] = {
53+
"original": message,
54+
"version": version,
55+
"alternatives": alternatives.split(
56+
"\n"
57+
), # Split into list if alternatives are on separate lines
58+
}
59+
60+
return refined_messages
61+
62+
63+
def save_refined_messages(output_file, refined_messages):
64+
"""Save the refined messages to a JSON file."""
65+
with Path.open(output_file, "w") as f:
66+
json.dump(refined_messages, f, indent=4)
67+
68+
69+
# Example usage
70+
# update_messages_in_code("your_script.py", "messages.json")
71+
72+
73+
# Main script
74+
if __name__ == "__main__":
75+
# File paths
76+
messages_file = "messages.json"
77+
rules_file = "rules.json"
78+
output_file = "refined_messages.json"
79+
80+
# Load inputs
81+
rules = load_rules(rules_file)
82+
messages = load_messages(messages_file)
83+
84+
# Process messages with ChatGPT
85+
refined_messages = send_to_chatgpt(messages, rules)
86+
87+
# Save the refined messages
88+
save_refined_messages(output_file, refined_messages)
89+
90+
print(f"Refined messages saved to {output_file}.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import ast
2+
import json
3+
from pathlib import Path
4+
5+
import astor
6+
7+
8+
def update_messages_in_code(code_file, messages_file):
9+
# Load corrected messages from the JSON file
10+
with Path.open(messages_file) as f:
11+
corrected_messages = json.load(f)
12+
13+
# Parse the Python code
14+
with Path.open(code_file) as f:
15+
tree = ast.parse(f.read())
16+
17+
# Walk through the AST to find calls to `mark_for_review`
18+
for node in ast.walk(tree):
19+
if (
20+
isinstance(node, ast.Call)
21+
and getattr(node.func, "id", None) == "mark_for_review"
22+
):
23+
# Get the original message
24+
if isinstance(node.args[0], ast.Constant):
25+
original_message = node.args[0].value
26+
27+
# Match it with corrected messages
28+
for key, corrected_message in corrected_messages.items():
29+
if original_message == corrected_message["original"]:
30+
# Replace the message with the corrected version
31+
node.args[0].value = corrected_message["current"]
32+
33+
# Add the version as the second argument
34+
version_node = ast.Constant(value=corrected_message["version"])
35+
if len(node.args) == 1:
36+
node.args.append(version_node)
37+
else:
38+
node.args[1] = version_node
39+
40+
# Write the updated code back to the file
41+
updated_code = astor.to_source(tree)
42+
with Path.open(code_file, "w") as f:
43+
f.write(updated_code)

0 commit comments

Comments
 (0)