Home > database >  Text Analysis on Python
Text Analysis on Python

Time:10-18

I need to write code that can find all of the common words between both variables and create a dictionary that shows how many times each common word occurs.

text_a = "The analyzed text can be broken into pieces, then followed by deletion of some arguments"
text_b = "I reversed the deletion of the pieces in your text, because the arguments won't stick"

CodePudding user response:

A way that you could find the common words between each string is to do:

text_a = "The analyzed text can be broken into pieces, then followed by deletion of some arguments"
text_b = "I reversed the deletion of the pieces in your text, because the arguments won't stick"

text_a_set = set(text_a.split()) # separate the string into its own words in a list then convert the list to set
text_b_set = set(text_b.split())

print(text_a_set.intersection(text_b_set)) # use a set operation to find the intersection (the words in both sets)

this is assuming that the input strings are not cleaned, if you were to remove ',', '.', etc you would come up with different results

CodePudding user response:

text_a = "The analyzed text can be broken into pieces, then followed by deletion of some arguments"
text_b = "I reversed the deletion of the pieces in your text, because the arguments won't stick"

s_text_a = set(text_a.split())
s_text_b = set(text_b.split())
common_words = s_text_a & s_text_b
print(common_words)
  • Related