如何检测文件中的某一行是否为正确的英语句子?
我需要判断一个文件中的“行”是否是英文句子。我正在使用Python。大致的答案就可以了。我知道这是一个自然语言处理(NLP)的问题,但有没有什么轻量级的工具可以给出一个合理的近似结果?我不想使用一个完整的NLP工具包,不过如果这是唯一的办法,那也没关系。
如果NLP工具包是答案的话,我正在了解的是自然语言工具包。如果有人有简单的例子可以教我怎么判断一个句子,请告诉我。
3 个回答
0
你可以使用Python的Reverend库。这个库的代码不到400行,挺简单的。你可以看看怎么使用它:
>>> from thomas import Bayes
>>> guesser = Bayes()
>>> guesser.train("en" u"a about above after again against all am an and any are aren't as at be because been before being below between both but by can't cannot could couldn't did didn't do does doesn't doing don't down during each few for from further had hadn't has hasn't have haven't having he he'd he'll he's her here here's hers herself him himself his how how's i i'd i'll i'm i've if in into is isn't it it's its itself let's me more most mustn't my myself no nor not of off on once only or other ought our ours ourselves out over own same shan't she she'd she'll she's should shouldn't so some such than that that's the their theirs them themselves then there there's these they they'd they'll they're they've this those through to too under until up very was wasn't we we'd we'll we're we've were weren't what what's when when's where where's which while who who's whom why why's with won't would wouldn't you you'd you'll you're you've your yours yourself yourselves")
>>> guesser.train("pt" u"último é acerca agora algmas alguns ali ambos antes apontar aquela aquelas aquele aqueles aqui atrás bem bom cada caminho cima com como comprido conhecido corrente das debaixo dentro desde desligado deve devem deverá direita diz dizer dois dos e ela ele eles em enquanto então está estão estado estar estará este estes esteve estive estivemos estiveram eu fará faz fazer fazia fez fim foi fora horas iniciar inicio ir irá ista iste isto ligado maioria maiorias mais mas mesmo meu muito muitos nós não nome nosso novo o onde os ou outro para parte pegar pelo pessoas pode poderá podia por porque povo promeiro quê qual qualquer quando quem quieto são saber sem ser seu somente têm tal também tem tempo tenho tentar tentaram tente tentei teu teve tipo tive todos trabalhar trabalho tu um uma umas uns usa usar valor veja ver verdade verdadeiro você")
>>> guesser.train("es" u"un una unas unos uno sobre todo también tras otro algún alguno alguna algunos algunas ser es soy eres somos sois estoy esta estamos estais estan como en para atras porque por qué estado estaba ante antes siendo ambos pero por poder puede puedo podemos podeis pueden fui fue fuimos fueron hacer hago hace hacemos haceis hacen cada fin incluso primero desde conseguir consigo consigue consigues conseguimos consiguen ir voy va vamos vais van vaya gueno ha tener tengo tiene tenemos teneis tienen el la lo las los su aqui mio tuyo ellos ellas nos nosotros vosotros vosotras si dentro solo solamente saber sabes sabe sabemos sabeis saben ultimo largo bastante haces muchos aquellos aquellas sus entonces tiempo verdad verdadero verdadera cierto ciertos cierta ciertas intentar intento intenta intentas intentamos intentais intentan dos bajo arriba encima usar uso usas usa usamos usais usan emplear empleo empleas emplean ampleamos empleais valor muy era eras eramos eran modo bien cual cuando donde mientras quien con entre sin trabajo trabajar trabajas trabaja trabajamos trabajais trabajan podria podrias podriamos podrian podriais yo aquel")
>>> guesser.guess(u'what language am i speaking')
>>> [('en', 0.99990000000000001)]
>>> guesser.guess(u'que língua eu estou falando')
>>> [('pt', 0.99990000000000001)]
>>> guesser.guess(u'en qué idioma estoy hablando')
>>> [('es', 0.99990000000000001)]
在选择最适合你需求的训练数据时,你需要非常小心。为了给你一个概念,我收集了一些停用词,这些词在英语、葡萄牙语和西班牙语中都是常见的。
1
目前,计算机软件无法可靠地判断一串词语是否是符合语法的英语句子。不过,你可以考虑使用亚马逊的Mechanical Turk服务。如果你把这个句子给五个以英语为母语的人看,如果大多数人认为这个句子是正确的,那么你可以比较有把握地认为它是对的。
当然,虽然Mechanical Turk提供了网络服务接口,可以通过Python来使用,但它的反馈并不是实时的。