Не помню уже для чего понадобилось мне получать все склонения слова без повторений. Была написана простейшая программа, которой на вход подается файл со словами для склонения, разделенных символом новой строки, и на выходе файл со всеми склонениями исходных слов. В качестве склонятора выбрал веб-сервис яндекса, который можно найти по адресу: http://nano.yandex.ru/project/inflect/. Быть может кому-нибудь пригодится.

Собственно, код:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
import urllib2, re, sys, getopt

def getInflections(xmlpage):
    words = re.findall('(.*)', xmlpage)
    return words

def infs2file(file, newfile=None):
    try:
        infile = open(file)
    except IOError:
        return False
    url = "http://export.yandex.ru/inflect.xml?name="
    if newfile == None:
        outfile = open("infs_"+file, "w")
    else:
        outfile = open(newfile, "w")
    sourceWords = infile.readlines()
    for word in sourceWords:
        page = urllib2.urlopen(url+word).read()
        #получаем список слов из xml
        newWords = getInflections(page)
        #удаляем дубликаты из списка
        newWords = list(set(newWords))
        #пишем в файл
        for inflection in newWords:
            print>>outfile, inflection
    return True

def usage():
    print """Infs2File - automatic get words inflections through Yandex Nano
Inflector Project.
Usage: infs2file [[KEY] FILE]
Keys:
    -h, --help            print this message
    -o, --outputfile      new file with inflections of words from source file.
Default name of output file is infl_FILE
"""

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "outputfile="])
    except getopt.GetoptError, err:
        print str(err) # will print something like "option -a not recognized"
        Usage()
        sys.exit(2)

    try:
        inputfile = args[0]
    except IndexError, err:
        Usage()
        sys.exit(2)
    outputfile = None

    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-o", "--outputfile"):
            outputfile = a
        else:
            assert False, "unhandled option"

    if infs2file(inputfile, outputfile):
        print "Succesfully done!"
    else:
        print "Somewhere error :-)"

if __name__ == "__main__":
    main()