solution
This commit is contained in:
parent
bc5869a233
commit
9eeb8e7eec
18970
dev-A/out.tsv
18970
dev-A/out.tsv
File diff suppressed because it is too large
Load Diff
61
format.ipynb
61
format.ipynb
@ -2,41 +2,50 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Data has been formatted and saved to output.txt\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import re\n",
|
||||
"\n",
|
||||
"# Open input file in read mode and output file in write mode\n",
|
||||
"with open('dev-A/out.tsv', 'r', encoding=\"utf-8\") as infile, open('dev-A/parsed.tsv', 'w', encoding=\"utf-8\") as outfile:\n",
|
||||
" \n",
|
||||
"with open('test-A/out.tsv', 'r', encoding=\"utf-8\") as infile, open('test-A/parsed.tsv', 'w', encoding=\"utf-8\") as outfile:\n",
|
||||
"\n",
|
||||
" # Default text to use when text segment is missing\n",
|
||||
" default_text = \"Airconditioner\"\n",
|
||||
"\n",
|
||||
" # Iterate through each line in the input file\n",
|
||||
" for line in infile:\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" # Regular expression to match \"{...}\" or \"}\"\n",
|
||||
" match = re.search(r'({.*}|\\})', line)\n",
|
||||
" \n",
|
||||
" match = re.search(r'({.*}|\\}[^{]*$)', line)\n",
|
||||
"\n",
|
||||
" # If \"{...}\" or \"}\" is found\n",
|
||||
" if match:\n",
|
||||
" # Extract \"{...}\" or \"}\" from the line\n",
|
||||
" data = match.group(0)\n",
|
||||
" \n",
|
||||
" data = match.group(0).strip()\n",
|
||||
"\n",
|
||||
" # Extract the text before \"{...}\" or \"}\"\n",
|
||||
" text = line[:match.start()].strip()\n",
|
||||
" \n",
|
||||
" # Split the text into segments\n",
|
||||
" segments = text.split()\n",
|
||||
" \n",
|
||||
" # If no segments, it means only data is present\n",
|
||||
" if not segments:\n",
|
||||
" outfile.write(data + '\\n')\n",
|
||||
" # If only one segment, duplicate it to conform to the format \"text\\ttext\\t{data}\"\n",
|
||||
" elif len(segments) == 1:\n",
|
||||
" outfile.write(segments[0] + '\\t' + segments[0] + '\\t' + data + '\\n')\n",
|
||||
" # If more than one segment, use them as is\n",
|
||||
" else:\n",
|
||||
" outfile.write('\\t'.join(segments) + '\\t' + data + '\\n')\n",
|
||||
"\n",
|
||||
" # If data is not enclosed within curly braces, enclose it\n",
|
||||
" if not data.startswith('{'):\n",
|
||||
" data = '{' + data\n",
|
||||
"\n",
|
||||
" # If text segment is missing, use default text\n",
|
||||
" if not text:\n",
|
||||
" text = default_text\n",
|
||||
"\n",
|
||||
" # Write to output file with the desired format\n",
|
||||
" outfile.write(f\"{text}\\t{text}\\t{data}\\n\")\n",
|
||||
" else:\n",
|
||||
" # If the line doesn't contain \"{...}\" or \"}\", write it as is\n",
|
||||
" outfile.write(line)\n",
|
||||
@ -53,7 +62,15 @@
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.10"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
|
10358
test-A/out.tsv
Normal file
10358
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
10358
test-A/raw-out.tsv
Normal file
10358
test-A/raw-out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user