Open Chinese Convert  0.4.3
A project for conversion between Traditional and Simplified Chinese
 All Data Structures Files Functions Variables Groups Pages
common.py
1 #coding: utf-8
2 
3 def sort_items(input_filename, output_filename):
4  input_file = open(input_filename, "r")
5  dic = {}
6 
7  for line in input_file:
8  if len(line) == 0:
9  continue
10  try:
11  key, value = line.split("\t")
12  except ValueError:
13  print line
14  while value[-1] == "\n" or value[-1] == "\r":
15  value = value[:-1]
16  dic[key] = value
17 
18  input_file.close()
19 
20  output_file = open(output_filename, "w")
21 
22  for key in sorted(dic.iterkeys()):
23  output_file.write(key + "\t" + dic[key] + "\n")
24 
25  output_file.close()
26 
27 def reverse_items(input_filename, output_filename):
28  input_file = open(input_filename, "r")
29  dic = {}
30 
31  for line in input_file:
32  if len(line) == 0:
33  continue
34  key, value = line.split("\t")
35  while value[-1] == "\n" or value[-1] == "\r":
36  value = value[:-1]
37 
38  value_list = value.split(" ")
39  for value in value_list:
40  if dic.has_key(value):
41  dic[value].append(key)
42  else:
43  dic[value] = [key]
44 
45  input_file.close()
46 
47  output_file = open(output_filename, "w")
48 
49  for key in sorted(dic.iterkeys()):
50  output_file.write(key + "\t" + " ".join(dic[key]) + "\n")
51 
52  output_file.close()
53 
54 def find_target_items(input_filename, keyword):
55  input_file = open(input_filename, "r")
56  for line in input_file:
57  if len(line) == 0:
58  continue
59  key, value = line.split("\t")
60  while value[-1] == "\n" or value[-1] == "\r":
61  value = value[:-1]
62 
63  value_list = value.split(" ")
64  for value in value_list:
65  if keyword in value:
66  print line,
67 
68  input_file.close()