Змея / Говнокод #27373 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
def generate_words(sample, phonemes, num=10): 
    global words, yd
    gen = True
    parens = 0
    r = random.random()
    word = ""
    i = 0
    while i < int(num):
        for j in range(0, len(sample)):
            if sample[j] == '(':
                if gen:
                    gen = (random.randint(0,1) == 0)
                if not gen:
                    parens += 1
            elif sample[j] == ')':
                if not gen:
                    parens -= 1
                if parens == 0:
                    gen = True
            elif sample[j] in phonemes.keys():
                for n, phtype in enumerate(phonemes.keys()):
                    if gen and phtype == sample[j]:
                        #k = random.choice(phonemes[phtype])
                        n = yd.randomGen(phonemeRanks[phtype])
                        k = phonemes[phtype][n]
                        word += k
            elif gen:
                word += sample[j]
                    
        if (not word in words) and (not isExceptional(word)):
            words.append(word)

        i += 1
        word = ""

# ...

"""parsing sound changes rule with the notation X>Y/Z, where X is a set of source phonemes
Y - a set of resulting phonemes, Z - a positional condition (optional)"""
def parsePhNotation(inline):
    rule = inline.split('>')
    if (not len(rule) == 2):
        return []

    source = rule[0].split(',')
    final = rule[1].split('/')

    result = final[0].split(',')
    posCond = []
    rule = []
    if (len(final) > 2):
        return False
    elif (len(final) == 2):
        posCond = final[1].split('_')
        if (not len(posCond) == 2):
            return []
        posCond[0] = posCond[0].split('#')
        posCond[1] = posCond[1].split('#')


        
        if (len(posCond[0]) == 2) and len(posCond[0][0]) > 0:
            return []
        elif len(posCond[0]) == 2:
            rule.append(" "+posCond[0][1])
        else:
            rule.append(posCond[0][0])

        if (len(posCond[1]) == 2) and len(posCond[1][1]) > 0:
            return []

        rule.append(posCond[1][0])
        if len(posCond[1]) == 2:
            rule[1] += " "
        
        rule[0] = rule[0].split(",")
        rule[1] = rule[1].split(",")

    final = []
    if len(source) > len(result):
        for i in range(len(result)-1, len(source)-1):
            result.append("")
    elif len(source) < len(result):
        for i in range(len(source)-1, len(result)-1):
            source.append("")

    final.append(source)
    final.append(result)
    if (len(rule)>0):
        final.append(rule)
    return final

Рекурсивный спуск, автомат с магазинной памятью, top-down parsing, bottom-up parsing? Не, не слышал. В свое время время делал вот такие самопальные алгоритмы для рандомной генерации слов по фонетическим шаблонам (типа "CV(C)", "VC" и т.д.) и фонетического преобразования слов и при попытке починить чета наступал на новые баги, т.к. не до конца понимал как вообще парсятся компьютерные языки.

Запостил: JaneBurt JaneBurt, (Updated )

Комментарии (1) RSS

Добавить комментарий

Семь раз отмерь — один отрежь, guest!

    А не использовать ли нам bbcode?


    8