Personal website https://benkurtovic.com/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

2014-08-20-copyvio-detector.md 6.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ---
  2. layout: post
  3. title: Copyvio Detector
  4. description: A technical writeup of some recent developments.
  5. ---
  6. This is an technical writeup of some recent developments involving the
  7. [copyright violation](//en.wikipedia.org/wiki/WP:COPYVIO) detector for
  8. Wikipedia articles that I maintain, located at
  9. [tools.wmflabs.org/copyvios](//tools.wmflabs.org/copyvios). Its source code is
  10. available on [GitHub](//github.com/earwig/copyvios).
  11. ## Dealing with sources
  12. Of course, the central component of the detector is finding and parsing
  13. potential sources of copyright violations. These sources are obtained through
  14. two methods: investigating external links found in the article, and searching
  15. for article content elsewhere on the web using a search engine
  16. ([Yahoo! BOSS](//developer.yahoo.com/boss/search/), paid for by the Wikimedia
  17. Foundation).
  18. To use the search engine, we must first break the article text up into plain
  19. text search queries, or "chunks". This involves some help from
  20. [mwparserfromhell](//github.com/earwig/mwparserfromhell), which is used to
  21. strip out non-text wikicode from the article, and the [Python Natural Language
  22. Toolkit](http://www.nltk.org/), which is then used to split this up into
  23. sentences, of which we select a few medium-sized ones to search for.
  24. mwparserfromhell is also used to extract the external links.
  25. Sources are fetched and then parsed differently depending on the document type
  26. (HTML is handled by
  27. [beautifulsoup](http://www.crummy.com/software/BeautifulSoup/), PDFs are
  28. handled by [pdfminer](http://www.unixuser.org/~euske/python/pdfminer/)), and
  29. normalized to a plain text form. We then create multiple
  30. [Markov chains](https://en.wikipedia.org/wiki/Markov_chain) – the *article
  31. chain* is built from word trigrams from the article text, and a *source chain*
  32. is built from each source text. A *delta chain* is created for each source
  33. chain, representing the intersection of it and the article chain by examining
  34. which nodes are shared.
  35. But how do we use these chains to decide whether a violation is present?
  36. ## Determining violation confidence
  37. One of the most nuanced aspects of the detector is figuring out the likelihood
  38. that a given article is a violation of a given source. We call this number, a
  39. value between 0 and 1, the "confidence" of a violation. Values between 0 and
  40. 0.4 indicate no violation (green background in results page), between 0.4 and
  41. 0.75 a "possible" violation (yellow background), and between 0.75 and 1 a
  42. "suspected" violation (red background).
  43. To calculate the confidence of a violation, the copyvio detector uses the
  44. maximum value of two functions, one of which accounts for the size of the delta
  45. chain (<span>\\(\Delta\\)</span>) in relation to the article chain
  46. (<span>\\(A\\)</span>), and the other of which accounts for just the size of
  47. <span>\\(\Delta\\)</span>. This ensures a high confidence value when both
  48. chains are small, but not when <span>\\(A\\)</span> is significantly larger
  49. than <span>\\(\Delta\\)</span>.
  50. The article–delta confidence function, <span>\\(C_{A\Delta}\\)</span>, is
  51. piecewise-defined such that confidence increases at an exponential rate as
  52. <span>\\(\frac{\Delta}{A}\\)</span> increases, until the value of
  53. <span>\\(C_{A\Delta}\\)</span> reaches the "suspected" violation threshold, at
  54. which point confidence increases at a decreasing rate, with
  55. <span>\\(\lim_{\frac{\Delta}{A} \to 1}C\_{A\Delta}(A, \Delta)=1\\)</span>
  56. holding true. The exact coefficients used are shown below:
  57. <div>$$C_{A\Delta}(A, \Delta)=\begin{cases} -\ln(1-\frac{\Delta}{A}) &
  58. \frac{\Delta}{A} \le 0.52763 \\[0.5em]
  59. -0.8939(\frac{\Delta}{A})^2+1.8948\frac{\Delta}{A}-0.0009 &
  60. \frac{\Delta}{A} \gt 0.52763 \end{cases}$$</div>
  61. A graph can be viewed [here](/static/article-delta_confidence_function.pdf),
  62. with the x-axis indicating <span>\\(\frac{\Delta}{A}\\)</span> and the y-axis
  63. indicating confidence. The background is colored red, yellow, and green when a
  64. violation is considered suspected, possible, or not present, respectively.
  65. The delta confidence function, <span>\\(C_{\Delta}\\)</span>, is also
  66. piecewise-defined. A number of confidence values were derived experimentally,
  67. and the function was extrapolated from there such that
  68. <span>\\(\lim_{Δ \to +\infty}C\_{\Delta}(\Delta)=1\\)</span>. The reference
  69. points were <span>\\(\\{(0, 0), (100, 0.5), (250, 0.75), (500, 0.9),
  70. (1000, 0.95)\\}\\)</span>. The function is defined as follows:
  71. <div>$$C_{\Delta}(\Delta)=\begin{cases} \frac{\Delta}{\Delta+100} & \Delta\leq
  72. 100 \\[0.5em] \frac{\Delta-25}{\Delta+50} & 100\lt \Delta\leq 250\; \\[0.5em]
  73. \frac{10.5\Delta-750}{10\Delta} & 250\lt \Delta\leq 500\; \\[0.5em]
  74. \frac{\Delta-50}{\Delta} & \Delta\gt500 \end{cases}$$</div>
  75. A graph can be viewed [here](/static/delta_confidence_function.pdf), with the
  76. x-axis indicating <span>\\(\Delta\\)</span>. The background coloring is the
  77. same as before.
  78. Now that we have these two definitions, we can define the primary confidence
  79. function, <span>\\(C\\)</span>, as follows:
  80. <div>$$C(A, \Delta) = \max(C_{A\Delta}(A, \Delta), C_{\Delta}(\Delta))$$</div>
  81. By feeding <span>\\(A\\)</span> and <span>\\(\Delta\\)</span> into
  82. <span>\\(C\\)</span>, we get our final confidence value.
  83. ## Multithreaded worker model
  84. At a high level, the detector needs to be able to rapidly handle a lot of
  85. requests at the same time, but without falling victim to denial-of-service
  86. attacks. Since the tool needs to download many webpages very quickly, it is
  87. vulnerable to abuse if the same request is repeated many times without delay.
  88. Therefore, all requests made to the tool share the same set of persistent
  89. worker subprocesses, referred to as *global worker* mode. However, the
  90. underlying detection machinery in earwigbot also supports a *local worker*
  91. mode, which spawns individual workers for each copyvio check so that idle
  92. processes aren't kept running all the time.
  93. But how do these workers handle fetching URLs? The "safe" solution is to only
  94. handle one URL at a time per request, but this is too slow when twenty-five
  95. pages need to be checked in a few seconds – one single slow website will cause
  96. a huge delay. The detector's solution is to keep unprocessed URLs in
  97. site-specific queues, so that at any given point, only one worker is handling
  98. URLs for a particular domain. This way, no individual website is overloaded by
  99. simultaneous requests, but the copyvio check as a whole is completed quickly.
  100. Other features enable efficiency: copyvio check results are cached for a period
  101. of time so that the Foundation doesn't have to pay Yahoo! for the same
  102. information multiple times; and if a possible source is found to have a
  103. confidence value within the "suspected violation" range, yet-to-be-processed
  104. URLs are skipped and the check short-circuits.