Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

#!/usr/bin/python 

# -*- coding: utf-8 -*- 

''' 

Make a list of top streets for a municipality. Bot expects two things on the commandline: 

* -countrycode : The country code (as it is in the database) 

* -langcode : The language code (as it is in the database) 

* -municipality : The name of the municipality (as it is in the database) 

* -minimum : (optional) The minimum of hits before we show the item 

''' 

from collections import Counter 

 

import pywikibot 

 

from erfgoedbot.database_connection import ( 

close_database_connection, 

connect_to_monuments_database 

) 

 

 

def getAddresses(countrycode, lang, municipality, conn, cursor): 

''' 

Get a list of addresses of a municipality in a country in a certain language 

''' 

result = [] 

query = ( 

"SELECT address " 

"FROM monuments_all " 

"WHERE country=%s AND lang=%s AND municipality=%s " 

"ORDER BY address ASC") 

cursor.execute(query, (countrycode, lang, municipality)) 

 

while True: 

try: 

row = cursor.fetchone() 

(address,) = row 

result.append(address) 

except TypeError: 

break 

 

return result 

 

 

def printTopStreets(addresses, minimum): 

''' 

Print the top streets with a minimum number of hits 

''' 

streets = Counter() # collections.Counter 

for address in addresses: 

address = address.replace('{{sorteer|', '') 

temp = '' 

partslist = [] 

for addrPart in address.split(' '): 

temp += ' ' + addrPart 

partslist.append(temp.strip()) 

 

streets.update(partslist) 

 

topStreets = [] 

 

for street in streets.most_common(): 

if street[1] < minimum: 

break 

topStreets.append(street[0]) 

 

filteredStreets = [] 

 

for topStreet1 in topStreets: 

for topStreet2 in topStreets: 

if topStreet1 != topStreet2 and topStreet2.startswith(topStreet1): 

filteredStreets.append(topStreet1) 

break 

 

pywikibot.output('Filtered out the following. These are probably ' 

'street parts:') 

for street in streets.most_common(): 

if street[1] < minimum: 

break 

if street[0] in filteredStreets: 

pywikibot.output('* %s - %s' % street) 

 

pywikibot.output('Found the following entries which are probably ' 

'real streets:') 

for street in streets.most_common(): 

if street[1] < minimum: 

break 

if not street[0] in filteredStreets: 

pywikibot.output('* %s - %s' % street) 

 

 

def main(): 

countrycode = '' 

lang = '' 

municipality = '' 

minimum = 15 

conn = None 

cursor = None 

# Connect database, we need that 

(conn, cursor) = connect_to_monuments_database() 

 

for arg in pywikibot.handleArgs(): 

option, sep, value = arg.partition(':') 

if option == '-countrycode': 

countrycode = value 

elif option == '-langcode': 

lang = value 

elif option == '-municipality': 

municipality = value 

elif option == '-minimum': 

minimum = int(value) 

else: 

raise Exception( 

'Bad parameters. Expected "-countrycode", "-langcode", ' 

'"-municipality", "-minimum" or pywikibot args. ' 

'Found "{}"'.format(option)) 

 

if countrycode and lang and municipality: 

addresses = getAddresses(countrycode, lang, municipality, conn, cursor) 

printTopStreets(addresses, minimum) 

else: 

print('Usage') 

 

close_database_connection(conn, cursor) 

 

 

if __name__ == "__main__": 

pywikibot.log('Start of %s' % __file__) 

try: 

main() 

finally: 

pywikibot.stopme()