File size: 4,815 Bytes
44d950f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import gradio as gr
from pinyin_to_ipa import pinyin_to_ipa

# ri has 4 conversions


def run() -> None:
  interface = build_interface()
  interface.queue()
  interface.launch(
    share=False,
    debug=True,
    inbrowser=True,
    quiet=False,
    show_api=False,
  )


def build_interface() -> gr.Blocks:
  with gr.Blocks(
      title="pinyin-to-ipa"
  ) as web_app:
    gr.Markdown(
      """
      # Pinyin to IPA conversion
      """
    )

    with gr.Tab("Conversion"):
      with gr.Row():
        with gr.Column():
          with gr.Group():
            input_txt_box = gr.Textbox(
              None,
              label="Pinyin syllable",
              placeholder="Enter the pinyin syllable you want to convert to IPA.",
              lines=1,
              max_lines=1,
            )

          convert_btn = gr.Button("Convert", variant="primary")

        with gr.Column():
          with gr.Group():
            with gr.Row():
              with gr.Column():
                output_txt_box = gr.Textbox(
                  show_label=True,
                  label="IPA",
                  interactive=False,
                  show_copy_button=True,
                  placeholder="The IPA transcription of the input will be displayed here.",
                  lines=1,
                  max_lines=4,  # ri
                )

      with gr.Row():
        gr.Examples(
            examples=[
              ["pang1"],
              ["pang2"],
              ["pang3"],
              ["pang4"],
              ["pang5"],
              ["pang"],
              ["hàng"],
              ["ang"],  # no initial
              ["hng"],  # SYLLABIC_CONSONANT_MAPPINGS
              ["ê"],  # INTERJECTION_MAPPINGS
              ["io"],  # INTERJECTION_MAPPINGS
              ["er"],  # INTERJECTION_MAPPINGS
              ["zhi"],  # FINAL_MAPPING_AFTER_ZH_CH_SH_R
              ["zi"],  # FINAL_MAPPING_AFTER_Z_C_S
            ],
            fn=synt,
            inputs=[
              input_txt_box,
            ],
            outputs=[
              output_txt_box,
            ],
            label="Examples",
            cache_examples=False,
            examples_per_page=20,
        )

    with gr.Tab("Info"):
      with gr.Column():
        gr.Markdown(
          """

          ### Phoneme Set

          - Vowels: a ɛ e ə ɚ ɤ i o ɔ u ʊ y
          - Diphthongs: ai̯ au̯ aɚ̯ ei̯ ou̯
          - Consonants: f h j k kʰ l m n p pʰ ɹ̩¹ ɻ¹ ɻ̩¹ s t ts tsʰ tɕ tɕʰ tʰ w x ŋ ɕ ɥ ʂ ʈʂ ʈʂʰ z̩¹ ʐ¹ ʐ̩¹

          Vowels and diphthongs contain one of these tones:

          - first tone: ˥
          - second tone: ˧˥
          - third tone: ˧˩˧
          - fourth tone: ˥˩
          - fifth tone: (nothing)

          ¹ These consonants contain also tones.

          ### Citation

          Taubert, S. (2024). pinyin-to-ipa (Version 0.0.2) [Computer software]. https://doi.org/10.5281/zenodo.10639971

          ### References

          - [https://en.wikipedia.org/wiki/Help:IPA/Mandarin](https://en.wikipedia.org/wiki/Help:IPA/Mandarin)
          - [https://en.wikipedia.org/wiki/Standard_Chinese_phonology](https://en.wikipedia.org/wiki/Standard_Chinese_phonology)
          - [https://en.wikipedia.org/wiki/Pinyin](https://en.wikipedia.org/wiki/Pinyin)
          - [https://de.wikipedia.org/wiki/Pinyin](https://de.wikipedia.org/wiki/Pinyin)
          - Duanmu, San. 2007. The Phonology of Standard Chinese. 2nd ed. Oxford ; New York: Oxford University Press.
          - Lin, Yen-Hwei. 2007. The Sounds of Chinese. Cambridge, UK ; New York: Cambridge University Press.

          ### Acknowledgments

          [pypinyin](https://github.com/mozillazg/python-pinyin) \\
          Funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) – Project-ID 416228727 – [CRC 1410](https://gepris.dfg.de/gepris/projekt/416228727?context=projekt&task=showDetail&id=416228727)

          ### App information

          - Version: 0.0.2
          - License: [MIT](https://github.com/stefantaubert/pinyin-to-ipa?tab=MIT-1-ov-file#readme)
          - GitHub: [stefantaubert/pinyin-to-ipa](https://github.com/stefantaubert/pinyin-to-ipa)
          """
        )

    # pylint: disable=E1101:no-member
    convert_btn.click(
      fn=synt,
      inputs=[
        input_txt_box,
      ],
      outputs=[
        output_txt_box,
      ],
      queue=True,
      show_progress=False,
    )

  return web_app


def synt(pinyin: str) -> str:
  try:
    output = pinyin_to_ipa(pinyin)
  except ValueError as error:
    result = "No valid pinyin detected! Please make sure to enter only one syllable."
    return result

  result = "\n".join(
    [
      "".join(x) for x in output
    ]
  )
  return result


if __name__ == "__main__":
  run()