def inside(): global ls ls = [] c = request.form choice = c['val'] if choice == 'r': return redirect("http://localhost:5000/recommend") else: user_choice = int(choice) - 1 selected_doc = user_docs[user_choice] all_docs = lst[0] classifier_list = lst[2] # classifiers are sorted according to their f_measure in decreasing order. It helps when all # three classifiers differ in their predictions. classifier_list = sorted(classifier_list, key=lambda cl: cl.stats['f_measure'], reverse=True) prediction_list = list() for classifier in classifier_list: prediction_list.append(classifier.classify([selected_doc])[0]) prediction_count = Counter(prediction_list) top_prediction = prediction_count.most_common(1) if top_prediction[0][1] > 1: prediction = top_prediction[0][0] else: prediction = prediction_list[0] # create knn instance using documents of predicted topic. and find k closest documents. knn = KNN(all_docs[prediction]) k_neighbours = knn.find_k_neighbours(selected_doc, k_n) ls.append(selected_doc) ls.append(k_neighbours) return render_template('scnd.html', ls=ls)
def recommendation(all_docs, test_docs, classifier_list): print("Recommendation System") print("---------------------") # ask user for the desired option count and recommendation count. set default value in case invalid inputs. try: option_count = int(raw_input("\nEnter number of articles to choose from. [number from 5 to 10 suggested]: ")) if option_count < 1 or option_count > 20: print("Invalid Choice.. By default selected 5.") option_count = 5 except: print("Invalid Choice.. By default selected 5.") option_count = 5 try: k_n = int(raw_input("\nEnter number of recommendation per article. [number from 5 to 10 suggested]: ")) if k_n < 1 or k_n > 20: print("Invalid Choice.. By default selected 5.") k_n = 5 except: print("Invalid Choice.. By default selected 5.") k_n = 5 end = False # run the loop until user quits. while not end: # pick random documents from test docs and provide titles to the user. user_docs = random.sample(test_docs, option_count) while True: print("\n---Available Choices For Articles(Titles)---\n") for i in range(len(user_docs)): print(str(i+1) + ": " + user_docs[i].title) print("r: Refresh List") print("q: Quit()\n") choice = raw_input("Enter Choice: ") if choice == 'q': end = True break elif choice == 'r': break else: try: user_choice = int(choice) - 1 if user_choice < 0 or user_choice >= len(user_docs): print("Invalid Choice.. Try Again..") continue except: print("Invalid Choice.. Try Again..") continue selected_doc = user_docs[user_choice] # classifiers are sorted according to their f_measure in decreasing order. It helps when all # three classifiers differ in their predictions. classifier_list = sorted(classifier_list, key=lambda cl: cl.stats['f_measure'], reverse=True) prediction_list = list() for classifier in classifier_list: prediction_list.append(classifier.classify([selected_doc])[0]) prediction_count = Counter(prediction_list) top_prediction = prediction_count.most_common(1) if top_prediction[0][1] > 1: prediction = top_prediction[0][0] else: prediction = prediction_list[0] # create knn instance using documents of predicted topic. and find k closest documents. knn = KNN(all_docs[prediction]) k_neighbours = knn.find_k_neighbours(selected_doc, k_n) while True: print("\nRecommended Articles for : " + selected_doc.title) for i in range(len(k_neighbours)): print(str(i+1) + ": " + k_neighbours[i].title) next_choice = raw_input("\nEnter Next Choice: [Article num to read the article. " "'o' to read the original article. " "'b' to go back to article choice list.] ") if next_choice == 'b': break elif next_choice == 'o': text = selected_doc.text print("\nArticle Text for original title : " + selected_doc.title) print(text) else: try: n_choice = int(next_choice) - 1 if n_choice < 0 or n_choice >= k_n: print("Invalid Choice.. Try Again..") continue except: print("Invalid Choice.. Try Again..") continue text = k_neighbours[n_choice].text print("\nArticle Text for recommended title : " + k_neighbours[n_choice].title) print(text)
def recommendation(all_docs, test_docs, classifier_list): print("Recommendation System") print("---------------------") # ask user for the desired option count and recommendation count. set default value in case invalid inputs. try: option_count = int( raw_input( "\nEnter number of articles to choose from. [number from 5 to 10 suggested]: " )) if option_count < 1 or option_count > 20: print("Invalid Choice.. By default selected 5.") option_count = 5 except: print("Invalid Choice.. By default selected 5.") option_count = 5 try: k_n = int( raw_input( "\nEnter number of recommendation per article. [number from 5 to 10 suggested]: " )) if k_n < 1 or k_n > 20: print("Invalid Choice.. By default selected 5.") k_n = 5 except: print("Invalid Choice.. By default selected 5.") k_n = 5 end = False # run the loop until user quits. while not end: # pick random documents from test docs and provide titles to the user. user_docs = random.sample(test_docs, option_count) while True: print("\n---Available Choices For Articles(Titles)---\n") for i in range(len(user_docs)): print(str(i + 1) + ": " + user_docs[i].title) print("r: Refresh List") print("q: Quit()\n") choice = raw_input("Enter Choice: ") if choice == 'q': end = True break elif choice == 'r': break else: try: user_choice = int(choice) - 1 if user_choice < 0 or user_choice >= len(user_docs): print("Invalid Choice.. Try Again..") continue except: print("Invalid Choice.. Try Again..") continue selected_doc = user_docs[user_choice] # classifiers are sorted according to their f_measure in decreasing order. It helps when all # three classifiers differ in their predictions. classifier_list = sorted(classifier_list, key=lambda cl: cl.stats['f_measure'], reverse=True) prediction_list = list() for classifier in classifier_list: prediction_list.append( classifier.classify([selected_doc])[0]) prediction_count = Counter(prediction_list) top_prediction = prediction_count.most_common(1) if top_prediction[0][1] > 1: prediction = top_prediction[0][0] else: prediction = prediction_list[0] # create knn instance using documents of predicted topic. and find k closest documents. knn = KNN(all_docs[prediction]) k_neighbours = knn.find_k_neighbours(selected_doc, k_n) while True: print("\nRecommended Articles for : " + selected_doc.title) for i in range(len(k_neighbours)): print(str(i + 1) + ": " + k_neighbours[i].title) next_choice = raw_input( "\nEnter Next Choice: [Article num to read the article. " "'o' to read the original article. " "'b' to go back to article choice list.] ") if next_choice == 'b': break elif next_choice == 'o': text = selected_doc.text print("\nArticle Text for original title : " + selected_doc.title) print(text) else: try: n_choice = int(next_choice) - 1 if n_choice < 0 or n_choice >= k_n: print("Invalid Choice.. Try Again..") continue except: print("Invalid Choice.. Try Again..") continue text = k_neighbours[n_choice].text print("\nArticle Text for recommended title : " + k_neighbours[n_choice].title) print(text)
def recommendation(all_docs, test_docs, classifier_list): print("Recommendation System") print("---------------------") try: option_count = int( input( "\nEnter number of articles to choose from. [number from 5 to 10 suggested]: " )) if option_count < 1 or option_count > 20: print("Invalid Choice.. By default selected 5.") option_count = 5 except: print("Invalid Choice.. By default selected 5.") option_count = 5 try: k_n = int( input( "\nEnter number of recommendation per article. [number from 5 to 10 suggested]: " )) if k_n < 1 or k_n > 20: print("Invalid Choice.. By default selected 5.") k_n = 5 except: print("Invalid Choice.. By default selected 5.") k_n = 5 end = False while not end: user_docs = random.sample(test_docs, option_count) while True: print("\n---Available Choices For Articles(Titles)---\n") for i in range(len(user_docs)): print(str(i + 1) + ": " + user_docs[i].title) print("r: Refresh List") print("q: Quit()\n") choice = input("Enter Choice: ") if choice == 'q': end = True break elif choice == 'r': break else: try: user_choice = int(choice) - 1 if user_choice < 0 or user_choice >= len(user_docs): print("Invalid Choice.. Try Again..") continue except: print("Invalid Choice.. Try Again..") continue selected_doc = user_docs[user_choice] classifier_list = sorted(classifier_list, key=lambda cl: cl.stats['f_measure'], reverse=True) prediction_list = list() for classifier in classifier_list: prediction_list.append( classifier.classify([selected_doc])[0]) prediction_count = Counter(prediction_list) top_prediction = prediction_count.most_common(1) if top_prediction[0][1] > 1: prediction = top_prediction[0][0] else: prediction = prediction_list[0] knn = KNN(all_docs[prediction]) k_neighbours = knn.find_k_neighbours(selected_doc, k_n) while True: print("\nRecommended Articles for : " + selected_doc.title) for i in range(len(k_neighbours)): print(str(i + 1) + ": " + k_neighbours[i].title) next_choice = input( "\nEnter Next Choice: [Article num to read the article. " "'o' to read the original article. " "'b' to go back to article choice list.] ") if next_choice == 'b': break elif next_choice == 'o': text = selected_doc.text print("\nArticle Text for original title : " + selected_doc.title) print(text) else: try: n_choice = int(next_choice) - 1 if n_choice < 0 or n_choice >= k_n: print("Invalid Choice.. Try Again..") continue except: print("Invalid Choice.. Try Again..") continue text = k_neighbours[n_choice].text print("\nArticle Text for recommended title : " + k_neighbours[n_choice].title) print(text)