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
49
format.ipynb
49
format.ipynb
@ -2,41 +2,50 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 4,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Data has been formatted and saved to output.txt\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"import re\n",
|
"import re\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Open input file in read mode and output file in write mode\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",
|
"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",
|
"\n",
|
||||||
" # Iterate through each line in the input file\n",
|
" # Iterate through each line in the input file\n",
|
||||||
" for line in infile:\n",
|
" for line in infile:\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Regular expression to match \"{...}\" or \"}\"\n",
|
" # Regular expression to match \"{...}\" or \"}\"\n",
|
||||||
" match = re.search(r'({.*}|\\})', line)\n",
|
" match = re.search(r'({.*}|\\}[^{]*$)', line)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # If \"{...}\" or \"}\" is found\n",
|
" # If \"{...}\" or \"}\" is found\n",
|
||||||
" if match:\n",
|
" if match:\n",
|
||||||
" # Extract \"{...}\" or \"}\" from the line\n",
|
" # Extract \"{...}\" or \"}\" from the line\n",
|
||||||
" data = match.group(0)\n",
|
" data = match.group(0).strip()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Extract the text before \"{...}\" or \"}\"\n",
|
" # Extract the text before \"{...}\" or \"}\"\n",
|
||||||
" text = line[:match.start()].strip()\n",
|
" text = line[:match.start()].strip()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Split the text into segments\n",
|
" # If data is not enclosed within curly braces, enclose it\n",
|
||||||
" segments = text.split()\n",
|
" if not data.startswith('{'):\n",
|
||||||
|
" data = '{' + data\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # If no segments, it means only data is present\n",
|
" # If text segment is missing, use default text\n",
|
||||||
" if not segments:\n",
|
" if not text:\n",
|
||||||
" outfile.write(data + '\\n')\n",
|
" text = default_text\n",
|
||||||
" # If only one segment, duplicate it to conform to the format \"text\\ttext\\t{data}\"\n",
|
"\n",
|
||||||
" elif len(segments) == 1:\n",
|
" # Write to output file with the desired format\n",
|
||||||
" outfile.write(segments[0] + '\\t' + segments[0] + '\\t' + data + '\\n')\n",
|
" outfile.write(f\"{text}\\t{text}\\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",
|
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" # If the line doesn't contain \"{...}\" or \"}\", write it as is\n",
|
" # If the line doesn't contain \"{...}\" or \"}\", write it as is\n",
|
||||||
" outfile.write(line)\n",
|
" outfile.write(line)\n",
|
||||||
@ -53,7 +62,15 @@
|
|||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.10"
|
"version": "3.10.10"
|
||||||
},
|
},
|
||||||
"orig_nbformat": 4
|
"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