0

My program is to find the number of types, the number of tokens, and the type-to-token ratio. However, I don't know how to tell Python the answer to ttr is not an integer.

from nltk.corpus import inaugural 

print inaugural.fileids()

tokens = inaugural.words("1789-Washington.txt")

numtokens = len(tokens)
print numtokens

types = sorted(set(tokens))
numtypes = len(types)
print numtypes

# This is the part I'm unsure about.     
ttr = numtypes/numtokens
print ttr
tobias_k
  • 81,265
  • 12
  • 120
  • 179
Auborey
  • 93
  • 1
  • 11

1 Answers1

2

If you are working in Python 3, the division operator / is performing floating point division by default:

>>> 3 / 2
1.5
>>> 4 / 2
2.0

since integer division is handled by the // operator.

In Python 2.x, if you want decimal accuracy in integer division, you can convert either the nominator or the denominator to float(), like this:

ttf = float(numtypes) / numtokens

Alternatively, as tobias_k points out, you can do

>>> from __future__ import division
>>> 3 / 2
1.5

to get Python3-esque division in Python 2.x

VHarisop
  • 2,816
  • 1
  • 14
  • 28