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
  • 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...

  • 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,...

  • 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 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...

  • 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...

  • 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...

  • In this project, you will write a complete program that allows the user to play a...

    In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps: 1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m. 2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1]. 3. The codebreaker is prompted to enter a guess, an n-digit sequence....

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

  • Coding for Python – don’t need it to be complex: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is locate...

    Coding for Python – don’t need it to be complex: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. This type of problem is common in many health disciplines. For example, identifying treatment pathways. The goal is to detect whether a certain pattern appears in the data series. In the following example, the pattern to be...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

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