Question

The goal is to write a program that reads two nove

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#The following is the code for the given problem statement you can give any number for k . as you did not mention anything about the input i took it as reading novels from text files.

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 9 10:18:35 2016

@author: naresh
"""
import os
import operator
#import csv
os.chdir("/home/naresh/Desktop/HomeworkLib/")
l1=[]
l1 = list(input("Enter the Novel Names:").split(','))
list_of_words = []
dict1 = {}
dict2 = {}
wrds_l_n1 = []
wrds_l_n2 = []
def read_words2(words_file):
list_of_words.append([word for line in open(words_file, 'r') for word in line.split()])
for val in l1:
read_words2(val);
for val in list_of_words[0]:
if val not in dict1:
dict1[val] = 1
else:
dict1[val] += 1
sorted_dict1 = sorted(dict1.items(), key=operator.itemgetter(1))
#type(dict1)
for val in list_of_words[1]:
if val not in dict2:
dict2[val] = 1
else:
dict2[val] += 1
sorted_dict2 = sorted(dict2.items(), key=operator.itemgetter(1))
k = int(input("Enter a k value: "))
if len(sorted_dict1) >= k:
top_k_values_n1 = sorted_dict1[-k:]
else:
top_k_values_n1 = sorted_dict1
if len(sorted_dict2) >= k:
top_k_values_n2 = sorted_dict2[-k:]
else:
top_k_values_n2 = sorted_dict2

for val in top_k_values_n1:
wrds_l_n1.append(val[0])
for val in top_k_values_n2:
wrds_l_n2.append(val[0])
print("The top k words that are present in Novel1 but not in Novel2: ")
for val in wrds_l_n1:
if val not in wrds_l_n2:
print(val)

#print("\n")
print("The top k words that are present in Novel2 but not in Novel1: ")
for val in wrds_l_n2:
if val not in wrds_l_n1:
print(val)
  

Enter the Novel Names: AmericanPsycho.txt, TheLostTine.txt Enter a k value: 10 The top k words that are present in Novel1 but

TheLostTime.txt AmericanPsycho.txt 1 There is an idea of a Patrick Batenan some kind of abstraction but there is no real me o

AmericanPsycho.txt outcome ผe do not think of the outcome which generally comes to pass and is also favourable we do not succ

Add a comment
Know the answer?
Add Answer to:
The goal is to write a program that reads two novels, finds the top-k most common...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3 Write a program that reads a sequence of words and prints every word whose...

    Python 3 Write a program that reads a sequence of words and prints every word whose frequency is equal to its length. You can assume that there will always be a word that meets this condition. If more than one word have this condition, you must print first the most frequent one. If two or more words with equal frequencies meet this condition, print them from the smallest to the largest in alphabetical order. Sample Input lee john lee peter...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • In this lab, you will write a program that reads a series name/value pairs, and stores...

    In this lab, you will write a program that reads a series name/value pairs, and stores them in a pair of vectors. After the name/value pairs are read, it will then read names (until the input is exhausted) and print out the corresponding value for that name. If the name is not found in the list, "name not found" should be printed. The names should be read as strings and stored as a vector<string>; the values should be read as...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.t...

    write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

  • Write a C/C++ program that reads a list of words and prints them in alphabetical order...

    Write a C/C++ program that reads a list of words and prints them in alphabetical order with one word per line. If two words have the same beginning sequence, the shorter word should be printed first. If the program is called with an “r” on the command line, the list of words should be printed in reverse alphabetical order. Test your program using the bunch-of-words.txt file bunch-of-words.txt: To Sherlock Holmes she is always the woman. I have seldom heard him...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • Consider the two strings "doggie dish" and "Drip coffee". What letters do they have in common?...

    Consider the two strings "doggie dish" and "Drip coffee". What letters do they have in common? That is, what is the letter intersection count for the pair of strings? Let's look at 'd' first. The first string has 2 d's (d's occurrence count is 2), while the second has just 1 d (capitalization doesn't count) - so the intersection for the strings for d is 1. What about g? There the occurrence counts are 2 and 0, so the intersection...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT