Home > database >  Making Dictionary with common words from paragraphs
Making Dictionary with common words from paragraphs

Time:11-13

Link to problem statement

Please help. I am very confused on how to execute this:

This is what I currently have:

def similarityAnalysis(paragraph1, paragraph2):
    dict = {}
    for word in lst:
        if word in dict:
            dict[word] = dict[word]   1
        else:
            dict[word] = 1
    for key, vale in dict.items():
        print(key, val)

CodePudding user response:

see below.

  • For find common words we use set intersection
  • For counting we use a dict

Code

lst1 = ['jack','Jim','apple']
lst2 = ['chair','jack','ball','steve']
common = set.intersection(set(lst1),set(lst2))
print('commom words below:')
print(common)
print()

print('counter below:')
counter = dict()
for word in lst1:
  if word not in counter:
    counter[word] = [0,0]
  counter[word][0]  = 1
for word in lst2:
  if word not in counter:
    counter[word] = [0,0]
  counter[word][1]  = 1
print(counter)

output

commom words below:
{'jack'}

counter below:
{'jack': [1, 1], 'Jim': [1, 0], 'apple': [1, 0], 'chair': [0, 1], 'ball': [0, 1], 'steve': [0, 1]}

CodePudding user response:

Analysing your code as follows:

  1. You use the variable name dict which is a reserved keyword (for creating dictionaries). By using this as a variable name, you will loose the ability to use the dict function.

  2. The function uses a variable named lst which is not one of its arguments. Where do the values for this variable come from?

  3. In the second for loop, you use the variable name vale but then later reference a different variable called val.

Otherwise, looks good. There may be other issues, that's as far as I got.

Recommend googling the following and seeing what code you find

  • "Python count the number of words in a paragraph"

Update: There are many ways to do this, but here's one answer:

def word_counts(lst):
    counts = {}
    for word in lst:
        counts[word] = counts.get(word, 0)   1
    return counts

def similarityAnalysis(paragraph1, paragraph2):
    lst1 = paragraph1.split()
    lst2 = paragraph2.split()
    counts1 = word_counts(lst1)
    counts2 = word_counts(lst2)
    common_words = set(lst1).intersection(lst2)
    return {word: (counts1[word], counts2[word]) for word in common_words}

paragraph1 = 'one three two one two four'
paragraph2 = 'one two one three three one'

print(similarityAnalysis(paragraph1, paragraph2))

Output:

{'three': (1, 2), 'one': (2, 3), 'two': (2, 1)}
  • Related