selfteaching / selfteaching/selfteaching-python-camp
Day10 思考:d9_and_d10中文词频统计结果的有效性
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 151
- Forks
- 875
- PR merge metrics
- No merged PRs in 30d
Description
第九天的任务是,统计字频
第十天的任务是,统计词频
关于文本的基本处理功能,我们已经在第九天之前完成,于是,第九第十天就是进一步借用内置库、第三方库让我们的代码变得更强大。
我们的处理对象是一个存放唐诗三百首的json文件,“tang300.json”
这个 tang300.json 内容长这个样:
[
{
"id": 1,
"contents": "孤鸿海上来,池潢不敢顾。\n侧见双翠鸟,巢在三珠树。\n矫矫珍木巅,得无金丸惧。\n美服患人指,高明逼神恶。\n今我游冥冥,弋者何所慕。",
"type": "五言古诗",
"author": "张九龄",
"title": "感遇四首之一"
},
...
于是,我们会调用了 json 这个内置库中的json.load(f)提取一个已经open的 json 文件的内容,并解码成为一个字典,然后提取其中每个 items 的 comtents properties 组成一个 string 就可以统计字频了 ......
不过,这里有个有意思的地方,尽管我们的主题是“调用第三方库”,我们依然可以不那么听话~ 直接用f.read()也可以达到差不过的效果,甚至它看起来更简便 —— 读出来的时候已经是一个 string, 尽管这个结果依然看着跟上面的 json 格式差不多,然后直接扔“词频统计器”就好了,反正它会自动帮我们处理那些乱七八糟的符号~
可,用f.read()确实有副作用,只不过在这个例子中不明显,到了第十天的学习中,
看看用 f.read() 读取与用 json.load(f) 读取两者有什么差异:
f.read():
json.load(f):
现在清楚了,因为使用 f.read() 会把每个 items 的 type 和 title 的内容也统计入内,我们得到了一个实际上“毫无意义”的统计结果 —— “古诗”、“五言律诗”、“李白” ......
也就是说,如果在我们统计词频的时候,出于结果的实际意义考虑,我们不能那么任性 ......
进而,这个“不能那么任性”还体现在于,由于中文组词的特点,任何两个字放到一块都有可能变成词语,所以,直接粗暴地把数字、英文、各种符号除去是不行的(需要处理英文、数字,因为 jieba 会连英文单词和数字都会一块统计,尽管 tang300.json 中各个 items 的 contents 不含英文,但在后面的练习中会有),于是,我考虑的是,把所有英文和数字转化成某一个符号,比如逗号 “,”
text_cn = ""
for t in text:
if ord(t) > 256:
text_cn = text_cn + t
else:
text_cn = text_cn + ","
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Begin with tang300.json and compare the f.read() and json.load(f) paths described here, including the provided filtering snippet. Check how the resulting input is passed to jieba and whether metadata such as type, title, and author enters the counts. Done means the issue records a decided, reproducible interpretation of which fields and characters belong in the word-frequency result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100