def get_artist_and_title(argv): if len(argv) == 3: artist = argv[1] title = argv[2] else: artist = raw_input("Artist name contains:\n") title = raw_input("Track title contains:\n") return artist, title
def upload(self): print('Upload Log file to KnowledgeBasic Server') print('Please Print File Name') self.file = raw_input() print('Please Print Directory Path') self.uploadDir = raw_input() self.uploadFile= self.uploadFile(self.file, self.uploadDir) self.form = cgi.FieldStorage() if not self.form.has_key(self.file): return self.fileitem = self.form[self.file] if not self.fileitem.file: return self.outpath = os.path.join(self.uploadDir, self.fileitem.filename) with open(self.outpath, 'wb') as fout: shutil.copyfileobj(self.fileitem.file, fout, 100000)
def kb0(self): print('Check File against KnowlegeBasic') print('Please Input Filename') self.file = raw_input() self.log = open(self.file, 'r') self.logRaw = self.log.read() self.logs = self.logRaw.splitlines() self.KB0 = [ 'xlicense: ERROR! ERR_STATE_ERR(-18)Confd not running', 'xlicense: ERROR! ERR_SYS_ERR (-5)Error calling start_or_wait_for_confd((null))', 'xlicense: ERROR! ERR_CONFD_ERR(-300)Could not commit changes!' ] self.countKB0 = set(self.KB0) & set(self.logs) self.percentKB0 = float(len(self.countKB0)) / float(len(self.KB0)) self.percentInt = int(self.percentKB0 * 100) self.prep = repr(self.countKB0) self.results = [self.prep] print('the messages found in common:') print(self.countKB0) print('the likely match to KnowledgeBasic0 is:') print(self.percentInt) print('Creating Result Files...') for item in self.countKB0: resultsString = open('results.txt', 'w') resultsString.write(self.prep) resultsFile = open('results.csv', 'w') wr = csv.writer(resultsFile, delimiter=' ', lineterminator='\n') wr.writerows(self.results)
def upload(self): print('Upload Log file to KnowledgeBasic Server') print('Please Print File Name') self.file = raw_input() print('Please Print Directory Path') self.uploadDir = raw_input() self.uploadFile = self.uploadFile(self.file, self.uploadDir) self.form = cgi.FieldStorage() if not self.form.has_key(self.file): return self.fileitem = self.form[self.file] if not self.fileitem.file: return self.outpath = os.path.join(self.uploadDir, self.fileitem.filename) with open(self.outpath, 'wb') as fout: shutil.copyfileobj(self.fileitem.file, fout, 100000)
def converseTrackwork(self): input = "" global lineName while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() elif input == "help": print( "Please enter the name of the line you are travelling on, in the form 'NAME line'" ) print("On what line are you planning to travel on?") self.converseTrackwork() tokenInput = word_tokenize(input, language='english') for x, y in enumerate(tokenInput): if x < len(tokenInput) - 1: if (y + " " + tokenInput[x + 1] ).lower() in lineList: #search 2 tokens at once lineName = (y + " " + tokenInput[x + 1]).upper() self.converseTrackworkDay() break print( "Hmmm sorry, I don't understand. On what line are you planning to travel on?" ) else: print( "I didn't catch that. On what line are you planning to travel on?" )
def kb0(self): print('Check File against KnowlegeBasic') print('Please Input Filename') self.file = raw_input() self.log = open(self.file, 'r') self.logRaw = self.log.read() self.logs = self.logRaw.splitlines() self.KB0 = ['xlicense: ERROR! ERR_STATE_ERR(-18)Confd not running', 'xlicense: ERROR! ERR_SYS_ERR (-5)Error calling start_or_wait_for_confd((null))', 'xlicense: ERROR! ERR_CONFD_ERR(-300)Could not commit changes!'] self.countKB0 = set(self.KB0) & set(self.logs) self.percentKB0 = float(len(self.countKB0)) / float(len(self.KB0)) self.percentInt = int(self.percentKB0 * 100) self.prep = repr(self.countKB0) self.results = [self.prep] print('the messages found in common:') print(self.countKB0) print('the likely match to KnowledgeBasic0 is:') print(self.percentInt) print('Creating Result Files...') for item in self.countKB0: resultsString = open('results.txt', 'w') resultsString.write(self.prep) resultsFile = open('results.csv', 'w') wr = csv.writer(resultsFile, delimiter=' ', lineterminator='\n') wr.writerows(self.results)
def processWatch(self): print('Check File for Process Death') print('Please Input Filename') file = raw_input('') self.log = open(file, 'r') self.logRaw = self.log.read() self.logs = self.logRaw.splitlines() self.processWatch = [ 'Stopping xhad...', 'Stopping xactiond', 'Stopping xconfigd...' ] self.countProcessWatch = set(self.processWatch) & set(self.logs) self.percentProcessWatch = float(len(self.countProcessWatch)) / float( len(self.processWatch)) self.percentInt = int(self.percentProcessWatch * 100) self.prep = repr(self.countProcessWatch) self.results = [self.prep] print('the messages found in common:') print(self.countProcessWatch) print('the likely match to KnowledgeBasic0 is:') print(self.percentInt) print('Creating Result Files...') for item in self.countProcessWatch: resultsString = open('results.txt', 'w') resultsString.write(self.prep) resultsFile = open('results.csv', 'w') wr = csv.writer(resultsFile, delimiter=' ', lineterminator='\n') wr.writerows(self.results)
def inputWSpellCheck(inputstring): correctSpelledString="" rawString="" if inputstring: tokens = word_tokenize(inputstring) for n in tokens: correctSpelledString = correctSpelledString + correction(n) +" " rawString=rawString+n+" " if rawString != correctSpelledString: print("Did you mean \"" + correctSpelledString+"\" ? Type \'yes\' or \'no\'") while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: tokeners=word_tokenize(input) if input.lower()=='quit': self.quitChat() elif input.lower()=="help": print("Simply type yes or no") inputWSpellCheck(inputstring) elif input.lower()=="yes" or input.lower()=="y" or input.lower()=="yep" or input.lower()=="yea" or input.lower()=="yeah": return correctSpelledString elif input.lower()=='no' or input.lower()=='nah' or input.lower()=='nope' or input.lower()=='na': return rawString for j in tokeners: if j.lower()=="yes" or j.lower()=="y" or j.lower()=="yep" or j.lower()=="yea" or j.lower()=="yeah": return correctSpelledString elif j.lower()=='no' or j.lower()=='nah' or j.lower()=='nope' or j.lower()=='na': return rawString print("Sorry, I didn't get that. Please type yes or no") else: return""
def converseTimetableFrom(self): input = "" global fromStation while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() elif input == "help": print( "Please enter a valid station name (case insensitive)") print("Which station would you like to travel from?") elif checkStations(input): fromStation = checkStations(input) print("Okay, " + fromStation + ", and where to?") self.converseTimetableTo() break else: print("Hmmm sorry, I don't know of a station called " + input + "\n") print("Which station would you like to leave from?") else: print("Sorry, I didn't catch that. Please try again \n") print("Which station would you like to leave from?")
def converseTimetableTo(self): input = "" global fromStation, toStation while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() elif input == "help": print( "Please enter a valid station name (case insensitive)") print("Which station would you like to travel to?") elif checkStations(input) and checkStations( input ) != fromStation: #validate the names and make sure it's not the same station toStation = checkStations(input) print( "Okay, to " + toStation + ", and do you want to travel on a weekday or on a weekend?" ) self.converseTimetableDay() break elif checkStations(input) == fromStation: print("You're already at " + fromStation + ", please pick a different station.\n") print("Which station would you like to travel to?") else: print("Sorry, I don't understand\n") print("Which station would you like to travel to?") else: print("Sorry, I didn't catch that. Please try again \n") print("Which station would you like to travel to?")
def converse(self, quit="quit"): input = "" while input != quit: input = quit try: input = compat.raw_input(">") except EOFError: print(input) if input: while input[-1] in "!.": input = input[:-1] print(self.respond(input))
def prompt(self): print('Menu: u = Upload a = Analyze d = Define') self.option = raw_input() while self.option != 'q': if self.option == 'a': self.kb0() print('Completed... Heading back to menu') elif self.option == 'u': self.upload() elif self.option == 'd': self.define()
def converseTrackworkDay(self): weekDays = ["monday", "tuesday", "wednesday", "thursday", "friday"] weekEnds = ["saturday", "sunday"] input = "" print("On what day will you be travelling?") global lineName, trackDate while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() elif input == "help": print( "Enter the day you want to leave by specifying the day of week, date in nth or n format, or say 'next' in your statement for next week's dates" ) print("On what day will you be travelling?") self.converseTrackworkDay() nextTrigger = False realDate = None inputTokens = word_tokenize(input, language='english') for x in inputTokens: if x.lower() == "next": nextTrigger = True if x.lower() in weekDays or x.lower() in weekEnds: realDate = getDateTime(x.lower(), nextTrigger) elif x.lower() in calNums.keys(): realDate = getDateTime(x.lower(), nextTrigger) elif x.lower()[:2].isdigit() and x.lower()[2:] in ( "th", "nd", "st", "rd"): realDate = getDateTime(x.lower()[:2], nextTrigger) elif x.lower()[:1].isdigit() and x.lower()[1:] in ( "th", "nd", "st", "rd"): realDate = getDateTime(x.lower()[:1], nextTrigger) elif x.lower() == "today": realDate = datetime.datetime.strptime( "2017-09-16", "%Y-%m-%d") elif x.lower() == "tomorrow": realDate = datetime.datetime.strptime( "2017-09-16", "%Y-%m-%d") + datetime.timedelta(1) if realDate: trackDate = realDate self.converseTrackworkTime() break else: print("Sorry, I don't understand\n") print("On what day will you be travelling?") else: print("Sorry, I don't understand\n") print("On what day will you be travelling?")
def Apriori_1_kminus1(self): #First, ask the user to input support and confidence support=raw_input("Enter the support for the dataset:"); confidence=raw_input("Enter the confidence for the dataset:"); lift=raw_input("Enter the lift measure for the dataset:") check=raw_input("Would you like to use lift to generate the rules- enter Y or N, N implies confidence") number_of_attributes=raw_input("Enter the number of attributes for this dataset:"); for i in range(0,int(number_of_attributes)): label=raw_input("Enter the attribute label:"); Apriori.attribute_names.append(label) Apriori.attribute_column[label]=i for key,val in Apriori.attribute_column.items(): print("column name",key,"column no",val) #reading the Tic Tac Toe file into numpy array DataMatrix=np.loadtxt("C:/Users/dell/PycharmProjects/Homework4/Output_Contraceptive.txt",delimiter=',') (row,column)=DataMatrix.shape #print(row,column) #reading each column one by one for i in range(0,column): column=DataMatrix[:,i] Apriori().calculate_candidate_one_itemsets(column,row,Apriori.attribute_names[i]); #for key,val in Apriori.candidate_one_itemsets.items(): # print(key,val); Apriori.calculate_candidate_one_support(self,row,support) Apriori.calculate_candidate_two_support(self,row,support,DataMatrix) m=number_of_attributes for i in range(3,m+1): val=Apriori.calculate_candidate_k(self,row,support,DataMatrix,i) if val == 0: break print("done with itemset generation") #for k,itemset in Apriori.candidate_master_dict.items(): # # print("finally") #print(k,itemset) print("counting maximal itemsets") Apriori.count_maximal(self,Apriori.candidate_master_dict_before_pruning) Apriori.count_closed_frequent(self,Apriori.candidate_master_dict_before_pruning) for key,itemset_dict in Apriori.candidate_master_dict.items(): if key != 1: if check == 'N': Apriori.generate_rules(self,itemset_dict,DataMatrix,confidence) else: Apriori.generate_rules_lift(self,itemset_dict,DataMatrix,lift)
def words2filter(): #打开文件读取敏感词汇 with open("./../../words/words.txt", "r") as file: words = file.read().split() print(words) user_input = raw_input("Please input a word:") for word in words: if word in user_input: user_input = user_input.replace(str(word), "*" * len(word)) print(user_input) return
def start_apriori(self): #First, ask the user to input support and confidence support=raw_input("Enter the support for the dataset:"); #confidence=raw_input("Enter the confidence for the dataset:"); number_of_attributes=raw_input("Enter the number of attributes for this dataset:"); for i in range(0,int(number_of_attributes)): label=raw_input("Enter the attribute label:"); AprioriSolution.attribute_names.append(label) AprioriSolution.attribute_column[label]=i for key,val in AprioriSolution.attribute_column.items(): print("column name",key,"column no",val) #reading the file into numpy array DataMatrix=np.loadtxt("C:/Users/dell/PycharmProjects/Homework4/Output_Contraceptive.txt",delimiter=',') (row,column)=DataMatrix.shape #reading each column one by one for i in range(0,9): column=DataMatrix[:,i] AprioriSolution().calculate_candidate_one_itemsets(column,row,AprioriSolution.attribute_names[i]); for key,val in AprioriSolution.candidate_one_itemsets.items(): print(key,val); AprioriSolution.calculate_candidate_one_support(self,row,support) AprioriSolution.calculate_candidate_two_support(self,row,support,DataMatrix) m=number_of_attributes for i in range(3,int(m)+1): val=AprioriSolution.calculate_candidate_k_1(self,row,support,DataMatrix,i) if val == 0: break for k,frequent_item in AprioriSolution.candidate_master_dict.items(): print("k",k,"itemset frequent",frequent_item)
def filterWords(): with open("./../../words/words.txt") as f: words = f.read().split() print(words) user_input = raw_input("Please input a word:") #标识是否为敏感词汇 flag = True for word in words: if word in user_input: flag = True break else: flag = False if flag: print("Freedom") else: print("Human Rights") return
def converseTrackworkTime(self): input = "" validTime = re.compile(r'^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])') print("At what time?") global trackTime while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() elif input == "help": print( "Enter a time in the format of HH:MM (24 hour time), as a digit in range of 1-12, or the written word in range one-twelve" ) print("What time do you want to leave?") self.converseTrackworkTime() inputTokens = word_tokenize(input, language='english') for p in inputTokens: if validTime.search(p): trackTime = p self.trackQuery() break elif (p.isdigit() and int(p) in range( 1, 12)): #validate whole hour time as ints trackTime = p + ":00" self.trackQuery() break elif p.lower() in clockNums.keys( ): #validate written whole hour times as strings trackTime = str(clockNums.get(p.lower())) + ":00" self.trackQuery() break else: print("Sorry, I don't understand\n") print("At what time will you leave?") else: print("Sorry, I don't understand\n") print("What time do you want to leave?")
def converseMain(self): input = "" timetable = re.compile( r"(.*)timetable(.*)" ) #use regex to show i know it but prefer another way trackwork = re.compile(r"(.*)trackwork(.*)") while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() while input[-1] in "!.": input = input[:-1] print(self.respond( input)) #taken from nltk.chat.util to utilise reflections if trackwork.search(input): self.converseTrackwork() break if timetable.search(input): self.converseTimetableFrom() break
def processWatch(self): print('Check File for Process Death') print('Please Input Filename') file = raw_input('') self.log = open(file, 'r') self.logRaw = self.log.read() self.logs = self.logRaw.splitlines() self.processWatch = ['Stopping xhad...', 'Stopping xactiond', 'Stopping xconfigd...'] self.countProcessWatch = set(self.processWatch) & set(self.logs) self.percentProcessWatch = float(len(self.countProcessWatch)) / float(len(self.processWatch)) self.percentInt = int(self.percentProcessWatch * 100) self.prep = repr(self.countProcessWatch) self.results = [self.prep] print('the messages found in common:') print(self.countProcessWatch) print('the likely match to KnowledgeBasic0 is:') print(self.percentInt) print('Creating Result Files...') for item in self.countProcessWatch: resultsString = open('results.txt', 'w') resultsString.write(self.prep) resultsFile = open('results.csv', 'w') wr = csv.writer(resultsFile, delimiter=' ', lineterminator='\n') wr.writerows(self.results)
#! /usr/bin/env python from nltk import * import nltk import string from nltk.compat import raw_input print("Enter your question ") question = raw_input() question = question.lower() stopwords = nltk.corpus.stopwords.words('english') content = nltk.word_tokenize(question) keywords_analysis = list( set(content) - set(stopwords) ) #print keywords_analysis # this code can also include the following # stemming # stop-word removal # lemmatization (necessary for questions that have more than one line)
def converseMain(self): input = "" global fromStation, toStation, dayTrip, dayTime, lineName, queryType, nextTrigger lastIndex=-1 validTime = re.compile(r'^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])') while 1: try: rawInput = compat.raw_input(">") except EOFError: print(rawInput) input=inputWSpellCheck(rawInput) if input: if input.lower()=="quit": self.quitChat() elif input.lower()=="help": print("Let us know how we can help you, by entering the information for timetables or trackwork") self.converseMain() #all help prompts run back to main inputTokens = word_tokenize(input,language='english') for x,y in enumerate(inputTokens): #CHECK STATION NAMES FROM/TO BASED ON LAST INDICES if checkStations(y.lower()) and x>0 and inputTokens[x-1].lower()=="from" and not fromStation: #print('got here') lastIndex = x fromStation = checkStations(y.lower()) elif checkStations(y.lower()) and x>0 and inputTokens[x-1].lower()=="to" and not fromStation: lastIndex = x fromStation = checkStations(y.lower()) elif checkStations(y.lower()) and x>lastIndex and not toStation: lastIndex = x toStation = checkStations(y.lower()) elif checkStations(y.lower()) and not fromStation: lastIndex = x fromStation = checkStations(y.lower()) #CHECK LINE NAMES elif x<len(inputTokens)-1: if (y + " " + inputTokens[x+1]).lower() in lineList: #search 2 tokens at once lineName = (y + " " + inputTokens[x+1]).upper() #CHECKING DATES elif y.lower() == "next": nextTrigger = True elif y.lower() in weekDays or y.lower() in weekEnds: realDate = getDateTime(y.lower(), nextTrigger) dayTrip = realDate elif y.lower() in calNums.keys(): realDate = getDateTime(y.lower(), nextTrigger) dayTrip = realDate elif y.lower()[:2].isdigit() and y.lower()[2:] in ("th","nd","st","rd"): realDate = getDateTime(y.lower()[:2], nextTrigger) dayTrip = realDate elif y.lower()[:1].isdigit() and y.lower()[1:] in ("th","nd","st","rd"): realDate = getDateTime(y.lower()[:1], nextTrigger) dayTrip = realDate elif y.lower() == "today": realDate = datetime.datetime.strptime("2017-09-16","%Y-%m-%d") dayTrip = realDate elif y.lower() == "tomorrow": realDate = datetime.datetime.strptime("2017-09-16","%Y-%m-%d") + datetime.timedelta(1) dayTrip = realDate #CHECKING TIMES elif validTime.search(y): dayTime = y elif (y.isdigit() and int(y) in range(1,12)): #validate whole hour time as ints dayTime = y +":00" elif y.lower() in clockNums.keys(): #validate written whole hour times as strings dayTime = str(clockNums.get(y.lower())) + ":00" #QUERY TYPE DEFINED elif y.lower() =="trackwork": queryType='trackwork' elif y.lower() =="timetable": queryType='timetable' #QUERY TYPE INFERRED if (fromStation or toStation) and not queryType: queryType = 'timetable' elif lineName and not queryType: queryType = 'trackwork' else: print("Sorry, I still don't understand what you're after") self.converseMain() #RESPONSE TO INPUT if queryType == 'timetable': print("Ok, so you are travelling {frSt}{toSt}{daTi}{daDa}".format( frSt=("from " +fromStation+" ") if fromStation else "", toSt=("to " +toStation+" ") if toStation else "", daTi=("at " +dayTime+" ") if dayTime else "", daDa=("on " +dayTrip.strftime("%A %d %B")+" ") if dayTrip else "" )) stillNeeded=[] for key,value in {'where you\'re coming from':fromStation, 'where you\'re going to':toStation, 'what time you\'re leaving' :dayTime, 'what day you\'re leaving':dayTrip}.items(): if value== None: stillNeeded = stillNeeded + [key] if stillNeeded: print("We still need ",end='') for z in stillNeeded: print(", " + z,end='') print("\n") if not stillNeeded: self.sqlBuilders() else: self.converseMain() elif queryType == 'trackwork': print("Ok, so you are travelling {liNa}{daTi}{daDa}".format( liNa=("on the " +lineName+" ") if lineName else "", daTi=("at " +dayTime+" ") if dayTime else "", daDa=("on " +dayTrip.strftime("%A %d %B")+" ") if dayTrip else "" )) stillNeeded=[] for key,value in {'lineName':lineName, 'what time you\'re leaving' :dayTime, 'what day you\'re leaving':dayTrip}.items(): if value== None: stillNeeded = stillNeeded + [key] if stillNeeded: print("We still need ",end='') for z in stillNeeded: print(", " + z,end='') print("\n") if not stillNeeded: self.sqlBuilders() else: self.converseMain()
def euclideanDistance(self): k=raw_input("Enter the k for the k-NN algorithm:") master_dict={} assigned_label=[] calculated_label=[] data_set=KNN().randomSampling() print("Read the dataset after random sampling",len(data_set)) assigned_label=KNN().getClassLabel(data_set) extracted_data=open_file(data_set) data_set_whole=[] columns=[0,1,2,3,4,5,11,22,23,32,36,40,41] for each in extracted_data: list=[] for column in columns: list.append(each[column]) data_set_whole.append(list) print("Read the dataset after enumeration",len(data_set)) count=0 for each in data_set: count+=1 print("Examining",count) master_dict={} for other in data_set: if each != other: distance=sp.euclidean(each,other) master_dict[distance]=other distance_list=[] #get the one with the least distance for key in master_dict.keys(): distance_list.append(key) #getting 1 nearest neighbour if int(k) == 1: min_distance=min(distance_list) neighbour=master_dict.get(min_distance) m=0 #get the index of the class label of the nearest neighbour for i in range(0,len(data_set)): if neighbour == data_set[i]: m=i break; #get the class label from the class label list class_label=assigned_label[i] calculated_label.append(class_label) else: #getting k nearest neighbour k_min_dist=[] for vary in range(0,int(k)): if len(distance_list) != 0: minimum=min(distance_list) k_min_dist.append(minimum) distance_list.remove(minimum) #print("k min dist",k_min_dist) match_list=[] index_list=[] for each in k_min_dist: vector=master_dict.get(each) match_list.append(vector) for i in range(0,len(match_list)): for j in range(0,len(data_set)): if data_set[j] == match_list[i]: index_list.append(j) class_labels=[] for index in index_list: class_labels.append(assigned_label[index]) count_new=Counter(class_labels) label=count_new.most_common()[0] calculated_label.append(label[0]) #print("calculated_label",calculated_label) #print("assigned label",assigned_label) #for i in range(0,len(class_name_list)): print("Labels calculated") #count=0 #for i in range(0,len(calculated_label)): #if calculated_label[i] == assigned_label[i]: #count+=1 #print("accuracy count",count) KNN().calcAccuracy(assigned_label,calculated_label)
def trainQuery(self): input = "" global fromStation, toStation, dayWeek, uTime #utilise global variables to keep track of details queryList = [] for row in conn.execute( sqlBuildTimetable(fromStation, toStation, uTime, dayWeek)): queryList = queryList + [row] print("Let me see - I have a train leaving " + fromStation + " at " + queryList[0][1] + " and arriving at " + toStation + " at " + queryList[0][3]) firstChoice = queryList[0] #save the original closest train queryList.sort( key=lambda tup: time.strptime(tup[1], "%H:%M") ) #perform an in-place sort for later iteration based on time for n, y in enumerate(queryList): if y == firstChoice: index = n #keep track of the index position for smooth iteration if index == 0: print("Would you like a later train?") elif index == len(queryList) - 1: print('Would you like an earlier train?') else: print("Would you like an earlier or later train?") while 1: #try/catch block in an infinite loop to ensure smooth, repeatable input try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() elif input == "help": print( "Type 'earlier' to see earlier trains, or 'later' to see later trains. If you are happy with your selection, type 'no thanks'" ) self.trainQuery() inputTokens = word_tokenize( input, language='english') #tokenize to check patterns for x in inputTokens: if x.lower( ) == "no": #always .lower() for matching purpose self.quitChat() break elif x.lower() == "early" or x.lower() == "earlier": if index == 0: print( "Sorry, no earlier trains found. Would you like a later train?" ) else: index -= 1 print("Let me check - I have a train leaving " + fromStation + " at " + queryList[index][1] + " and arriving at " + toStation + " at " + queryList[index][3]) if index == 0: #check if earlier/later trains even exist print("Would you like a later train?") else: print( "Would you like an earlier or later train?" ) elif x.lower() == "late" or x.lower() == "later": if index == len(queryList) - 1: print( "Sorry, no later trains found. Would you like an earlier train?" ) else: index += 1 print("Let me check - I have a train leaving " + fromStation + " at " + queryList[index][1] + " and arriving at " + toStation + " at " + queryList[index][3]) if index == len(queryList) - 1: print("Would you like an earlier train?") else: print( "Would you like an earlier or later train?" ) else: print("Sorry, I didn't understand\n") print("Would you like an earlier or later train?")
def sqlBuilders(self): #SQL BUILDERS global queryType, fromStation, toStation, dayTrip, dayTime, lineName if queryType == 'timetable': if dayTrip.weekday()<5: partWeek = 'WD' else: partWeek = 'WE' queryList=[] for row in conn.execute(sqlBuildTimetable(fromStation, toStation, dayTime, partWeek)): queryList = queryList + [row] print("Let me see - I have a train leaving "+ fromStation + " at " + queryList[0][1] + " and arriving at " + toStation + " at " + queryList[0][3]) firstChoice = queryList[0] #save the original closest train queryList.sort(key=lambda tup: time.strptime(tup[1], "%H:%M")) #perform an in-place sort for later iteration based on time for n,y in enumerate(queryList): if y == firstChoice: index = n #keep track of the index position for smooth iteration if index==0: print("Would you like a later train?") elif index==len(queryList)-1: print('Would you like an earlier train?') else: print("Would you like an earlier or later train?") while 1: #try/catch block in an infinite loop to ensure smooth, repeatable input try: rawInput = compat.raw_input(">") except EOFError: print(rawInput) input=inputWSpellCheck(rawInput) if input: if input=="quit": self.quitChat() elif input=="help": print("Type 'earlier' to see earlier trains, or 'later' to see later trains. If you are happy with your selection, type 'no thanks'") print("Would you like an earlier or later train?") self.sqlBuilders() inputTokens = word_tokenize(input, language='english') #tokenize to check patterns for x in inputTokens: if x.lower() == "no": #always .lower() for matching purpose self.quitChat() break elif x.lower() == "early" or x.lower() == "earlier": if index==0: print("Sorry, no earlier trains found. Would you like a later train?") else: index -=1 print("Let me check - I have a train leaving " + fromStation + " at " + queryList[index][1] + " and arriving at "+ toStation + " at " + queryList[index][3]) if index==0: #check if earlier/later trains even exist print("Would you like a later train?") else: print("Would you like an earlier or later train?") elif x.lower() == "late" or x.lower() == "later": if index==len(queryList)-1: print("Sorry, no later trains found. Would you like an earlier train?") else: index +=1 print("Let me check - I have a train leaving " + fromStation + " at " + queryList[index][1] + " and arriving at "+ toStation + " at " + queryList[index][3]) if index==len(queryList)-1: print("Would you like an earlier train?") else: print("Would you like an earlier or later train?") else: print("Sorry, I didn't understand\n") print("Would you like an earlier or later train?") if queryType == 'trackwork': query = conn.execute(sqlBuildTrackwork(lineName, dayTrip, dayTime)) queryList=[] for x in query: queryList = queryList + [x] if queryList: queryLine = queryList[0][0] queryStartDate = datetime.datetime.strptime(queryList[0][1],"%Y-%m-%d %H:%M") queryEndDate = datetime.datetime.strptime(queryList[0][2],"%Y-%m-%d %H:%M") queryInfo = queryList[0][3] print("There is trackwork on the " + queryLine + " on " + queryStartDate.strftime("%A %d %B %H:%M") + " to " + queryEndDate.strftime("%A %d %B %H:%M") + "; " + queryInfo) print("Would you like more information?") while 1: try: rawInput = compat.raw_input(">") except EOFError: print(rawInput) input2=inputWSpellCheck(rawInput) if input2: if input2.lower()=="quit": self.quitChat() elif input2.lower()=="help": print("Let us know if you would like more help or not") self.sqlBuilders() tokens=word_tokenize(input2,language='english') for n in tokens: if n.lower() =='yes' or n.lower()=='yeah' or n.lower()=='yep' or n.lower()=='ye' or n.lower()=='yea': print("Would you like any trackwork or timetable information?") #reset variables fromStation=None toStation=None dayTrip=None dayTime=None lineName=None queryType=None nextTrigger=False lastIndex=-1 self.converseMain() break elif n.lower()=='no' or n.lower()=='nah' or n.lower()=='nope' or n.lower()=='na': self.quitChat() else: print("Sorry, I don't understand") print("Would you like more information?") else: print("There is no trackwork") print("Would you like any trackwork or timetable information?")
input_classifier.train(questions_file='C:/Users/John/Development/Python/AI_Final_Project/data/input/questions.txt', statements_file='C:/Users/John/Development/Python/AI_Final_Project/data/input/statements.txt') input_classifier.print_accuracy() input_classifier.print_important_features(5) statement_classifier = StatementClassifier() statement_classifier.train( genus_direct_file='C:/Users/John/Development/Python/AI_Final_Project/data/statements/genus_direct.txt', genus_direct_not_file='C:/Users/John/Development/Python/AI_Final_Project/data/statements/genus_direct_not.txt') statement_classifier.print_accuracy() statement_classifier.print_important_features(5) for i in range(10): print("Enter question or statement") query = raw_input() query = query.lower() word_tokens = nltk.word_tokenize(query) print("Your tagged query = {}".format(nltk.pos_tag(word_tokens))) print("This query is a {}".format(input_classifier.classify_text(query))) if input_classifier.classify_text(query) == 'question': print("I can't answer questions yet") continue print("This type of query is {}".format(statement_classifier.classify_text(query))) if statement_classifier.classify_text(query) == 'genus_direct_not': print("I don't know how to handle not statements yet") continue
from operator import itemgetter ##wprowadzanie wierzchołków from nltk.compat import raw_input from quicksort import quickSort wierzchołki=raw_input("Wprowadz wierzchołki: ") wierzchołki=wierzchołki.split() lista_wierzchołkow= [(a) for a in wierzchołki] print(lista_wierzchołkow) ilość_krawędzi=raw_input("Wprowadz liczbę krawędzi: ") lista_krawedzi=[] #dodawanie krawedzi for x in range(0,int(ilość_krawędzi)): krawedz= raw_input("Wprowadz 2 wierzchołki krawedzi i wage kazde po spacji nastepnie enter: ") krawedz= krawedz.split() krawedz= [(a) for a in krawedz] lista_krawedzi.append(krawedz) #sortowanie lista_krawedzi=quickSort(lista_krawedzi) print(lista_krawedzi) ##przykladowe dane # lista_wierzchołkow = list( "ABCDEF" ) # lista_krawedzi = [ ("A", "B", 4), ("B", "C", 2), # ("A", "F", 2),("C", "D", 8), # ("F", "D", 6), # ("A", "E", 1),("E", "B", 2),
def converseTimetableDay(self): weekDays = ["monday", "tuesday", "wednesday", "thursday", "friday"] weekEnds = ["saturday", "sunday"] input = "" global dayWeek, fromStation while 1: try: input = compat.raw_input(">") except EOFError: print(input) if input: if input == "quit": self.quitChat() elif input == "help": print( "Enter the day you want to leave by specifying weekend/weekday, the date in nth or n format, name of the day of week, or say 'next' to specify next week's dates" ) print( "Do you want to travel on a weekday or on a weekend?") self.converseTimetableDay() nextTrigger = False #for using 'next' keyword eg. next week, next monday etc realDate = None #uninitialised date object inputTokens = word_tokenize(input, language='english') for x in inputTokens: if x.lower() == "next": nextTrigger = True if x.lower() in weekDays or x.lower() in weekEnds: realDate = getDateTime( x.lower(), nextTrigger ) #get the date object for the proposed travel date elif x.lower() == "weekday": dayWeek = "WD" print( "Travelling on a weekday, and what time would you like to depart from " + fromStation + "?") self.converseTimetableTime() break elif x.lower() == "weekend": dayWeek = "WE" print( "Travelling on a weekend, and what time would you like to depart from " + fromStation + "?") self.converseTimetableTime() break elif x.lower() in calNums.keys( ): #see calNums, check calendar dates realDate = getDateTime(x.lower(), nextTrigger) elif x.lower()[:2].isdigit() and x.lower()[2:] in ( "th", "nd", "st", "rd" ): #using slices instead of regex because i can realDate = getDateTime(x.lower()[:2], nextTrigger) elif x.lower()[:1].isdigit() and x.lower()[1:] in ( "th", "nd", "st", "rd"): realDate = getDateTime(x.lower()[:1], nextTrigger) elif x.lower() == "today": realDate = datetime.datetime.strptime( "2017-09-16", "%Y-%m-%d" ) #set the date used for the trackwork test elif x.lower() == "tomorrow": realDate = datetime.datetime.strptime( "2017-09-16", "%Y-%m-%d") + datetime.timedelta( 1) #timedelta to perform time arithmetic if realDate: #specify for SQL query if realDate.weekday() < 5: dayWeek = "WD" else: dayWeek = "WE" print("Travelling on " + realDate.strftime("%A, %B %d") + ", and what time would you like to depart from " + fromStation + "?") self.converseTimetableTime() break else: print("Sorry, I don't understand\n") print( "Do you want to travel on a weekday or on a weekend?") else: print("Sorry, I don't understand\n") print("Do you want to travel on a weekday or on a weekend?")
input_classifier.print_important_features(5) statement_classifier = StatementClassifier() statement_classifier.train( genus_direct_file= 'C:/Users/John/Development/Python/AI_Final_Project/data/statements/genus_direct.txt', genus_direct_not_file= 'C:/Users/John/Development/Python/AI_Final_Project/data/statements/genus_direct_not.txt' ) statement_classifier.print_accuracy() statement_classifier.print_important_features(5) for i in range(10): print("Enter question or statement") query = raw_input() query = query.lower() word_tokens = nltk.word_tokenize(query) print("Your tagged query = {}".format(nltk.pos_tag(word_tokens))) print("This query is a {}".format( input_classifier.classify_text(query))) if input_classifier.classify_text(query) == 'question': print("I can't answer questions yet") continue print("This type of query is {}".format( statement_classifier.classify_text(query))) if statement_classifier.classify_text(query) == 'genus_direct_not':
# # inverse dictionary. This is easy for future lookups of ids # id_to_word = {val: key for key, val in word_to_id.items()} # # Retrieve the TFIDF # logger.info("Loading the TFIDF.") tfidf_matrix = mmread("files/pubMed-tfidf.mm") # # User input # print("Enter a word for which you want co-occurance: ") user_word = raw_input() print("How many layers of output do you want to generate(0-5): ") level = int(raw_input()) # # This method returns all the documents containing the given word. # def fetchAllDocuments(param): docs = [] id = word_to_id[param] counter = 0 for col in tfidf_matrix.col: if col == id: docs.append(tfidf_matrix.row[counter]) counter += 1 return docs