import tkinter as tk from tkinter import * import csv import codecs import re import os from tkinter.filedialog import askopenfilename alphabets= "([A-Za-z])" prefixes = "(Mr|St|Mrs|Ms|Dr)[.]" suffixes = "(Inc|Ltd|Jr|Sr|Co)" starters = "((Mr|Mrs|Ms|Dr|A\s|I\s|He\s|She\s|It\s|If\s|Good\s|Being\s|They\s|Their\s|There\s|Our\s|We\s|But\s|Here\s|Another\s|Though\s|When\s|However\s|That\s|This\s|Wherever\s|The\s|You))" acronyms = "([A-Z][.][A-Z][.](?:[A-Z][.])?)" digits = "([0-9])" # to split by multile characters # regular expressions are easiest (and fastest) def splitParagraphIntoSentences(text): text = " " + text + " " text = text.replace("\n"," ") text = re.sub(prefixes,"\\1<prd>",text) #text = re.sub(websites,"<prd>\\1",text) if "Ph.D" in text: text = text.replace("Ph.D.","Ph<prd>D<prd>") text = re.sub("\s" + alphabets + "[.] "," \\1<prd> ",text) text = re.sub(acronyms+" "+starters,"\\1<stop> \\2",text) text = re.sub(alphabets + "[.]" + alphabets + "[.]" + alphabets + "[.]","\\1<prd>\\2<prd>\\3<prd>",text) text = re.sub(alphabets + "[.]" + alphabets + "[.]","\\1<prd>\\2<prd>",text) text = re.sub(" "+suffixes+"[.] "+starters," \\1<stop> \\2",text) text = re.sub(" "+suffixes+"[.]"," \\1<prd>",text) text = re.sub(" " + alphabets + "[.]"," \\1<prd>",text) text = re.sub(digits + "[.]" + digits,"\\1<prd>\\2",text) if "”" in text: text = text.replace(".”","”.") if "\"" in text: text = text.replace(".\"","\".") if "!" in text: text = text.replace("!\"","\"!") if "?" in text: text = text.replace("?\"","\"?") if "..." in text: text = text.replace("..."," <stop>") text = text.replace(".",".<stop>") text = text.replace("?","?<stop>") text = text.replace("!","!<stop>") text = text.replace("<prd>",".") sentences = text.split("<stop>") sentences = sentences[:-1] sentences = [s.strip() for s in sentences] return sentences # gui window title and dimensions window = tk.Tk() window.resizable(0,0) window.title("CSV to XML Converter Interface") window.geometry("700x650") y=[] # dropdown details var = tk.StringVar(window) var.set("SELECT") var2 = tk.StringVar(window) var2.set("SELECT") var3 = tk.StringVar(window) var3.set("SELECT") choices_ac=['Hygiene','Environment and view','Transportation','Activities','Amenities','Food','Location', 'Expenditure', 'Favourable season/day/time', 'Infrastructure', 'Staff', 'Artefacts','Cultural Richness','Wildlife Richness','Others'] choices=['positive', 'negative','neutral'] aspect_t=[] aspect_t_pol=[] aspect_c=[] # aspectterm Label at_label = tk.Label(text="Aspect Term:", font=('systemfixed',12,"bold")) at_label.place(x=50, y=450) # aspect polarity at_label2 = tk.Label(text="Polarity:", font=('systemfixed',12,"bold")) at_label2.place(x=220, y=450) # aspectterm Entry at = tk.Entry() at.place(x=50, y=485) # aspect polarity entry option = tk.OptionMenu(window, var, *choices) option.place(x=220, y=480) # aspectcateogary and its polarity Label ac_label = tk.Label(text="Aspect Cateogary:", font=('systemfixed',12,"bold")) ac_label.place(x=340, y=450) # aspectcateogary and it polarity Entry ac = tk.OptionMenu(window, var3, *choices_ac ) ac.place(x=340, y=480) sentence=[] tags=[] csvRows=[] y=[] z=[] global row_index global sent_index global rownum global csvData global index global xmlData global textdata global flag global data global r_index global s_index if os.path.isfile('sample.txt'): textdata=open('sample.txt','r') data=textdata.read() #print(data) dsplit=data.split(' ') #print('dsplit='+str(dsplit)) r_index=dsplit[0] s_index=dsplit[1] else: textdata=open('sample.txt', 'w+') textdata.write("0 0") r_index=0 s_index=0 flag=True index=0 row_index=0 sent_index=0 print('r_index='+str(r_index)+'s_index='+str(s_index)) def refresh(): at.delete(0,'end') var.set('SELECT') var3.set('SELECT') def start_f(): global xmlFile global textdata global row global tags global row_index global sent_index rownum=0 start.config(state=DISABLED) filename = tk.filedialog.askopenfilename() csvData = csv.reader(codecs.open(filename)) #print("i reached here 1") xmlFile=filename +'.xml' if (int(r_index)==0) and (int(s_index)==0): xmlData = open(xmlFile, 'w+') xmlData.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + "\n") xmlData.write('<review>') xmlData.write('<tripreview review_index="'+str(row_index)+'"'+'>'+'\n') #print("i reached here 2") else: row_index=int(r_index) sent_index=int(s_index) xmlData = open(xmlFile, 'a') #print("i reached here 3") for row in csvData: if rownum == 0: tags.extend(row) #print("i reached here 4") else: csvRows.append(row) rownum=rownum+1 for index in range(len(csvRows)): for i in range(len(tags)): if (i==5): sentence=splitParagraphIntoSentences(csvRows[index][i]) y.append(sentence) z.extend(y) y.clear() #print(len(z)) next(row_index,sent_index) next_review(row_index) submit_btn.config(state=DISABLED) def next_review(row_index): lab2 =Text(window) lab2.pack() lab2.insert(END,csvRows[row_index][5]) lab2.place(x=2,y=50) lab2.place(height=150,width=695) def next(row_index,sent_index): global textdata #xmlData = open(xmlFile, 'a') lab =Text(window) lab.pack() lab.insert(END,z[row_index][sent_index]) lab.place(x=2,y=290) lab.place(height=150,width=695) print("") global r r=0 def submit(flag): global row_index global sent_index global r global textdata textdata=open('sample.txt','w+') xmlData = open(xmlFile, 'a') if (flag): xmlData.write(' '+'</sentence>'+'\n') sent_index=sent_index+1 r=0 if(sent_index==len(z[row_index])): sent_index=0 row_index=row_index+1 next_review(row_index) if (row_index>=len(z)-1): xmlData.write(' '+'</tripreview>'+'\n') xmlData.write('</review>') xmlData.close() else: xmlData.write(' '+'</tripreview>'+'\n') xmlData.write(' '+'<tripreview review_index="'+str(row_index)+'"'+'>'+'\n') next(row_index,sent_index) textdata.write(str(row_index)+' '+str(sent_index)) else: if (r==0): if (((sent_index==0)and (row_index==0)) or (sent_index==0)): for i in range(len(tags)): print(len(tags)) if (i==5): continue else: xmlData.write(' ' + '<' + tags[i] + '>') xmlData.write(csvRows[row_index][i]+' ' + '</' + tags[i] + '>'+"\n") for i in range(len(tags)): if (i==5): xmlData.write(" "+'<sentence id="'+str(row_index)+'_'+str(sent_index)+'"'+'>'+z[row_index][sent_index]+"\n") xmlData.write(" "+'<aspectTerm'+' '+'term="'+str(at.get())+'"'+' '+'polarity="'+str(var.get())+'"'+' '+'aspectCategories="'+ str(var3.get())+'"' +'>' + "\n") xmlData.write(" "+'</aspectTerm>' + "\n") refresh() elif (len(z[row_index]) > sent_index): xmlData.write(" "+'<sentence id="'+str(row_index)+'_'+str(sent_index)+'"'+'>'+z[row_index][sent_index]+"\n") xmlData.write(" "+'<aspectTerm'+' '+'term="'+str(at.get())+'"'+' '+'polarity="'+str(var.get())+'"'+' '+'aspectCategories="'+ str(var3.get()) +'"'+'>' + "\n") xmlData.write(" "+'</aspectTerm>' + "\n") textdata.write(str(row_index)+' '+str(sent_index)) refresh() r=1 else: xmlData.write(" "+'<aspectTerm'+' '+'term="'+str(at.get())+'"'+' '+'polarity="'+str(var.get())+'"'+' '+'aspectCategories="'+ str(var3.get()) +'"'+'>' + "\n") xmlData.write(" "+'</aspectTerm>' + "\n") textdata.write(str(row_index)+' '+str(sent_index)) refresh() print('row_index'+str(row_index)) print('sent_index'+str(sent_index)) def start_f2(): global flag global textdata flag=True submit(flag) submit_btn.config(state=DISABLED) textdata=open('sample.txt','w+') textdata.write(str(row_index)+' '+str(sent_index)) def add_aspect(): print("in add_aspect") print(sent_index) global flag flag=False submit(flag) submit_btn.config(state=ACTIVE) # Add Aspect Term button add_at = tk.Button(text="Add", font=('systemfixed',12,"bold"), padx=5, pady=5, bg = "#000054", fg="#ffffff",command=add_aspect) add_at.place(x=530, y=465) #Next button submit_btn = tk.Button(text="Next", font=('systemfixed',12,"bold"), padx=5, pady=5, bg = "#000054", fg="#ffffff",command=start_f2) submit_btn.place(x=220, y=570) # Start Button start = tk.Button(text="Start", font=('systemfixed',12,"bold"), padx=5, pady=5, bg = "#000054", fg="#ffffff", command=start_f) start.place(x=50, y=570) window.mainloop()
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |