1
from en import verb
print verb.tenses()
print verb.infinitive('argue')


['infinitive', 'present participle', 'past plural', '2nd singular present', '2nd singular past', 'past', '3rd singular present', 'past participle', '1st singular present', '1st singular past', '3rd singular past', 'present plural']
    argue

Using this.

I can't find a method that give all tenses of the verb. There is only one way to call each function: replace space from the list using verb object. How can I accomplish this?

The input: argue. Output should be: arguing,argued,argue..

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • 1
    Do you want to conjugate the verb? If so have you seen the documentation [here](https://www.nodebox.net/code/index.php/Linguistics#verb_conjugation)? – or1426 Aug 30 '15 at 23:23
  • I want all forms of the verb. I went through the source code and couldn't a function that does the trick. The documentation doesn't mention a function like this. Please correct me if I am wrong – Abhishek Bhatia Aug 30 '15 at 23:24
  • What exactly are you trying to do that fails? – Reut Sharabani Aug 30 '15 at 23:29
  • I have to call `verb.infinitive(),verb.present_ participle...`. I am looking for a shorthand to do this looping the list strings. – Abhishek Bhatia Aug 30 '15 at 23:30
  • Can you provide sample input and it's corresponding output? Your code sample is unclear. – Nathaniel Ford Aug 30 '15 at 23:32
  • @Nathaniel Ford The input: `argue` . Output: `arguing`,`argued`,`argue`.. – Abhishek Bhatia Aug 30 '15 at 23:33
  • Please refer to http://www.sscce.org Your code doesn't compile. Are you intending to run it in a shell? Also, 'output' means both 'expected' and 'actual' output. – Nathaniel Ford Aug 30 '15 at 23:34
  • Will you have to download this package https://www.nodebox.net/code/index.php/Linguistics. It seems to work. Please check again. The actual output implies calling each of the nine functions. I am looking for better way to do this. – Abhishek Bhatia Aug 30 '15 at 23:37

2 Answers2

1

You can create a list of names / parameters for each name of the tense. For example:

tense_functions = {
    'infinitive': ('infinitive', {}),
    'present participle': ('present_participle', {}),
    '1st singular present': ('present', {'person': 1}),
    ...
}
for tense in verb.tenses():
    options = tense_functions[tense]
    func = getattr(verb, options[0])
    print(func('argue', **options[1]))
viraptor
  • 33,322
  • 10
  • 107
  • 191
  • Thanks! I am fairly new to python. Please explain more, I can't seem understand much. ` – Abhishek Bhatia Aug 30 '15 at 23:40
  • 1
    You can't do this, because the list would have to contain `2nd_singular_present` and you can't have function names starting with numbers. (well, you can, but not easily and I doubt your library exposes them) – viraptor Aug 30 '15 at 23:42
  • Yes, I just figured it out. So remove it from the comment. Sorry, my bad. Please explain the code more. – Abhishek Bhatia Aug 30 '15 at 23:44
  • I get this error: `NameError: name 'person' is not defined` – Abhishek Bhatia Aug 30 '15 at 23:45
  • 1
    Missed quotes - fixed now. – viraptor Aug 30 '15 at 23:47
  • I understand your approach, which works great. But please explain these lines `func = getattr(verb, options[0]) print(func('argue', **options[1]))`. How did you create a callable here? – Abhishek Bhatia Aug 30 '15 at 23:50
  • `**options[1]` is used how here? – Abhishek Bhatia Aug 30 '15 at 23:59
  • `**options[1]` is expanded to the keyword arguments for the function. Since some of the tenses require the `person` or other arguments and others don't, a dictionary can be used instead. Basically `f(name=value)` is the same as `f(**{'name': value})`. Have a look at https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists – viraptor Aug 31 '15 at 03:45
0

You can do getattr(verb, 'infinitive') and it will return a reference the the exact same function as verb.infinitive. You can then loop through a list of strings like this:

some_tenses = ['infinitive', 'present_participle', 'past_plural',]
for tense in some_tenses:
    print getattr(verb, tense)('argue')

Of course, the strings would have to be the exact function names in the module, whatever they are.

You might also want to look into hasattr(). If you try getattr() but the attribute you provide doesn't exist for that object, you'll get an AttributeError. Using if hasattr(... before you try getattr(... will let you handle such cases gracefully. Alternatively, you can use a try...except block.

Dan Cusher
  • 121
  • 5