*! polishpaper.ado v2.1 - Generate a polishing template for academic papers
*! version 2.1  02May2026
*! authors: Wu Lianghai (AHUT), Wu Hanyan (NUAA) and Yang Lu (Rugao Finance Bureau)
*! Purpose: Generate a structured polishing template for academic papers
*! Update: v2.0 Add author() and date() options, improve bilingual support, fix line break error

program define polishpaper, rclass
    version 16.0
    syntax [, Language(string) Saving(string) Replace Author(string) Date(string)]

    * Set default language to English (support Chinese/中文/English/英文 and zh/en)
    if "`language'" == "" {
        local lang "English"
    }
    else {
        if inlist("`language'", "Chinese", "中文", "zh") {
            local lang "Chinese"
        }
        else if inlist("`language'", "English", "英文", "en") {
            local lang "English"
        }
        else {
            di as error "language() must be 'Chinese'/'中文'/'zh' or 'English'/'英文'/'en'"
            exit 198
        }
    }

    * Set default author (updated to include third author Yang Lu)
    if "`author'" == "" {
        local author "Prof. Wu Lianghai, Wu Hanyan & Yang Lu"
    }

    * Set default date
    if "`date'" == "" {
        local filedate "02May2026"
    }
    else {
        local filedate "`date'"
    }

    * Set default filename
    if "`saving'" == "" {
        local fname "polish_template.txt"
    }
    else {
        local fname `"`saving'"'
    }

    * Check if file already exists
    if "`replace'" == "" {
        capture confirm file "`fname'"
        if _rc == 0 {
            if "`lang'" == "Chinese" {
                di as error "错误：文件 `fname' 已存在！"
                di "请使用 replace 选项覆盖现有文件。"
            }
            else {
                di as error "Error: File `fname' already exists!"
                di "Please use the replace option to overwrite the existing file."
            }
            exit 602
        }
    }
    else {
        if "`lang'" == "Chinese" {
            di "注意：将覆盖已存在的文件 `fname'"
        }
        else {
            di "Note: Will overwrite the existing file `fname'"
        }
    }

    * Open file for writing
    tempname fh
    file open `fh' using `"`fname'"', write text `replace'

    * Write content via appropriate template
    if "`lang'" == "Chinese" {
        _zh_template, fh(`fh') author("`author'") date("`filedate'")
    }
    else {
        _en_template, fh(`fh') author("`author'") date("`filedate'")
    }

    * Close file
    file close `fh'

    * Display generation results
    di _n
    if "`lang'" == "Chinese" {
        di "已成功生成润色模板文件：`fname'"
        di "作者：`author'"
        di "日期：`filedate'"
        di "语言：中文"
    }
    else {
        di "Polishing template successfully generated: `fname'"
        di "Author: `author'"
        di "Date: `filedate'"
        di "Language: English"
    }

    * Display file content for easy copy-paste
    di _n
    if "`lang'" == "Chinese" {
        di "文件内容："
    }
    else {
        di "File content:"
    }
    type "`fname'"
end

* -------------------------------------------------------------------
* English template generator (direct file write)
* -------------------------------------------------------------------
program define _en_template
    syntax [, FH(string) AUTHOR(string) DATE(string)]

    file write `fh' "===== Academic Paper Polishing Template =====" _n
    file write `fh' "(Generated by polishpaper v2.1, `author', `date')" _n _n
    file write `fh' "Instructions: For each task below, follow the instruction and write/revise your content in the space provided." _n _n
    file write `fh' "--- Task 1: Improve Paragraph (Clarity & Conciseness) ---" _n
    file write `fh' "Instruction: Improve the clarity, conciseness and overall quality of the following paragraph. Ensure the language is precise, sentences well-structured, and flow logical." _n
    file write `fh' "Your paragraph:" _n
    file write `fh' "[Write your paragraph here]" _n
    file write `fh' "Revised version:" _n
    file write `fh' "[Write revised paragraph here]" _n _n
    file write `fh' "--- Task 2: Paraphrase Sentence (Avoid Plagiarism) ---" _n
    file write `fh' "Instruction: Paraphrase the following sentence to avoid plagiarism while retaining original meaning. Ensure grammar and academic vocabulary." _n
    file write `fh' "Original sentence:" _n
    file write `fh' "[Paste original sentence]" _n
    file write `fh' "Paraphrased sentence:" _n
    file write `fh' "[Write paraphrased sentence]" _n _n
    file write `fh' "--- Task 3: Suggest Title ---" _n
    file write `fh' "Instruction: Suggest a concise and informative title for a research paper based on the following findings. The title should accurately reflect the content and capture attention." _n
    file write `fh' "Your findings:" _n
    file write `fh' "[Describe your findings]" _n
    file write `fh' "Suggested title:" _n
    file write `fh' "[Write suggested title]" _n _n
    file write `fh' "--- Task 4: Generate Keywords ---" _n
    file write `fh' "Instruction: Generate a list of relevant keywords for a research paper on your topic. These keywords should facilitate discoverability and indexing in academic databases." _n
    file write `fh' "Your research topic:" _n
    file write `fh' "[Your research topic]" _n
    file write `fh' "Keywords:" _n
    file write `fh' "[List keywords]" _n _n
    file write `fh' "--- Task 5: Write Conclusion ---" _n
    file write `fh' "Instruction: Write a comprehensive conclusion summarizing key findings, limitations, and future research directions. Restate the research question and highlight contributions." _n
    file write `fh' "Your research topic/question:" _n
    file write `fh' "[Your research topic/question]" _n
    file write `fh' "Conclusion:" _n
    file write `fh' "[Write conclusion]" _n _n
    file write `fh' "--- Task 6: Discussion Outline ---" _n
    file write `fh' "Instruction: Create a well-structured outline for the discussion section, including key points, supporting evidence, and connections to existing literature." _n
    file write `fh' "Your research topic:" _n
    file write `fh' "[Your research topic]" _n
    file write `fh' "Discussion outline:" _n
    file write `fh' "[Provide outline]" _n _n
    file write `fh' "--- Task 7: Argue Significance ---" _n
    file write `fh' "Instruction: Develop a compelling argument for the significance of your research, highlighting its potential impact on the field and contribution to knowledge." _n
    file write `fh' "Your research topic:" _n
    file write `fh' "[Your research topic]" _n
    file write `fh' "Significance argument:" _n
    file write `fh' "[Write argument]" _n _n
    file write `fh' "===== End of Template =====" _n
end

* -------------------------------------------------------------------
* Chinese template generator (direct file write)
* -------------------------------------------------------------------
program define _zh_template
    syntax [, FH(string) AUTHOR(string) DATE(string)]

    file write `fh' "===== 学术论文润色模板 =====" _n
    file write `fh' "(由 polishpaper v2.1 生成，作者：`author'，日期：`date')" _n _n
    file write `fh' "使用说明：对于下面的每个任务，按照指示在提供的空白处撰写/修改您的内容。" _n _n
    file write `fh' "--- 任务1：改进段落（润色优化）---" _n
    file write `fh' "指示：提高段落的清晰度、简洁性和整体质量。确保语言准确，句式优良，逻辑通顺。" _n
    file write `fh' "您的段落：" _n
    file write `fh' "[在此处粘贴您的段落]" _n
    file write `fh' "润色后版本：" _n
    file write `fh' "[在此处写润色后段落]" _n _n
    file write `fh' "--- 任务2：改写句子（降重/去剽窃）---" _n
    file write `fh' "指示：改写句子以避免剽窃，同时保留原意。确保语法正确并使用恰当的学术词汇。" _n
    file write `fh' "原始句子：" _n
    file write `fh' "[在此处粘贴原始句子]" _n
    file write `fh' "改写后句子：" _n
    file write `fh' "[在此处写改写后句子]" _n _n
    file write `fh' "--- 任务3：建议标题（拟定题目）---" _n
    file write `fh' "指示：根据研究发现建议一个简洁明了的论文标题。标题应准确反映内容并吸引读者。" _n
    file write `fh' "您的研究发现：" _n
    file write `fh' "[描述您的研究发现]" _n
    file write `fh' "建议标题：" _n
    file write `fh' "[在此处写建议标题]" _n _n
    file write `fh' "--- 任务4：生成关键词（索引优化）---" _n
    file write `fh' "指示：生成相关关键词列表。这些关键词应有助于在学术数据库中被发现和索引。" _n
    file write `fh' "您的研究主题：" _n
    file write `fh' "[您的研究主题]" _n
    file write `fh' "关键词：" _n
    file write `fh' "[列出关键词]" _n _n
    file write `fh' "--- 任务5：撰写结论（总结/展望）---" _n
    file write `fh' "指示：撰写全面的结论，总结主要发现、局限性和未来方向。重申研究问题并强调研究贡献。" _n
    file write `fh' "您的研究主题/问题：" _n
    file write `fh' "[您的研究主题/问题]" _n
    file write `fh' "结论：" _n
    file write `fh' "[在此处写结论]" _n _n
    file write `fh' "--- 任务6：讨论大纲（结构梳理）---" _n
    file write `fh' "指示：为讨论部分创建结构良好的大纲，包含要点、支持证据及与现有文献的联系。" _n
    file write `fh' "您的研究主题：" _n
    file write `fh' "[您的研究主题]" _n
    file write `fh' "讨论大纲：" _n
    file write `fh' "[在此处提供大纲]" _n _n
    file write `fh' "--- 任务7：论证意义（价值升华）---" _n
    file write `fh' "指示：有力论证研究的重要性，强调其对该领域的潜在影响及对知识库的贡献。" _n
    file write `fh' "您的研究主题：" _n
    file write `fh' "[您的研究主题]" _n
    file write `fh' "意义论证：" _n
    file write `fh' "[在此处写论证]" _n _n
    file write `fh' "===== 模板结束 =====" _n
end