code_junkie

Determining “Owner” of Text Edited by Multiple Users

Nov 13th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. User 38193 owns 42% (922 / 2171) of the final post
  2. User 2635 owns 28% (625 / 2171) of the final post
  3. User 116 owns 24% (529 / 2171) of the final post
  4. User 745 owns 3% (76 / 2171) of the final post
  5. User 13005 owns 0% (11 / 2171) of the final post
  6. User 18941 owns 0% (5 / 2171) of the final post
  7. User 8562 owns 0% (3 / 2171) of the final post
  8.  
  9. 53 ms
  10.  
  11. User 38193 owns 42% (922 / 2171) of the final post
  12. User 2635 owns 28% (625 / 2171) of the final post
  13. User 116 owns 24% (529 / 2171) of the final post
  14. User 745 owns 3% (76 / 2171) of the final post
  15. User 13005 owns 0% (11 / 2171) of the final post
  16. User 18941 owns 0% (5 / 2171) of the final post
  17. User 8562 owns 0% (3 / 2171) of the final post
  18.  
  19. 53 ms
  20.  
  21. #!/usr/bin/env python
  22.  
  23. import collections
  24. import difflib
  25. import logging
  26. import pprint
  27. import urllib2
  28. import re
  29.  
  30. class OwnageDeterminer(object):
  31.  
  32. add_coefficient = 1
  33. remove_coefficient = .5
  34.  
  35. def __init__(self, edits):
  36. self.edits = edits
  37. self.counts_by_username = {}
  38.  
  39. def __call__(self):
  40. edits, counts_by_username = self.edits, self.counts_by_username
  41. for i, edit in enumerate(edits):
  42. username = edit['username']
  43. unique_counts = {'added': 0, 'removed': 0}
  44. existing_text = edits[i-1]['text'] if i > 0 else ''
  45. new_text = edits[i]['text']
  46. for char_diff in difflib.ndiff(existing_text, new_text):
  47. if char_diff.startswith('+'):
  48. unique_counts['added'] += 1
  49. elif char_diff.startswith('-'):
  50. unique_counts['removed'] += 1
  51. user_counts = counts_by_username.get(username, collections.defaultdict(int))
  52. user_counts['removed'] += self.remove_coefficient * unique_counts['removed']
  53. user_counts['added'] += self.add_coefficient * unique_counts['added']
  54. counts_by_username[username] = user_counts
  55. winner = None
  56. winning_score = 0
  57. score_by_username = {}
  58. for username, counts in counts_by_username.iteritems():
  59. score = counts['removed'] + counts['added']
  60. if score > winning_score:
  61. winner = username
  62. winning_score = score
  63. score_by_username[username] = score
  64. logging.debug('Scores: %s', pprint.pformat(score_by_username))
  65. return winner
  66.  
  67.  
  68. if __name__ == '__main__':
  69. logging.basicConfig(level=logging.DEBUG)
  70. site = urllib2.urlopen('http://stackoverflow.com/revisions/327973/list')
  71. contents = site.read()
  72. regex = re.compile(r'(/revisions/viewmarkup/d+).*?/users/d+/([w-]+)',
  73. re.MULTILINE|re.DOTALL)
  74. revisions = regex.findall(contents)
  75. print revisions
  76. edits = []
  77. for reluri, username in sorted(revisions, key=lambda t: t[0]):
  78. text = urllib2.urlopen('http://stackoverflow.com{0}'.format(reluri)).read()
  79. edit = {'username': username, 'text': text}
  80. edits.append(edit)
  81. od = OwnageDeterminer(edits)
  82. print od()
  83.  
  84. DEBUG:root:Scores: {'blorgbeard': 0.5,
  85. 'dave-markle': 0.5,
  86. 'dbr': 1172.0,
  87. 'gatekiller': 69.5,
  88. 'joseph-ferris': 0.0,
  89. 'lkessler': 0.0,
  90. 'mark-harrison': 592.0,
  91. 'mdb': 3.0,
  92. 'onebyone-livejournal-com': 0.0,
  93. 'paul-oyster': 482.0,
  94. 'rob-wells': 0.0,
  95. 'simucal': 1070.5,
  96. 'skiphoppy': 0.0,
  97. 'thesoftwarejedi': 701.0}
  98. dbr
  99.  
  100. Break latest revision into a set of sentences.
  101. //Sentence is any text fragment surrounded by punctuation
  102.  
  103. For each Sentence
  104. Find which user created that sentence.
  105. Add 1 to the user who created the sentence
  106. Add 1 to the number of sentences
  107.  
  108. For Each user
  109. % ownership = Count for that user / Number of sentences.
  110.  
  111. Let n = total revisions
  112. Let m = the revision number of a poster
  113. Let post[it] = array with text of post at revision 'it'
  114. Let votes[it] = votes that revision 'it' received (also add bonus for comments/answers)
  115. value = 0
  116. for (it = m; it < n; ++it) {
  117. value += (Levenshtein(post[it-1], post[m]) / average_length_post) * (votes[it])
  118. }
Add Comment
Please, Sign In to add comment