<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Tool on Shaodong's Blog</title><link>https://zhangshaodong.com/tags/tool/</link><description>Recent content in Tool on Shaodong's Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 15 Mar 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://zhangshaodong.com/tags/tool/feed.xml" rel="self" type="application/rss+xml"/><item><title>Python in use</title><link>https://zhangshaodong.com/2024/03/15/python-in-use/</link><pubDate>Fri, 15 Mar 2024 00:00:00 +0000</pubDate><guid>https://zhangshaodong.com/2024/03/15/python-in-use/</guid><description>&lt;p>I have been using &lt;a href="https://www.python.org" target="_blank" rel="noopener">Python&lt;/a> for a while and have recently come to realize how useful it is. This blog is intended to preserve codes I&amp;rsquo;ve created for my personal use.&lt;/p>
&lt;p>I enjoy reading books in English on &lt;a href="https://weread.qq.com" target="_blank" rel="noopener">Wechat Reading (WR)&lt;/a>, though I am not much into Wechat itself. In recent years, WR has increasingly equipped itself with functions assisting readers in engaging with English books such as notes, lookup, auto-translation and pronunciation, etc. Among these, taking notes is as crucial as looking up for me. I have, therefore, underlined tons of English vocab while writing down notes.&lt;/p>
&lt;p>However, whenever I want to extract the vocab from my notes on WR, the thing bothered me lot was that WR exports all underlined vocab and its notes where I left notes both in English and Chinese. &lt;strong>How can I pull out the vocab without any Chinese characters and duplicates?&lt;/strong>&lt;/p>
&lt;img alt="A WeChat Read export containing English highlights and Chinese notes" decoding="async" height="1688" src="https://zhangshaodong.com/2024/03/15/python-in-use/cn_and_eng_hu8561e8046f5c48cbd20310ee48e30854_709459_948x0_resize_box_3.png" style="zoom:50%;" width="948">
&lt;p>My solution is to use &lt;a href="https://en.wikipedia.org/wiki/Regular_expression" target="_blank" rel="noopener">Regular Expressions (Regex)&lt;/a>. Let me start by writing an outline of the code:&lt;/p>
&lt;pre tabindex="0">&lt;code>import re
text = &amp;#39;&amp;#39;&amp;#39;....&amp;#39;&amp;#39;&amp;#39;
pttn = r&amp;#39;...&amp;#39;
re.findall(pttn, text)
&lt;/code>&lt;/pre>&lt;p>I found the extracted list is a list with the vocab preceded by &lt;code>&amp;gt;&amp;gt; &lt;/code> and that most of the vocab I marked has more than 5 characters. Then I defined the pattern as follows:&lt;/p>
&lt;pre tabindex="0">&lt;code>pttn = r&amp;#39;&amp;gt;&amp;gt; (\w{5,})&amp;#39;
&lt;/code>&lt;/pre>&lt;p>I used &lt;code>\w&lt;/code> here instead of &lt;code>\b[a-zA-Z]\b&lt;/code> because there are no &lt;code>0-9&lt;/code> in English words. On top of this, I limited the number of characters to a minimum of &lt;code>5&lt;/code> to exclude articles (e.g., &lt;em>a, the&lt;/em>), prepositions (e.g., &lt;em>from, in, with&lt;/em>), and pronouns (e.g., &lt;em>I, you, he, she&lt;/em>). Finally, I put &lt;code>\w{5,}&lt;/code> in &lt;code>( )&lt;/code>. This way, the captured words will not be prefixed with &lt;code>&amp;gt;&amp;gt; &lt;/code>. Take a look at &lt;a href="https://regexper.com/#%27%3E%3E%20%28%5Cw%7B5%2C%7D%29%27" target="_blank" rel="noopener">&amp;raquo; (\w{5,})&lt;/a>. Looks good.&lt;/p>
&lt;p>Sequentially, I find all matches and remove duplicates by converting the &lt;code>list&lt;/code> to a &lt;code>set&lt;/code>:&lt;/p>
&lt;pre tabindex="0">&lt;code>ls = set(re.findall(pttn, text))
&lt;/code>&lt;/pre>&lt;p>To facilitate reading, let me print it out:&lt;/p>
&lt;pre tabindex="0">&lt;code>for word in ls:
print(word)
&lt;/code>&lt;/pre>&lt;p>Lastly, put the list in &lt;code>text&lt;/code> and run the test:&lt;/p>
&lt;pre tabindex="0">&lt;code>import re
text = &amp;#39;&amp;#39;&amp;#39;
&amp;gt;&amp;gt; vainglorious
&amp;gt;&amp;gt; chieftains
&amp;gt;&amp;gt; squad
&amp;gt;&amp;gt; decrees
&amp;gt;&amp;gt; apparatus
2024/2/5 发表想法
[kəˈnuː]
&amp;gt;&amp;gt; canoe
&amp;gt;&amp;gt; canoe
2024/2/5 发表想法
进行; 赞同; 兜风; 陪伴
&amp;gt;&amp;gt; “You’ll go along
&amp;gt;&amp;gt; “You’ll go along
2024/2/5 发表想法
英[tru:p]美[trup]
n 剧团，戏班子;一班，一团
i 巡回演出
&amp;gt;&amp;gt; troupe
&amp;gt;&amp;gt; troupe
2024/2/5 发表想法
/prɪˈmɪə(r), prəˈmjer/
n. 首次公演;首次展出
n. (戏剧等的)主角女演员
a. 首次的,最早的
a. 首位的;首要的
a. (在戏剧、舞蹈等中)扮演女主角的
vt. 首次上演
vi. 首次上演;(名演员等)首次露面
vi. 首次出现
&amp;gt;&amp;gt; premiere
&amp;gt;&amp;gt; premiere
&amp;gt;&amp;gt; upreme judg
&amp;gt;&amp;gt; dubbed
&amp;gt;&amp;gt; raid
&amp;gt;&amp;gt; payroll
&amp;gt;&amp;gt; constables
&amp;#39;&amp;#39;&amp;#39;
pttn = r&amp;#39;&amp;gt;&amp;gt; (\w{5,})&amp;#39;
ls = set(re.findall(pttn, text))
for word in ls:
print(word)
&lt;/code>&lt;/pre>&lt;pre tabindex="0">&lt;code>upreme
payroll
troupe
premiere
decrees
squad
canoe
dubbed
constables
vainglorious
chieftains
apparatus
&lt;/code>&lt;/pre>&lt;p>This is exactly what I want! Let me wrap up by writing the above into a function named &lt;code>capture_words&lt;/code>:&lt;/p>
&lt;pre tabindex="0">&lt;code>def capture_words(text):
pattern = r&amp;#39;&amp;gt;&amp;gt; (\w{5,})&amp;#39;
ls = set(re.findall(pttn, text))
for word in ls:
print(word)
&lt;/code>&lt;/pre>&lt;pre tabindex="0">&lt;code>capture_words(&amp;#39;&amp;#39;&amp;#39;&amp;gt;&amp;gt; hahahah &amp;gt;&amp;gt; testtttt &amp;#39;&amp;#39;&amp;#39;) # test
&lt;/code>&lt;/pre>&lt;pre tabindex="0">&lt;code>hahahah
testtttt
&lt;/code>&lt;/pre>&lt;p>However, the function is not a perfect solution pulling out the vocab, as there are some words (less than 5 characters) missed such as &lt;em>raid&lt;/em> . I will keep tweaking the code for a more comprehensive solution.&lt;/p></description><category>Python</category><category>Tool</category><category>Practice</category><category>Regex</category><category>English</category></item></channel></rss>