0

How to pass value inside pprint at run time?

import nltk, sys
from pprint import pprint
from nltk.corpus import framenet as fn
#Word = raw_input("enter a word: ")
pprint(fn.frames(r'(?i)Medical_specialties'))
f = fn.frame(256)
f.ID
f.name
f.definition
print f
print '\b'
pprint(sorted([x for x in f.FE]))
pprint(f.frameRelations)
print

At run time, I need to get a word from the user and pass it to fn.frames function in place of Medical_specialties, which in turn throws a list of frames as frame ID relevant to the word. Then I can call those numbers to query further.

Output:

[<frame ID=256 name=Medical_specialties>]
alexis
  • 48,685
  • 16
  • 101
  • 161
Programmer_nltk
  • 863
  • 16
  • 38

1 Answers1

1

Although you happen to be working with the nltk, your question has nothing to do with it (or with pprint, for that matter): You need to input a string from the user, then stick "(?i)" in front to construct your desired regexp.

Since that's all you need to do, the simplest solution is this:

word = raw_input("enter a word: ")
rexp = "(?i)" + word
pprint(fn.frames(rexp))

A more powerful way to put together strings is with python's C-style string formatting, or the newer format() syntax. E.g., to specify word boundaries before and after the input "word", you'd do it like this (C-style syntax):

rexp = r"(?i)\b%s\b" % word

You'll probably find the above links hard to digest, so try this exposition or this high-voted SO question.

Community
  • 1
  • 1
alexis
  • 48,685
  • 16
  • 101
  • 161