-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoem.py
More file actions
70 lines (46 loc) · 1.98 KB
/
Copy pathpoem.py
File metadata and controls
70 lines (46 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
import sys
import importlib
importlib.reload(sys) # Reload does the trick!
from util import *
import re
__author__ = 'chenliang'
def process_chu_ci():
with open(get_relative_path(__file__, "raw/poems/楚辞")) as data_file:
data_file_content = data_file.read().encode("UTF-8").decode("UTF-8")
# print data_file_content
# poems = re.findall(ur'(^[^\s].*$\n)((^($|^\s+.*$)\n)+)', data_file_content, re.MULTILINE)
# poems = re.findall(ur'(^[\S].*$\n)((^$\n|^\s+.*\n)*)(?!^\S)', data_file_content, re.MULTILINE)
poems = re.findall(r'(^[\S].*$\n)((^$\n|^\s+.*\n)+)', data_file_content, re.MULTILINE)
print(len(poems))
for poem in poems:
print(len(poem))
print("--->".join(poem))
# for p in poem:
# print p
# process_chu_ci()
def get_files(path):
result = []
for root, dirs, files in os.walk(path):
result.extend(map(lambda f: os.path.join(root, f), files))
return result
def get_characters_in_poem_files(files):
result = set()
for poem_file_path in files:
with open(poem_file_path) as poem_file:
map(lambda k: result.add(k), filter(lambda ch: ch.strip() != "", poem_file.read().encode("utf-8").decode("utf-8")))
return result
def generate_all_characters_in_poems():
characters = get_all_characters_in_poem()
with open(get_relative_path(__file__, "data/poem_characters"), "w") as poem_characters_file:
poem_characters_file.write("{0}".format("".join(characters)))
def get_all_characters_in_poem():
poem_files = get_files(get_relative_path(__file__, "raw/poems"))
return get_characters_in_poem_files(poem_files)
# generate_all_characters_in_poems()
# print "importing opem"
def get_poem_score(name):
count = len(list(filter(lambda c: c in all_characters_in_poems, name)))
return int(count * 1.0 * 100 / len(name))
all_characters_in_poems = get_all_characters_in_poem()
# process_chu_ci()