文本中单词的音节数

时间:2011-05-03 21:38:38

标签: python nltk

我有以下代码摘录,使用NLTK查找给定输入文本'sample.txt'中所有单词的音节数:

   import re
   import nltk
   from curses.ascii import isdigit
   from nltk.corpus import cmudict
   import nltk.data
   import pprint

   d = cmudict.dict()

   tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
   fp = open("sample.txt")
   data = fp.read()
   tokens = nltk.wordpunct_tokenize(data)
   text = nltk.Text(tokens)
   words = [w.lower() for w in text]
   print words #to print all the words in input text
   regexp = "[A-Za-z]+"
   exp = re.compile(regexp)

   def nsyl(word):
      return max([len([y for y in x if isdigit(y[-1])]) for x in d[word]])

  sum1 = 0
  count = 0
  count1 = 0
  for a in words:
     if exp.match(a)):
         print a
         print "no of syllables:",nysl(a)
         sum1 = sum1 + nysl(a)
         print "sum of syllables:",sum1
         if nysl(a)<3:
             count = count + 1
         else:
             count1 = count1 + 1

  print "no of words with syll count less than 3:",count
  print "no of complex words:",count1

此代码将每个输入字与cmu字典匹配,并给出该字的音节数。但它无法工作并显示错误,因为在字典中找不到单词或我在输入中使用专有名词。我想检查dictinary中是否存在该单词,如果不存在,请跳过它并继续考虑下一个单词。我该怎么做?

1 个答案:

答案 0 :(得分:3)

我猜这个问题是一个关键错误。用

替换您的定义
def nsyl(word):
  lowercase = word.lowercase()
  if lowercase not in d:
     return -1
  else:
     return max([len([y for y in x if isdigit(y[-1])]) for x in d[lowercase]])

相反,您可以在调用nsyl之前检查单词是否在字典中,然后您不必担心nsyl方法本身。