2013-02-18 62 views
1

我正在做一個簡單的命令行的Python程序一整行,並確認與一個拼寫檢查用戶的輸入。我在之前發現了http://norvig.com/spell-correct.html,並且正在使用它來驗證我的用戶將輸入的內容。就我而言,我正在根據BART站列表驗證用戶輸入。用戶必須準確輸入電臺名稱,或從拼寫檢查器獲取建議。以下是我正在驗證的BART電臺列表。搜索用蟒正則表達式

Lake Merritt 
Daly City 
Fruitvale 
Coliseum/Oakland Airport 
San Leandro 
. 
. 
. 

我在做什麼和示例代碼,我發現,是我對驗證多個words-「達利城」,而不是僅僅之間的區別「Fruitvale。」我用正則表達式和python不太好,我試圖弄清楚如何讀取每一行,並檢查從行首到結尾的所有內容。所以我很難找出正則表達式來讀取一行中的所有內容。以下是我正在試圖改變代碼:

def words(text): return re.findall('[a-z]+', text.lower()) 

其中文本是

我試圖

def words(text): 
    lines=text.split('\n') 
    return re.search('[a-z]+', lines) 

思路,將工作,因爲該示例中使用的大的文本文件(以我至少)意味着我正在搜索每行至少一個字符串中的小寫字符。不過,我得到了這回

Traceback (most recent call last): 
File "spell.py", line 15, in <module> 
NWORDS = train(words(file('stations.txt').read())) 
File "spell.py", line 6, in words 
return re.search('[a-z]+', lines) 
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py",  line 142, in search 
return _compile(pattern, flags).search(string) 
TypeError: expected string or buffer 

我真的不知道如何做到這一點。誰能幫忙?

+0

什麼're.findall( '[AZ] +'' – 2013-02-18 02:02:10

+2

在你給第二種情況下're.search'列表 – placeybordeaux 2013-02-18 02:02:14

+0

正則表達式只能在一行文本中工作,所以你要麼使用'map'或'for'循環來遍歷整個列表,然後可以返回所有列表的另一個列表匹配 – supercheetah 2013-02-18 02:09:23

回答

1

也許使用difflib,而不是弱勢族羣的拼寫校正。 difflib具有get_close_matches功能,它可以幫助您猜測BART站中哪個字符串與用戶輸入的字符串最接近。例如,

import difflib 

bart_stations = ['Lake Merritt', 'Daly City', 'Fruitvale', 'Coliseum/Oakland Airport', 
       'San Leandro'] 

while True: 
    text = raw_input('Enter BART station: ') 
    if not text: break # Pressing Enter quits 
    guess = difflib.get_close_matches(text, bart_stations, n=1, cutoff=0)[0] 
    print('Closest match: {g}'.format(g = guess)) 

運行腳本產量:

% test.py 
Enter BART station: Merit 
Closest match: Lake Merritt 
Enter BART station: Fruity 
Closest match: Fruitvale 
Enter BART station: Coli 
Closest match: Daly City 
Enter BART station: Col 
Closest match: Coliseum/Oakland Airport 
Enter BART station: Lean 
Closest match: San Leandro 
Enter BART station: 
+0

謝謝,這似乎工作,是一個非常簡單,直接的解決方案。 – 2013-02-18 02:51:51