-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.py
68 lines (57 loc) · 1.72 KB
/
functions.py
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
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 29 18:04:18 2014
@author: Bing Liu
"""
from random import random
from random import randint
import collections
def read_configuration(file_name):
f_r = open(file_name, 'rb')
ordered_dict = collections.OrderedDict()
for line in f_r:
sentence_type = line.split(';')[0]
sentence_description = line.split(';')[1]
sentence_num = int(line.split(';')[2])
ordered_dict[sentence_type + ':' + sentence_description] = sentence_num
f_r.close()
return ordered_dict
def weighted_random(weights_dict):
number = random()*sum(weights_dict.values())
for k,v in weights_dict.iteritems():
if number < v:
break
number -= v
return k
def random_ele(ele_list):
return ele_list[randint(0, len(ele_list)-1)]
def read_weighted_dict(file_name):
f_r = open(file_name, 'rb')
weighted_dict = {}
for line in f_r:
ele = line.split('\t')[0]
weight = float(line.split('\t')[1])
weighted_dict[ele] = weight
f_r.close()
return weighted_dict
def read_list(file_name):
f_r = open(file_name, 'rb')
ele_list = list()
for line in f_r:
ele_list.append(line.strip())
f_r.close()
return ele_list
def read_person_names(file_name, name_accu_prob_threshold):
accu_prob_threshold = name_accu_prob_threshold
# read female first names from file
f_r = open(file_name, 'rb')
name_dist = {}
prob = 0
for line in f_r:
name = line.split()[0].title()
accu = float(line.split()[2])
if (accu < accu_prob_threshold):
prob = float(line.split()[1])
name_dist[name] = prob
f_r.close()
return name_dist