Judge a Passage and Manipulate (Id-3149)

In English, a single word is called an unigram and two words together are called bigrams. Some words give positive meaning and some give negative meaning. Weightage is also given for unigrams and bigrams. Given a file 'unigrams.txt' with single positive and negative words with weights and a file 'bigrams.txt' with double positive and negative words with their weights, Write a pseudocode and the subsequent Python code to determine if a passage in the input file is positive or negative. Both unigrams and bigrams in 'unigrams.txt' and 'bigrams.txt' begin with either positive or negative sign to indicate whether it is a positive or negative term. A passage is said to be positive when the sum of weight of positive unigrams and bigrams in it is greater than sum of weight of negative unigrams and bigrams in it. If a passage is negative then replace all the negative unigrams with positive unigrams. Passage in the input file ends with the keyword “END”. Weightage of all positive unigrams are unique and similarly weightage of all negative unigrams are also unique.
For example, if the content of unigrams.txt is as follows:
-bad 5
-worse 3
-worst 6
+great 6
+good 5
+nice 3
bad, worse and worst are negative words and great, good and nice are positive words and their weightage is give that is separated by a tab. If content of 'bigrams.txt' is as follows:
+very good 5
+very great 7
-very bad 5
-bad luck 7
-bad mood 6
+good mood 6
If the input passage is “the movie was very good that we enjoyed it though the theatre was bad It was a great experience Last movie was very bad and we came out with a bad mood good END ” then weightage of positive terms is 21 and weightage of negative terms is 26. So negative unigrams have to be replaced by positive unigrams of same weightage and output passage will
the movie was very good that we enjoyed it though the theatre was good It was a great experience Last movie was very good and we came out with a good mood good
Input Format
Name of the file with input passage
Output Format
Total weight of positive terms
Total weight of negative terms
Print manipulated text if negative terms > positive terms and print original text otherwise

Input
The name of the file

Processing Involved:
def replace(good,bad,name):
    file = open(name)
    contents = file.read()
    contents = contents[:-4]
    file.seek(0)
    bad_keys = list(bad.keys())
    bad_keys.sort(key = lambda x:len(x),reverse=True)
    good_keys = list(good.keys())
    good_keys.sort(key=lambda x: len(x), reverse=True)
    for i in bad_keys:
        if i in contents:
            for k in good_keys:
                if good[k] == bad[i] and len(i.split()) == len(k.split()):
                    contents = contents.replace(i,k)
                    break
    print(contents)

def file_score(good,bad,name):
    bad_score = 0
    good_score = 0
    file = open(name)
    for i in sorted(bad.keys()):
        if i in file.read():
            file.seek(0)
            bad_score += bad[i]*(file.read()).count(i)
        file.seek(0)
    for i in sorted(good.keys()):
        if i in file.read():
            file.seek(0)
            good_score += good[i]*(file.read()).count(i)
        file.seek(0)
    if bad_score > good_score:
        replace(good,bad,name)
    file.close()

bad = {}
good = {}
name = input()
read_data(name)
separator(good,bad,'unigrams.txt')
separator(good,bad,'bigrams.txt')

file_score(good,bad,name)

Output:
Total weight of positive terms

Total weight of negative terms


Print manipulated text if negative terms > positive terms and print original text otherwise

Program:
def replace(good,bad,name):
    file = open(name)
    contents = file.read()
    contents = contents[:-4]
    file.seek(0)
    bad_keys = list(bad.keys())
    bad_keys.sort(key = lambda x:len(x),reverse=True)
    good_keys = list(good.keys())
    good_keys.sort(key=lambda x: len(x), reverse=True)
    for i in bad_keys:
        if i in contents:
            for k in good_keys:
                if good[k] == bad[i] and len(i.split()) == len(k.split()):
                    contents = contents.replace(i,k)
                    break
    print(contents)

def file_score(good,bad,name):
    bad_score = 0
    good_score = 0
    file = open(name)
    for i in sorted(bad.keys()):
        if i in file.read():
            file.seek(0)
            bad_score += bad[i]*(file.read()).count(i)
        file.seek(0)
    for i in sorted(good.keys()):
        if i in file.read():
            file.seek(0)   
            good_score += good[i]*(file.read()).count(i)
        file.seek(0)
    print(good_score,bad_score,sep='\n')
    if bad_score > good_score:
        replace(good,bad,name)
    else:
        file.seek(0)
        print(file.read()[:-4])
    file.close()
bad = {'bad': 5, 'worse': 3, 'worst': 6, 'very bad': 5, 'bad luck': 7, 'bad mood': 6}
good = {'great': 6, 'good': 5, 'nice': 3, 'very good': 5, 'very great': 7, 'good mood': 6}
name = input()
file_score(good,bad,name)
Pseudocode:
Step1. get the name of the file
Step2. create two dictionaries to store the bad words and the score and good words and the score.
Step3. read the file and calculate the bad_score and good_score if bad_score is greater than the good_score then call the replace function to replace the bad words with good words and print the passage else print the original contents
Step4.End

8 comments:

  1. Code isn't working...����

    ReplyDelete
    Replies
    1. What is the error....please take a screenshot and send it realskillvit@gmail.com

      Delete
    2. c++ code im waiting since 2017

      Delete
  2. please decode this in half an hour


    Given 'n' rectangles, form all possible bigger rectangles, by joining the given rectangles. Two rectangles can be joined if they have a common side. Write an algorithm and the subsequent Python code to determine the bigger rectangles (which are formed by combining the given rectangles) of breadth 'l' in minimum cost 'C'. Cost for combining two rectangles r1 and r2 to form a bigger rectangle r3 is '1'.

    For example, given 10 rectangles, with coordinates as shown below and breadth as 8. Let the rectangles be indexed from 0 to 9.
    [(0,5), (10,5), (10,10), (0, 10)] 0
    [(10, 5),(20,5),(20,10),(10,10)] 1
    [(20,5), (25,5), (25,10), (20,10)] 2
    [(0,10),(10, 10),(10, 13), (0,13)] 3
    [(10,10),(20,10),(20,13),(10,13)] 4
    [(0,13),(10,13),(10,15),(0,15)] 5
    [(10,13),(20,13),(20,15),(10,15)] 6
    [(-5,5),(-1,5),(-1,10),(-5,10)] 7
    [(0,20),(10,20),(0,30),(10,30)] 8
    [(0,7),(10,7),(10,10),(0,10)] 9
    Coordinates of all possible bigger rectangles of breadth 8 are:
    [(0, 5), (20, 5), (20, 13), (0, 13)]
    [(0, 5), (10, 5), (10, 13), (0, 13)]
    [(10, 5), (20, 5), (20, 13), (10, 13)]
    [(0, 7), (10, 7), (10, 15), (0, 15)]
    and the biggest rectangle of breadth 8 that can be formed with minimum cost and the index number of the rectangles that are combined are shown as below:
    [(0, 5), (10, 5), (10, 13), (0, 13)] (0,3)
    [(10, 5), (20, 5), (20, 13), (10, 13)] (1,4)
    Input Format
    Number of rectangles
    X-Coordinate of corner1 of rectangle 1
    Y-Coordinate of corner1 of rectangle 1
    X-Coordinate of corner2 of rectangle 1
    Y-Coordinate of corner2 of rectangle 1
    X-Coordinate of corner3 of rectangle 1
    Y-Coordinate of corner3 of rectangle 1
    X-Coordinate of corner4 of rectangle 1
    Y-Coordinate of corner4 of rectangle 1
    X-Coordinate of corner1 of rectangle 2
    Y-Coordinate of corner1 of rectangle 2
    X-Coordinate of corner2 of rectangle 2
    Y-Coordinate of corner2 of rectangle 2
    X-Coordinate of corner3 of rectangle 2
    Y-Coordinate of corner3 of rectangle 2
    X-Coordinate of corner4 of rectangle 2
    Y-Coordinate of corner4 of rectangle 2
    ....
    X-Coordinate of corner1 of rectangle n
    Y-Coordinate of corner1 of rectangle n
    X-Coordinate of corner2 of rectangle n
    Y-Coordinate of corner2 of rectangle n
    X-Coordinate of corner3 of rectangle n
    Y-Coordinate of corner3 of rectangle n
    X-Coordinate of corner4 of rectangle n
    Y-Coordinate of corner4 of rectangle n
    Desired breadth of rectangle 'l'
    Output Format
    Coordinates of all rectangles of breadth 'l' and index of the smaller rectangles as a tuple separated by a tab

    ReplyDelete
  3. thambi..sondhama podu pa....

    ReplyDelete
  4. Online Casino Site | Bet365 Online Casino | Lucky Club
    Sign up at Lucky Club and claim your exclusive welcome bonus of up 카지노사이트luckclub to €500 + 500 free spins. · Click to claim your welcome bonus. · · Make a deposit. · Log in. · Sign in.

    ReplyDelete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...