验证英文文本中“a”和“an”的正确使用-Python

2024-05-16 02:37:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个程序,从文件中读取文本,并指出“a”和“an”的使用不正确。据我所知,一般规则是,当下一个单词以元音开头时,就用“an”。但它也应该考虑到,也应该从文件中读取异常。在

有人能给我一些建议和窍门,告诉我如何开始这个。可能有帮助的功能。在

我很高兴:-)

我对Python很陌生。在


Tags: 文本程序功能an规则单词建议元音
3条回答

或许这可以给你一个粗略的指导:

  1. 您需要将输入文本解析为韵律单位,因为我怀疑“a/an”的规则是否适用于韵律边界(例如,“我们已经找到a(显然不是最佳的)解决方案。”与“我们已经找到了一个明显的解决方案”)。

  2. 下一步你需要把每个韵律单位分解成语音单词。

  3. 现在你需要确定这些词,它们代表未定义的文章(“房子”vs“a级产品”)。

  4. 一旦你确定了文章,看看你韵律单元中的下一个单词,并确定(这里是龙)这个单词第一个音位的音节特征。

  5. 如果它有[+syll],则文章应该是“an”。如果它有[-syll],那么文章应该是“a”。如果文章在韵律单元的末尾,它应该是也许是“a”(但是省略号呢:“等等,我给你一个。。。--他大声喊道,但还没来得及说出最后一句话就死了。除了abanert提到的历史例外、方言差异等。

  6. 如果找到的项目与预期不匹配,请将其标记为“不正确”。


这里有一些伪代码:

def parseProsodicUnits(text): #here be dragons
def parsePhonologicalWords(unit): #here be dragons
def isUndefinedArticle(word): #here be dragons
def parsePhonemes(word): #here be dragons
def getFeatures(phoneme): #here be dragons

for unit in parseProsodicUnits(text):
    for idx, word in enumerate (parsePhonologicalWords(unit)[:-1]):
        if not isUndefinedArticle(word): continue
        syllabic = '+syll' in getFeatures(parsePhonemes(unit[idx+1])[0])
        if (word == 'a' and syllabic) or (word == 'an' and not syllabic):
            print ('incorrect')

这里有一个解决方案,其中正确性定义为:an出现在以元音开头的单词之前,否则a可用于

#!/usr/bin/env python
import itertools
import re
import sys

try:
    from future_builtins import map, zip
except ImportError: # Python 3 (or old Python versions)
    map, zip = map, zip
from operator import methodcaller

import nltk  # $ pip install nltk
from nltk.corpus import cmudict  # >>> nltk.download('cmudict')

def starts_with_vowel_sound(word, pronunciations=cmudict.dict()):
    for syllables in pronunciations.get(word, []):
        return syllables[0][-1].isdigit()  # use only the first one

def check_a_an_usage(words):
    # iterate over words pairwise (recipe from itertools)
    #note: ignore Unicode case-folding (`.casefold()`)
    a, b = itertools.tee(map(methodcaller('lower'), words)) 
    next(b, None)
    for a, w in zip(a, b):
        if (a == 'a' or a == 'an') and re.match('\w+$', w): 
            valid = (a == 'an') if starts_with_vowel_sound(w) else (a == 'a')
            yield valid, a, w

#note: you could use nltk to split text in paragraphs,sentences, words
pairs = ((a, w)
         for sentence in sys.stdin.readlines() if sentence.strip() 
         for valid, a, w in check_a_an_usage(nltk.wordpunct_tokenize(sentence))
         if not valid)

print("Invalid indefinite article usage:")
print('\n'.join(map(" ".join, pairs)))

示例输入(每行一句话)

^{pr2}$

输出

Invalid indefinite article usage:
a acre
an rhythm
an yearly

不清楚为什么最后一对无效,请参见Why is it “an yearly”?

all_words = "this is an wonderful life".split()
for i in range(len(all_words)):
    if all_words[i].lower() in ["a","an"]:
       if all_words[i+1][0].lower() in "aeiou":
           all_words[i] = all_words[i][0]+"n"
       else:
           all_words[i] = all_words[i][0]
print " ".join(all_words)

这应该让你开始,但这不是一个完整的解决方案。。。。在

相关问题 更多 >