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')
#!/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
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)
或许这可以给你一个粗略的指导:
您需要将输入文本解析为韵律单位,因为我怀疑“a/an”的规则是否适用于韵律边界(例如,“我们已经找到a(显然不是最佳的)解决方案。”与“我们已经找到了一个明显的解决方案”)。
下一步你需要把每个韵律单位分解成语音单词。
现在你需要确定这些词,它们代表未定义的文章(“房子”vs“a级产品”)。
一旦你确定了文章,看看你韵律单元中的下一个单词,并确定(这里是龙)这个单词第一个音位的音节特征。
如果它有[+syll],则文章应该是“an”。如果它有[-syll],那么文章应该是“a”。如果文章在韵律单元的末尾,它应该是也许是“a”(但是省略号呢:“等等,我给你一个。。。--他大声喊道,但还没来得及说出最后一句话就死了。除了abanert提到的历史例外、方言差异等。
如果找到的项目与预期不匹配,请将其标记为“不正确”。
这里有一些伪代码:
这里有一个解决方案,其中正确性定义为:
an
出现在以元音开头的单词之前,否则a
可用于:示例输入(每行一句话)
^{pr2}$输出
不清楚为什么最后一对无效,请参见Why is it “an yearly”?
这应该让你开始,但这不是一个完整的解决方案。。。。在
相关问题 更多 >
编程相关推荐