Untitled

 avatar
unknown
python
2 years ago
1.9 kB
6
Indexable
text_input = input("Enter the text: ").lower()

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

print("\nPunctuations removed: ")

no_punctuation = ""

for char in text_input:
  
   # If the character is not a punctuation mark, add it to the no_punctuation string
   if char not in punctuations:
     no_punctuation = no_punctuation + char
     
print(no_punctuation)

common_articles = ['a', 'that', 'an', 'the', 'on' ,'in' ,'for', 'of', 'it', 'he', 'she', 'her', 'him' ,'his' ,'they', 'to' ,'was' ,'and', 'is' ]
print("\nCommon words removed:")

modified_words = []

word_frequencies =  {}

unique_words = []

words = no_punctuation.split()

for word in words:
  
  # If the word is not a common article, add it to the list of modified words
  if word not in common_articles:
    modified_words.append(word)

    if word not in unique_words:
      unique_words.append(word)

    # If the word is already in the dictionary, increment its count
    # Otherwise, initialize its count to 1
    if word in word_frequencies:
      word_frequencies[word] += 1
    else:
      word_frequencies[word] = 1

print(' '.join(modified_words))

# sort the list with a custom key
unique_words.sort(key = lambda x: word_frequencies[x], reverse=True)
 
print(f"\n3 most frequent words: {unique_words[:3] }")

with open('positivewords', "r") as f:
  
  file = f.read().splitlines()

positive_words_count = 0

positive_words = []

for frequent_word in unique_words[:3]:
  
  # If the word is in the 'positivewords' file, add it to the list of positive words and increment the count
  if frequent_word in file:
    positive_words.append(frequent_word)
    positive_words_count += 1
    
print(f"\n{positive_words_count} word(s) are positive: {' '.join(positive_words)}")

if positive_words_count >= 2:
  print("This text conveys a positive sentiment.")
else:
  print("This text does not convey a positive sentiment")
Editor is loading...