Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Some functions about diacritics
5"""
6import re
7import keyword
10def change_style(name):
11 """
12 Switches from *AaBb* into *aa_bb*.
14 @param name name to convert
15 @return converted name
17 Example:
19 .. runpython::
20 :showcode:
22 from pyquickhelper.texthelper import change_style
24 print("changeStyle --> {0}".format(change_style('change_style')))
25 """
26 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
27 s2 = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
28 return s2 if not keyword.iskeyword(s2) else s2 + "_"
31def add_rst_links(text, values, tag="epkg", n=4):
32 """
33 Replaces words by something like ``:epkg:'word'``.
35 @param text text to process
36 @param values values
37 @param tag tag to use
38 @param n number of consecutive words to look at
39 @return new text
41 .. runpython::
42 :showcode:
44 from pyquickhelper.texthelper import add_rst_links
45 text = "Maybe... Python is winning the competition for machine learning language."
46 values = {'Python': 'https://www.python.org/',
47 'machine learning': 'https://en.wikipedia.org/wiki/Machine_learning'}
48 print(add_rst_links(text, values))
49 """
50 def replace(words, i, n):
51 mx = max(len(words), i + n)
52 for last in range(mx, i, -1):
53 w = ''.join(words[i:last])
54 if w in values:
55 return last, ":{0}:`{1}`".format(tag, w)
56 return i + 1, words[i]
58 reg = re.compile("(([\\\"_*`\\w']+)|([\\W]+)|([ \\n]+))")
59 words = reg.findall(text)
60 words = [_[0] for _ in words]
61 res = []
62 i = 0
63 while i < len(words):
64 i, w = replace(words, i, n)
65 res.append(w)
66 return ''.join(res)