如何检测文件中的给定行是否是正确的英语句子?

时间:2011-05-08 13:21:45

标签: python nlp

我需要检测文件中给定的“行”是否是英语句子。我正在使用Python。一个近似的答案会做。我知道这是一个NLP问题,但有一个轻量级的工具,给出一个合理的近似值?我不想使用完整的NLP工具包,但如果这是唯一的方法,那就没问题了。

如果NLP工具包是答案,那么我正在阅读的是Natural Language Toolkit。如果有人有一个关于如何检测句子的简单示例,请指出我。

5 个答案:

答案 0 :(得分:4)

有一个名为NLTK的python库,用于处理自然语言。

还有language-detection-with-python-nltk描述了将其用于检测输入文本语言的过程。

这应该有希望成功。

答案 1 :(得分:2)

也许您正在寻找nltk library中的Punkt Tokenizer,它可以为您提供给定文字中的英语句子。然后,您可以通过grammar检查(由Acron指出)

来对句子采取行动

答案 2 :(得分:1)

目前,计算机软件无法告诉您给定的一系列令牌是否是具有任何合理程度可靠性的语法英语句子。但是,您可能会关注亚马逊的Mechanical Turk。如果你将这个句子提交给五个以英语为母语的人,并且大多数人都说这是一个语法,你可以假设它具有合理的确定性。

当然,虽然Mechanical Turk确实有一个Web Services API,因此可以在Python中使用,但它不是实时的。

答案 3 :(得分:0)

所以你基本上想要在线上进行语法检查?

可能this可以使用吗?

答案 4 :(得分: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)]

您应该非常小心地选择最适合您需求的培训数据。只是为了给你一个想法,我从stop wordsEnglishPortuguese收集了一些Spanish