Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/396846815083237376
deleted 24 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Improve code for python dict and if conditions Word analysis on input from a file

How do I improve the below code, which takes input from a file and does some word analysis. I? I find a lot of redundancy in it. I want to know a better way to implementof implementing it.
 

 analysis = {}
 wordanalysis = {}
 found = True 

 for line in sys.stdin:
    (n1,n2,n3,n4,n5,n6) = re.split("\t+",line.strip())
    tweet = re.split("\s+", n6.strip().lower())
    #adding tweet id to a dictionary

    for i in range(0, len(tweet)):
      print "{", n1, wordanalysis,"}"
      wordanalysis["word"] = tweet[i]
      wordanalysis["charcount"]= len(tweet[i])

      if len(tweet[i]) > 7:
         wordanalysis["longword"] = found
      else:
         wordanalysis["longword"] = not(found)
      #if 1st char is UpperCase
      if tweet[i].istitle():
         wordanalysis["title"] = found
      else:
         wordanalysis["title"] = not(found)

      if tweet[i].isupper():
         wordanalysis["uppercase"] = found
      else:
         wordanalysis["uppercase"] = not(found)

Improve code for python dict and if conditions

How do I improve the below code which takes input from a file and does some word analysis. I find a lot of redundancy in it. I want to know a better way to implement it.
 

 analysis = {}
 wordanalysis = {}
 found = True 

 for line in sys.stdin:
    (n1,n2,n3,n4,n5,n6) = re.split("\t+",line.strip())
    tweet = re.split("\s+", n6.strip().lower())
    #adding tweet id to a dictionary

    for i in range(0, len(tweet)):
      print "{", n1, wordanalysis,"}"
      wordanalysis["word"] = tweet[i]
      wordanalysis["charcount"]= len(tweet[i])

      if len(tweet[i]) > 7:
         wordanalysis["longword"] = found
      else:
         wordanalysis["longword"] = not(found)
      #if 1st char is UpperCase
      if tweet[i].istitle():
         wordanalysis["title"] = found
      else:
         wordanalysis["title"] = not(found)

      if tweet[i].isupper():
         wordanalysis["uppercase"] = found
      else:
         wordanalysis["uppercase"] = not(found)

Word analysis on input from a file

How do I improve the below code, which takes input from a file and does some word analysis? I find a lot of redundancy in it. I want to know a better way of implementing it.

analysis = {}
wordanalysis = {}
found = True 

for line in sys.stdin:
   (n1,n2,n3,n4,n5,n6) = re.split("\t+",line.strip())
   tweet = re.split("\s+", n6.strip().lower())
   #adding tweet id to a dictionary

   for i in range(0, len(tweet)):
     print "{", n1, wordanalysis,"}"
     wordanalysis["word"] = tweet[i]
     wordanalysis["charcount"]= len(tweet[i])

     if len(tweet[i]) > 7:
        wordanalysis["longword"] = found
     else:
        wordanalysis["longword"] = not(found)
     #if 1st char is UpperCase
     if tweet[i].istitle():
        wordanalysis["title"] = found
     else:
        wordanalysis["title"] = not(found)

     if tweet[i].isupper():
        wordanalysis["uppercase"] = found
     else:
        wordanalysis["uppercase"] = not(found)
Source Link
fscore
  • 634
  • 1
  • 7
  • 15

Improve code for python dict and if conditions

How do I improve the below code which takes input from a file and does some word analysis. I find a lot of redundancy in it. I want to know a better way to implement it.

 analysis = {}
 wordanalysis = {}
 found = True 

 for line in sys.stdin:
    (n1,n2,n3,n4,n5,n6) = re.split("\t+",line.strip())
    tweet = re.split("\s+", n6.strip().lower())
    #adding tweet id to a dictionary

    for i in range(0, len(tweet)):
      print "{", n1, wordanalysis,"}"
      wordanalysis["word"] = tweet[i]
      wordanalysis["charcount"]= len(tweet[i])

      if len(tweet[i]) > 7:
         wordanalysis["longword"] = found
      else:
         wordanalysis["longword"] = not(found)
      #if 1st char is UpperCase
      if tweet[i].istitle():
         wordanalysis["title"] = found
      else:
         wordanalysis["title"] = not(found)

      if tweet[i].isupper():
         wordanalysis["uppercase"] = found
      else:
         wordanalysis["uppercase"] = not(found)