2013-01-23 23 views
21

Tôi muốn có chức năng thư viện python dịch/chuyển đổi qua các phần khác nhau của lời nói. đôi khi nó nên sản lượng nhiều từ (ví dụ: "coder" và "mã" là cả hai danh từ từ động từ "mã", một là đề tài khác là đối tượng)Chuyển đổi các từ giữa động từ/danh từ/dạng tính từ

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

tôi chủ yếu quan tâm đến động từ < => danh từ , cho một chương trình ghi chú tôi muốn viết. tức là tôi có thể viết "caffeine đối kháng A1" hoặc "caffeine là chất đối kháng A1" và với một số NLP, nó có thể tìm ra chúng có nghĩa là giống nhau. (Tôi biết điều đó là không dễ dàng, và rằng nó sẽ đưa NLP phân tích cú pháp và không chỉ thẻ, nhưng tôi muốn hack lên một nguyên mẫu).

câu hỏi tương tự ... Converting adjectives and adverbs to their noun forms (câu trả lời này chỉ xuất phát xuống POS gốc. Tôi muốn đi giữa POS.)

ps gọi là chuyển đổi trong ngôn ngữ học http://en.wikipedia.org/wiki/Conversion_%28linguistics%29

+1

Tôi không biết nếu nó có thể làm điều này, nhưng hãy nhìn vào các NLTK. http://nltk.org/ –

+0

Bạn sẽ xử lý các trường hợp mơ hồ như thế nào? Ví dụ, 'diguise' có thể là động từ hoặc danh từ, tùy thuộc vào ngữ cảnh. –

+0

đó là ok: 'nounize ('ngụy trang') == ['ngụy trang']' và 'verbify ('ngụy trang') == ['ngụy trang']' và 'adjectivate ('ngụy trang') == ['ngụy trang'] ' –

Trả lời

3

Tôi hiểu rằng doesn này 't trả lời toàn bộ câu hỏi của bạn, nhưng nó trả lời một phần lớn của nó. Tôi sẽ kiểm tra http://nodebox.net/code/index.php/Linguistics#verb_conjugation Thư viện trăn này có thể liên hợp động từ và nhận ra một từ là một động từ, danh từ hoặc tính từ.

VÍ DỤ MÃ

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

Nó cũng có thể phân loại từ.

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

Tải xuống ở đầu liên kết.

+0

vâng, tôi thấy rằng ở đây, nhưng nó chỉ làm những thứ bên trong (không phải trên) POS http://stackoverflow.com/questions/3753021/using-nltk-and-wordnet-how-do-i-convert-simple-tense -verb-into-its-present-past? rq = 1 –

3

Một cách tiếp cận có thể là sử dụng từ điển các từ có thẻ POS và ánh xạ từ. Nếu bạn nhận được hoặc tạo từ điển như vậy, bạn có thể sử dụng một số thông tin như sau: tất cả các từ điển liệt kê các thẻ POS của từ, cũng như các dạng cơ sở cho tất cả các biểu mẫu có nguồn gốc).

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 
12

Đây là phương pháp tiếp cận heuristic. Tôi vừa mới viết mã cho nó như vậy các ứng dụng cho phong cách. Nó sử dụng derivationally_related_forms() từ wordnet. Tôi đã thực hiện nounify. Tôi đoán verbify hoạt động tương tự. Từ công trình những gì tôi đã thử nghiệm khá tốt:

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 
2

Đây là một chức năng mà là về mặt lý thuyết có thể chuyển đổi từ giữa danh từ/động từ/tính từ/form trạng từ mà tôi cập nhật từ here (ban đầu được viết bởi bogs, tôi tin tưởng) để tuân thủ với nltk 3.2.5 hiện tại là synset.lemmassysnset.name là các chức năng.

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') 

Như bạn có thể thấy bên dưới, nó không hoạt động quá lớn. Nó không thể chuyển đổi giữa tính từ và trạng từ (mục tiêu cụ thể của tôi), nhưng nó đưa ra một số kết quả thú vị trong các trường hợp khác.

>>> 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)] 

niềm hy vọng này có thể tiết kiệm cho ai đó một chút rắc rối

Các vấn đề liên quan