语言识别的差异langid

时间:2015-03-25 11:37:33

标签: python

我有一个我读过的数据库。我想识别由列定义的特定单元格中的语言。我正在使用langid库来进行python。

我从我的数据库中读到这样的内容:

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 个答案:

答案 0 :(得分:1)

在循环row中绑定了一个带有单个项目的元组,即('tags',) - 其中'tags'代表单词列表。因此str(row)(在Python 3中)返回"('tags',)",这是传递给langid.classify()的字符串(包括单引号,逗号和大括号)。如果您使用的是Python 2,则字符串将变为"(u'tags',)"

现在,我不确定这是否解释了不同的语言检测结果,我在Python 2中的测试表明它没有,但它是数据库源数据和普通字符串数据之间的明显区别。

可能会出现一些编码问题。数据如何存储在数据库中?文本数据应采用UTF-8编码。