Recent Roundup

March 2nd, 2010 § 0 comments § permalink

一些小体会,简单地记一下。

  • 最近的一次面试中,被问了一个 critical thinking 的问题。“假设你患了某种肾病,需要做肾脏移植才能康复。有用的信息是人类的肾脏有两种类型,A 和 B,但是在移植手术之前你即无法知道自己的类型也不知道捐献器官者的类型。假设在男性和女性中 A 型的比例分别是 p 和 q。现在有男女两个捐献者,你应该选择谁?”这其实是个非常简单的纯概率题(过程中我却以为是陷阱,给了一个自以为非常 smart 的答案)。If you do the math,it’s just straightforward。下图就是答案。
    match_prob
  • A few learnings from playing Texas Hold’em:
    1. 即使是零和博弈通货膨胀也能提高经济活动的活跃度;
    2. bailout 是玩家不多的时候经济稳定的基石;
    3. 自由竞争往往导致垄断;
    4. 为什么同花实际出现的概率要大于顺子呢?Poker Probability.

    A few learnings from playing Plants vs Zombies:

    1. 新时代的新问题要用新时代的新技术来解决;
    2. 大局观和局部战术都很重要,攻击要合理选择突破口,有重点。
  • 听说一个故事,想起 Adam Lambert 版本的 Tracks of My Tears

Update Project Page 02/13/2010: Build a txt eBook

February 14th, 2010 § 1 comment § permalink

这里是一个把搜狐读书里的《蔡澜谈日本 – 日本电影》下载整编为 txt 电子书的例子。

  1. 首先把《蔡澜谈日本:日本电影》的首页下载并转化为 UTF-8 编码。
    wget -c "http://lz.book.sohu.com/serialize-id-12171.html" -O index.raw
    iconv -f GBK -t UTF-8 index.raw > index.raw.utf
    mv -f index.raw.utf index.raw
    
  2. 第二步是从首页的 html 文件中找出每一章节的链接和目录名。
    # find lines containing chapter links
    sed -n '/<ul class="clear">/,/</ul>/p' index.raw | grep 'chapter.*html' > links.raw
    # find links
    awk -F 'href="' '{print $2}' links.raw | cut -d'"' -f1 | sed 's@^@http://lz.book.sohu.com/@' > chapterlinks.raw
    # find chapter titles
    awk -F '">' '{print $2}' links.raw | cut -d'<' -f1 | sed 's@$@.txt@' > chaptertitles.raw
    # put links and titles together
    paste chapterlinks.raw chaptertitles.raw > chapter_to_dl.raw
    

    得到的一个内容如下的文件

    http://lz.book.sohu.com/chapter-12171-111059829.html    片冈千惠藏.txt
    http://lz.book.sohu.com/chapter-12171-111059833.html    冈崎宏三.txt
    http://lz.book.sohu.com/chapter-12171-111059837.html    胜新太郎(一).txt
    http://lz.book.sohu.com/chapter-12171-111059845.html    胜新太郎(二).txt
    
  3. 这一步是将 chapter_to_dl.raw 文件里第一列的链接下载并存为第二列所示的文件名。这里用到一个 awk 脚本 download.awk。然后再把每一节都从 GBK 编码转为 UTF-8 编码。
    awk -f download.awk chapter_to_dl.raw
    for mftxt in $(ls *.txt)
    do
      iconv -f GBK -t UTF-8 "$mftxt" > "$mftxt".utf
      mv -f "$mftxt".utf "$mftxt"
    done
    
  4. 第四步是从每个章节中的 html 文件中提取真正的文本内容。
    for mftxt in $(ls *.txt)
    do
      sed -n '/<div .* id="txtBg">/,/</div>/p' "$mftxt" | grep '<p>' | sed 's/<[^>]*>//g;s/&nbsp;&nbsp;&nbsp;&nbsp;/n/g' > "$mftxt".part
      mv -f "$mftxt".part "$mftxt"
    done
    
  5. 最后一步是将全文连接起来。
    for mfchpt in $(cat chaptertitles.raw)
    do
      echo "$mfchpt" | sed 's/.txt$//' >> book.txt
      echo >> book.txt
      cat "$mfchpt" >> book.txt
      echo >> book.txt
    done
    

最后得到的这个 book.txt 便是想要的《蔡澜谈日本 – 日本电影》了,我的偏好是放在 Stanza 或者 Good Reader 里。脚本在这里