2015-03-25 49 views
0

我有一個我讀過的數據庫。我想確定列中定義的特定單元格中的語言。我正在使用python的langid庫。語言識別中的差異langid

我從數據庫中讀取這樣的:

connector = sqlite3.connect("somedb.db") 
selecter = connector.cursor() 
selecter.execute(''' SELECT tags FROM sometable''') 
for row in selecter: #iterate through all the rows in db 
    #print (type(row)) #tuple 
    rf = str(row) 
    #print (type(rf)) #string 
    lan = langid.classify("{}".format(rf)) 

從技術上講,它的工作原理。它標識所使用的語言,稍後(此處未顯示)將識別的語言寫回到數據庫中。

所以,現在出現了怪異的部分。 我想手動仔細檢查一些結果。所以,我有這樣的話:

a = "shadow party people bw music mer white man black france men art nature monochrome french fun shoe sand nikon europe noir noiretblanc sable playa poetic nb ombre shade contraste plage blanc saxophone dunkerque nord homme musique saxo artiste artistique musicien chaussure blancandwhite d90 saxophoniste zyudcoote" 

當我執行的database語言識別它繪出我的葡萄牙到數據庫中。 但是,像這樣進行的:

a = "shadow party people bw music mer white man black france men art nature monochrome french fun shoe sand nikon europe noir noiretblanc sable playa poetic nb ombre shade contraste plage blanc saxophone dunkerque nord homme musique saxo artiste artistique musicien chaussure blancandwhite d90 saxophoniste zyudcoote" 
lan = langid.classify(a) 

好了,我返回法國。除此之外,它既不是法語也不是葡萄牙語,爲什麼它返回不同的結果?!

回答

1

在循環row綁定到元組與單個項,即('tags',) - ,其中'tags'代表的單詞列表。因此(在Python 3中)返回"('tags',)",它就是傳遞給langid.classify()的這個字符串(包括單引號,逗號和大括號)。如果您使用的是Python 2,則字符串將變爲"(u'tags',)"

現在,我不確定這是否解釋了不同的語言檢測結果,並且我在Python 2中的測試顯示它沒有,但它是數據庫源數據和普通字符串數據之間的明顯區別。

可能會出現一些編碼問題。數據如何存儲在數據庫中?文本數據應該是UTF-8編碼的。

+0

嘿,我在Python 3.3中工作,做'str(row)'不會返回'(tags),'。我遍歷一個數據庫,每行得到不同的結果。如果每一行都是'(標籤)',那麼每一行的語言應該是相同的。 我想我也在看編碼問題。我用'FIELD NAME,TEXT'創建了這個字段,默認情況下,這個編碼從SQLite設置爲'UTF-8'。我收到的數據是來自flickr API的屏幕截圖,但也根據他們的文檔'UTF-8'。 – Stophface 2015-03-26 07:13:54

+0

通過'tags'我的意思是標籤字段的內容...不是字面字符串'標籤'。你正在傳遞一個字符串化的元組來分類。我懷疑這個問題是由這個問題引起的,但是把它作爲字符串傳遞是錯誤的,你應該修正這個問題以消除它作爲問題。 – mhawke 2015-03-26 07:28:33

+0

誤會。我現在通過它:'a =''.join(row)'。更好? – Stophface 2015-03-26 09:07:19