从单词中删除结尾元音

时间:2018-05-17 15:28:19

标签: python python-3.x

此函数接收一个字符串作为输入,并应返回字符串中的音节数。

此功能具有以下条件:
1.音节数等于元音数量
2.两个或多个连续元音仅计为一个元素 3.单词末尾的一个或多个元音不计算在内。

这是我迄今为止所做的,但显然我仍然遗失了很多。我不确定如何继续这里,所以我希望你们能帮忙。

df
  .withColumn("prefix", $"user".substr(0,2)) // Add prefix column
  .write.mode("overwrite")            
  .partitionBy("prefix")                     // Use it for partitioning 
  .parquet("path/to/location")

4 个答案:

答案 0 :(得分:2)

我建议您使用以下简单代码:

def syllables(word):

    vowels = ['a', 'e', 'i', 'o', 'u', 'y']

    N = 0
    previousLetterIsAVowel = False

    # Perform a loop on each letter of the word
    for i in word.lower():
        if i in vowels:
            # Here it is a vowel
            # Indicate for the next letter that it is preceded by a vowel
            # (Don't count it now as a syllab, because it could belong to a group a vowels ending the word)
            previousLetterIsAVowel = True
        else:
            # Here: it is not a vowel
            if previousLetterIsAVowel:
                # Here it is preceded by a vowel, so it ends a group a vowels, which is considered as a syllab
                N += 1
            # Indicate for the next letter that it is not preceded by a vowel
            previousLetterIsAVowel = False

    return N

print(syllables("bureau"))      # it prints 1
print(syllables("papier"))      # it prints 2
print(syllables("ordinateur"))  # it prints 4
print(syllables("India"))       # it prints 1

我还提供了使用正则表达式的单行样式解决方案,如果您对正则表达式有所了解,也很容易阅读。它只是计算辅音后面的元音组的数量:

import re

def syllables(word):
    return len(re.findall('[aeiouy]+[bcdfghjklmnpqrstvwxz]', word.lower()))

答案 1 :(得分:0)

要检查最后一个元音,你可以尝试这样的事情(我不会因为你要放松整个音节而迭代): - > EX:意大利语" Aia" (脱粒场)

if word[-1] in vocals:  
 word=word[:-1]  

- 抱歉,但我没有设法将代码放入'进入评论所以发布了答案

答案 2 :(得分:0)

我会选择:

def syllables(word):
    def isVowel(c):
        return c.lower() in ['a','e','i','o','u','y']

    # Delete ending vowel of the word since they don't count in number of syllables  
    while word and isVowel(word[-1]):
        word = word[:-1]

    lastWasVowel = False
    counter = 0

    for c in word:
        isV = isVowel(c)
        # skip multiple vowels after another
        if lastWasVowel and isV:
            continue
        if isV:
            # found one
            counter += 1
            lastWasVowel = True
        else:
            # reset vowel memory
            lastWasVowel = False


    return counter

LaurentH被盗:

print(syllables("bureau"))      # prints 1
print(syllables("papier"))      # prints 2
print(syllables("ordinateur"))  # prints 4
print(syllables("I"))           # prints 0

答案 3 :(得分:0)

我认为如果有previousLetterIsAVowel并且N返回0,我们返回1.示例单词Bee。

除了Laurent H.回答

if N == 0 and previousLetterIsAVowel:
    return 1
else:
    return N