web-dev-qa-db-ja.com

動詞/名詞/形容詞形式の間で単語を変換する

python異なる品詞に翻訳/変換するライブラリ関数が必要です。場合によっては複数の単語を出力する必要があります(例: "coder"と "code"は両方とも "to code"からの名詞です) "、一方が主体、もう一方が目的)

# :: String => List of String
print verbify('writer') # => ['write']
print nounize('written') # => ['writer']
print adjectivate('write') # => ['written']

私は主に動詞<=>名詞を気にします。私が書きたいメモ取りプログラムの場合。つまり、「カフェインはA1に拮抗する」または「カフェインはA1拮抗薬である」と書くことができ、一部のNLPでは、それらが同じことを意味することがわかります。 (私はそれが簡単ではないことを知っています、そしてそれは解析してタグ付けするだけでなく、プロトタイプを作り上げたいNLPを必要とします).

同様の質問... 形容詞と副詞を名詞形に変換する (この回答はルートPOSにのみ達します。POS間を行きたいです。)

言語学の変換と呼ばれるps http://en.wikipedia.org/wiki/Conversion_%28linguistics%29

27
sam boosalis

これは、よりヒューリスティックなアプローチです。私はそれをコーディングしたので、スタイルの謝罪をします。 wordnetのderivationally_related_forms()を使用します。 nounifyを実装しました。 verbifyは同様に機能すると思います。私がテストしたものからかなりうまくいきます:

from nltk.corpus import wordnet as wn

def nounify(verb_Word):
    """ Transform a verb to the closest noun: die -> death """
    verb_synsets = wn.synsets(verb_Word, pos="v")

    # Word not found
    if not verb_synsets:
        return []

    # Get all verb lemmas of the Word
    verb_lemmas = [l for s in verb_synsets \
                   for l in s.lemmas if s.name.split('.')[1] == 'v']

    # Get related forms
    derivationally_related_forms = [(l, l.derivationally_related_forms()) \
                                    for l in    verb_lemmas]

    # filter only the nouns
    related_noun_lemmas = [l for drf in derivationally_related_forms \
                           for l in drf[1] if l.synset.name.split('.')[1] == 'n']

    # Extract the words from the lemmas
    words = [l.name for l in related_noun_lemmas]
    len_words = len(words)

    # Build the result in the form of a list containing tuples (Word, probability)
    result = [(w, float(words.count(w))/len_words) for w in set(words)]
    result.sort(key=lambda w: -w[1])

    # return all the possibilities sorted by probability
    return result
15
bogs

これは理論的には、私が here から更新した名詞/動詞/形容詞/副詞形式の間で単語を変換できる関数です(元は bogs 、私は信じています)nltk 3.2.5に準拠するようになりましたsynset.lemmasおよびsysnset.nameは関数です。

from nltk.corpus import wordnet as wn

# Just to make it a bit more readable
WN_NOUN = 'n'
WN_VERB = 'v'
WN_ADJECTIVE = 'a'
WN_ADJECTIVE_SATELLITE = 's'
WN_ADVERB = 'r'


def convert(Word, from_pos, to_pos):    
    """ Transform words given from/to POS tags """

    synsets = wn.synsets(Word, pos=from_pos)

    # Word not found
    if not synsets:
        return []

    # Get all lemmas of the Word (consider 'a'and 's' equivalent)
    lemmas = []
    for s in synsets:
        for l in s.lemmas():
            if s.name().split('.')[1] == from_pos or from_pos in (WN_ADJECTIVE, WN_ADJECTIVE_SATELLITE) and s.name().split('.')[1] in (WN_ADJECTIVE, WN_ADJECTIVE_SATELLITE):
                lemmas += [l]

    # Get related forms
    derivationally_related_forms = [(l, l.derivationally_related_forms()) for l in lemmas]

    # filter only the desired pos (consider 'a' and 's' equivalent)
    related_noun_lemmas = []

    for drf in derivationally_related_forms:
        for l in drf[1]:
            if l.synset().name().split('.')[1] == to_pos or to_pos in (WN_ADJECTIVE, WN_ADJECTIVE_SATELLITE) and l.synset().name().split('.')[1] in (WN_ADJECTIVE, WN_ADJECTIVE_SATELLITE):
                related_noun_lemmas += [l]

    # Extract the words from the lemmas
    words = [l.name() for l in related_noun_lemmas]
    len_words = len(words)

    # Build the result in the form of a list containing tuples (Word, probability)
    result = [(w, float(words.count(w)) / len_words) for w in set(words)]
    result.sort(key=lambda w:-w[1])

    # return all the possibilities sorted by probability
    return result


convert('direct', 'a', 'r')
convert('direct', 'a', 'n')
convert('quick', 'a', 'r')
convert('quickly', 'r', 'a')
convert('hunger', 'n', 'v')
convert('run', 'v', 'a')
convert('tired', 'a', 'r')
convert('tired', 'a', 'v')
convert('tired', 'a', 'n')
convert('tired', 'a', 's')
convert('wonder', 'v', 'n')
convert('wonder', 'n', 'a')

以下に示すように、それほどうまく機能しません。形容詞と副詞の形(私の具体的な目標)を切り替えることはできませんが、他の場合には興味深い結果が得られます。

>>> convert('direct', 'a', 'r')
[]
>>> convert('direct', 'a', 'n')
[('directness', 0.6666666666666666), ('line', 0.3333333333333333)]
>>> convert('quick', 'a', 'r')
[]
>>> convert('quickly', 'r', 'a')
[]
>>> convert('hunger', 'n', 'v')
[('hunger', 0.75), ('thirst', 0.25)]
>>> convert('run', 'v', 'a')
[('persistent', 0.16666666666666666), ('executive', 0.16666666666666666), ('operative', 0.16666666666666666), ('prevalent', 0.16666666666666666), ('meltable', 0.16666666666666666), ('operant', 0.16666666666666666)]
>>> convert('tired', 'a', 'r')
[]
>>> convert('tired', 'a', 'v')
[]
>>> convert('tired', 'a', 'n')
[('triteness', 0.25), ('banality', 0.25), ('tiredness', 0.25), ('commonplace', 0.25)]
>>> convert('tired', 'a', 's')
[]
>>> convert('wonder', 'v', 'n')
[('wonder', 0.3333333333333333), ('wonderer', 0.2222222222222222), ('marveller', 0.1111111111111111), ('marvel', 0.1111111111111111), ('wonderment', 0.1111111111111111), ('question', 0.1111111111111111)]
>>> convert('wonder', 'n', 'a')
[('curious', 0.4), ('wondrous', 0.2), ('marvelous', 0.2), ('marvellous', 0.2)]

これが誰かのちょっとしたトラブルを救えることを願っています

4
stuart

1つのアプローチは、POSタグとwordformsマッピングで単語の辞書を使用することです。そのようなディクショナリを取得または作成する場合(すべてのディクショナリがWordのPOSタグとすべての派生フォームの基本フォームをリストしているため、従来のディクショナリのデータにアクセスできる場合は非常に可能です)、次のようなものを使用できます。

def is_verb(Word):
    if Word:
        tags = pos_tags(Word)
        return 'VB' in tags or 'VBP' in tags or 'VBZ' in tags \
               or 'VBD' in tags or 'VBN' in tags:

def verbify(Word):
    if is_verb(Word):
        return Word
    else:
       forms = []
       for tag in pos_tags(Word):
           base = Word_form(Word, tag[:2])
           if is_verb(base):
              forms.append(base)
       return forms
3

これはあなたの質問全体に答えるものではないことを理解していますが、それはその大部分に答えます。私はチェックアウトします http://nodebox.net/code/index.php/Linguistics#verb_conjugation これはpythonライブラリは動詞を活用でき、単語は動詞、名詞、または形容詞です。

コード例

print en.verb.present("gave")
print en.verb.present("gave", person=3, negate=False)
>>> give
>>> gives

単語を分類することもできます。

print en.is_noun("banana")
>>> True

ダウンロードはリンクの上部にあります。

3
xxmbabanexx