2014-02-07 2 views
3

나는 문장의 목록을 가지고 있으며, 기본적으로 내 목표는 "opp, nr, off, abv, behnd"형식의 전치사를 올바른 철자법으로 " 위, 뒤에 "등등. 단어의 soundex 코드는 동일하므로 단어별로이 목록 단어를 반복하는 표현식을 작성해야하며 soundex가 동일한 경우 올바른 철자로 바꾸십시오.Soundex, python을 사용하여 단어 바꾸기

예 - [
는 '이동 주유소 twrds',
'그 계획들이 ABV 된 모든'
는 '그냥 OPP 카운터 스탠드를', '잭 트리 NR 서 있었다']

그래서 단어 nr, abv, opp 및 twrds를 올바른 형식으로 대체해야합니다. 방향과 twrds의 soundex 코드는 동일하므로 교체해야합니다.
내가
여기 사운 덱스 알고리즘입니다 ..이 목록을 반복해야합니다

import string 

allChar = string.uppercase + string.lowercase 
charToSoundex = string.maketrans(allChar, "91239129922455912623919292" * 2) 

def soundex(source): 
    "convert string to Soundex equivalent" 

    # Soundex requirements: 
    # source string must be at least 1 character 
    # and must consist entirely of letters 
    if (not source) or (not source.isalpha()): 
    return "0000" 

    # Soundex algorithm: 
    # 1. make first character uppercase 
    # 2. translate all other characters to Soundex digits 
    digits = source[0].upper() + source[1:].translate(charToSoundex) 

    # 3. remove consecutive duplicates 
    digits2 = digits[0] 
    for d in digits[1:]: 
     if digits2[-1] != d: 
      digits2 += d 

    # 4. remove all "9"s 
    # 5. pad end with "0"s to 4 characters 
    return (digits2.replace('9', '') + '000')[:4] 

if __name__ == '__main__': 
    import sys 
    if sys.argv[1:]: 
     print soundex(sys.argv[1]) 
    else: 
    from timeit import Timer 
    names = ('Woo', 'Pilgrim', 'Flingjingwaller') 
    for name in names: 
     statement = "soundex('%s')" % name 
     t = Timer(statement, "from __main__ import soundex") 
     print name.ljust(15), soundex(name), min(t.repeat()) 

당신이 제안 수있는 또 다른 방법이있을 경우, 그래서 그것을 감사하겠습니다, 초보자입니다 .. 감사합니다.

+0

당신이 당신의 들여 쓰기를 해결할 수 있을까요? – Bach

+0

고정 :). 또한 올바른 필자로 구성된 파일을 만들어야합니까 ?? –

+0

그건 수정되지 않았습니다. 'def'에서'return'에 이르기까지 들여 쓰기가되어야합니다. – Bach

답변

0

내가 인챈트 모듈을 사용합니다 :

import enchant 
d = enchant.Dict("en_US") 

phrase = ['Jack was standing nr the tree' , 
'they were abv everything he planned' , 
'Just stand opp the counter' , 
'Go twrds the gas station'] 

output = [] 
for section in phrase: 
    sect = '' 
    for word in section.split(): 
     if d.check(word): 
      sect += word + ' ' 
     else: 
      for correct_word in d.suggest(word): 
       if soundex(correct_word) == soundex(word): 
        sect += correct_word + ' ' 
    output.append(sect[:-1]) 
관련 문제