Skip to content

Fix main.py to correctly build JSON from english/german files#411

Open
kunghun-jeong wants to merge 1 commit intoagwaBom:mainfrom
kunghun-jeong:fix-main
Open

Fix main.py to correctly build JSON from english/german files#411
kunghun-jeong wants to merge 1 commit intoagwaBom:mainfrom
kunghun-jeong:fix-main

Conversation

@kunghun-jeong
Copy link
Copy Markdown

a) What line of code you changed

  • path_to_file_list (around line 5–10):

    • Changed open(path, 'w') and return lines
      to reading the file in 'r' mode and returning
      a list of lines without newline characters.
  • train_file_list_to_json (around line 15–30):

    • Removed manual string concatenation with template_start/mid/end
      and now use json.dumps({"English": en, "German": de}, ensure_ascii=False)
      to generate valid JSON strings.
  • write_file_list (around line 30–40):

    • Changed opening the file in 'r' mode and writing only '\n'
      to opening in 'w' mode and writing each element of file_list
      followed by '\n'.
  • main (around line 40+):

    • Fixed function calls so that english.txt / german.txt are first
      read by path_to_file_list, and then passed into
      train_file_list_to_json, and the result is written
      to concated.json using write_file_list.

b) Why it is a wrong code

  • path_to_file_list opened the file in write mode and never read it,
    returning an undefined variable lines, so it could not work as
    a function that “reads a file and returns a list of lines”.

  • train_file_list_to_json did not produce valid JSON format and also
    used only "German" as a key in the template, which does not match
    the required {"English": "...", "German": "..."} structure.

  • write_file_list opened the output file in read mode and wrote only
    newline characters, so the result file did not contain any actual data.

  • In main, the functions were called with wrong arguments
    (e.g., passing a path string into train_file_list_to_json) so the
    script could not correctly read input files and build concated.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant