├── README.md
└── main.py
/README.md:
--------------------------------------------------------------------------------
1 | # tophonetics.com API
2 | # Update: This is broken. I will not be maintaining this
3 | ## The replit is using [eng-to-ipa](https://pypi.org/project/eng-to-ipa/) instead.
4 | An API for [tophonetics.com](https://www.tophonetics.com/) using Flask. It basically just sends a request to tophonetics.com, then formats the HTML response into plain text IPA. Thus, it is possible that it could fail if they reformat their website. Also, it can take up to 3 seconds for the IPA to load.
5 |
6 | An instance is currently running on https://tophonetics-api.ajlee.repl.co/api.
7 |
8 | ## Use
9 | ### Python (requests)
10 | ```python
11 | import requests
12 | english = "hello"
13 | british_english_ipa = requests.get("https://tophonetics-api.ajlee.repl.co/api", data={"text": english}).text
14 | american_english_ipa = requests.get("https://tophonetics-api.ajlee.repl.co/api", data={"text": english, "dialect": "am"}).text
15 | ```
16 | ### In the URL
17 | Visit https://tophonetics-api.ajlee.repl.co/api?text=[text] in the browser to get the IPA in plain text. You can also add "&dialect=am" after if you want American English (British English is the default).
18 |
19 | ## Characters returned
20 | The following characters are used in the IPA returned:
21 |
22 | Consonants: bdfhjklmnprstvwxzðŋɡʃʒʤʧθ
23 | Vowels: aeiouæɑɒɔəɛɜɪʊʌ
24 | Other: *ˈˌː
25 |
26 | ## Legality
27 | I posted a comment on tophonetics.com sharing this, and they replied: "We do not encourage this as it is open to abuse and, yes, it will hit the wall eventually, but good job". I suppose this shows that it is allowed and legal though they don't encourage it.
28 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 | from flask import request
3 | from markupsafe import escape
4 | import requests, html
5 | from bs4 import BeautifulSoup
6 |
7 | app = Flask(__name__)
8 |
9 | @app.route('/')
10 | def index():
11 | return 'Type in "/api?text=" and then some text.
You can also change the dialect to American by adding "&dialect=am" after the text.'
12 |
13 | @app.route('/api')
14 | def toipa():
15 | try:
16 | text = request.form['text']
17 | except:
18 | try:
19 | text = request.args.get('text', '')
20 | except:
21 | return 'Type in "/api?text=" and then some text.
You can also change the dialect to American by adding "&dialect=am" after the text.'
22 | try:
23 | dialect = request.form['dialect']
24 | except:
25 | try:
26 | dialect = request.args.get('dialect', '')
27 | except KeyError:
28 | dialect = 'br'
29 | try:
30 | prebracket = request.form['prebracket']
31 | except:
32 | try:
33 | prebracket = request.args.get('prebracket', '')
34 | except KeyError:
35 | prebracket = ''
36 | try:
37 | postbracket = request.form['postbracket']
38 | except:
39 | try:
40 | postbracket = request.args.get('postbracket', '')
41 | except KeyError:
42 | postbracket = ''
43 | data = {"text_to_transcribe": text, "submit": "", "output_dialect": dialect, "output_style": "only_tr", "preBracket": prebracket, "postBracket": postbracket, "speech_support": "1"}
44 | response = requests.post("https://tophonetics.com/", data=data).text
45 | soup = BeautifulSoup(response, features="html5lib")
46 | tag = str(soup.find(id='transcr_output')).replace('
','\n')
47 | result = ''.join(BeautifulSoup(tag, features="html5lib").findAll(text=True))
48 | return html.unescape(result).strip().replace(u'\xa0', ' ')
49 |
50 | if __name__ == '__main__':
51 | app.run(host='0.0.0.0')
52 |
--------------------------------------------------------------------------------