Home > Article > Backend Development > Teach you step by step how to do a keyword matching project (search engine) ---- Day 22, teach you how to do it on the 22nd day_PHP Tutorial
Latest interview experience: Interview Feelings (2), Interview feelings
The latest architecture: high-concurrency data collection architecture application (Redis application)
Comment: I just adjusted my mentality today and continued to write the articles that I had not finished before. I have also taken a break in the past few months. I went home to do hard work, which was also regarded as exercising my body. After all, anything It can't change your health. I also recommend handsome guys in the IT industry to exercise more and exercise other parts of your body.
Day 22
Starting point: Teach you step by step how to do keyword matching project (search engine) ---- Day 1
Review: Teach you step by step how to do keyword matching project (search engine) ---- Day 21
Xiao Shuaishuai is a person who likes to make summaries. Based on the knowledge he has learned before, he summarized it as follows:
1. The problem of expansion and type of baby attributes has been well controlled initially, but there are still big obstacles in promotion, operation and maintenance.
2. To split keywords, we use scws extension and our own native business word splitting solution. Word splitting effectively solves the difficulty of matching phrases.
3. All the initial work seems to have been completed, and only the final finishing work is needed to make the project officially operational.
Xiao Shuaishuai has a strong sense of initiative. He wrote a code by himself without asking Boss Yu. The code is mainly to connect all the steps.
For the construction of extended CharList of baby attributes, please refer to: Teach you step by step how to do keyword matching projects (search engine) ---- Day 12 ~ Teach you step by step how to do keyword matching projects (search engine) ---- Day 18
The main steps of Selector are as follows:
1. Get baby attributes.
2. Use business knowledge to expand baby attributes to form CharList
3. Get keywords from the thesaurus
4. Keyword splitting algorithm
5. Matching degree algorithm
6. Return the matching keyword list
The code is as follows:
<span> 1</span> <?<span>php </span><span> 2</span> <span>#</span><span>@Filename:selector/Selector.php</span> <span> 3</span> <span>#</span><span>@Author:oshine</span> <span> 4</span> <span> 5</span> <span>require_once</span> <span>dirname</span>(<span>__FILE__</span>) . '/SelectorItem.php'<span>; </span><span> 6</span> <span>require_once</span> <span>dirname</span>(<span>__FILE__</span>) . '/charlist/CharList.php'<span>; </span><span> 7</span> <span>require_once</span> <span>dirname</span>(<span>__FILE__</span>) . '/charlist/CharlistHandle.php'<span>; </span><span> 8</span> <span>require_once</span> <span>dirname</span>(<span>dirname</span>(<span>__FILE__</span>)) . '/lib/Logger.php'<span>; </span><span> 9</span> <span>10</span> <span>class</span><span> Selector </span><span>11</span> <span>{ </span><span>12</span> <span>13</span> <span>private</span> <span>static</span> <span>$charListHandle</span> = <span>array</span><span>( </span><span>14</span> "黑名单" => "BacklistCharListHandle", <span>15</span> "近义词" => "LinklistCharListHandle" <span>16</span> <span> ); </span><span>17</span> <span>18</span> <span>public</span> <span>static</span> <span>function</span> select(<span>$num_iid</span><span>) </span><span>19</span> <span> { </span><span>20</span> <span>$selectorItem</span> = SelectorItem::createFromApi(<span>$num_iid</span><span>); </span><span>21</span> <span>22</span> Logger::trace(<span>$selectorItem</span>-><span>props_name); </span><span>23</span> <span>24</span> <span>$charlist</span> = <span>new</span><span> CharList(); </span><span>25</span> <span>26</span> <span>foreach</span> (self::<span>$charListHandle</span> <span>as</span> <span>$matchKey</span> => <span>$className</span><span>) { </span><span>27</span> <span>28</span> <span>$handle</span> = self::createCharListHandle(<span>$className</span>, <span>$charlist</span>, <span>$selectorItem</span><span>); </span><span>29</span> <span>$handle</span>-><span>exec</span><span>(); </span><span>30</span> <span>31</span> <span> } </span><span>32</span> <span>33</span> <span>$selectWords</span> = <span>array</span><span>(); </span><span>34</span> <span>35</span> <span>$keywords</span> = DB::makeArray("select word from keywords"<span>); </span><span>36</span> <span>foreach</span> (<span>$keywords</span> <span>as</span> <span>$val</span><span>) { </span><span>37</span> <span>#</span><span> code...</span> <span>38</span> <span>$keywordEntity</span> = SplitterApp::<span>split</span>(<span>$val</span>["word"<span>]); </span><span>39</span> <span>40</span> <span>#</span><span> code...</span> <span>41</span> <span>if</span>(MacthExector::macth(<span>$keywordEntity</span>,<span>$charlist</span><span>)){ </span><span>42</span> <span>$selectWords</span>[] = <span>$val</span>["word"<span>]; </span><span>43</span> <span> } </span><span>44</span> <span>45</span> <span> } </span><span>46</span> <span>47</span> <span>return</span> <span>$selectWords</span><span>; </span><span>48</span> <span> } </span><span>49</span> <span>50</span> <span>public</span> <span>static</span> <span>function</span> createCharListHandle(<span>$className</span>, <span>$charlist</span>, <span>$selectorItem</span><span>) </span><span>51</span> <span> { </span><span>52</span> <span>if</span> (<span>class_exists</span>(<span>$className</span><span>)) { </span><span>53</span> <span>return</span> <span>new</span> <span>$className</span>(<span>$charlist</span>, <span>$selectorItem</span><span>); </span><span>54</span> <span> } </span><span>55</span> <span>throw</span> <span>new</span> <span>Exception</span>("class not exists", 0<span>); </span><span>56</span> <span> } </span><span>57</span> }
For test-driven code programming, please refer to:
The same principle is also used. Write the test code first, and then complete the MatchExector code.
The main function of MatchExector is to calculate the matching degree.
1. If there is only one word in the blacklist, the matching degree will definitely be zero.
2. If it is a core word, then calculate it according to the algorithm mentioned before, please refer to: Teach you step-by-step keyword matching project (search engine) ---- Day 19
<span> 1</span> <?<span>php </span><span> 2</span> <span>#</span><span>@Filename:mathes/MatchExector.php</span> <span> 3</span> <span>#</span><span>@Author:oshine</span> <span> 4</span> <span> 5</span> <span>class</span><span> MatchExector { </span><span> 6</span> <span> 7</span> <span>public</span> <span>static</span> <span>function</span> match(KeywordEntity <span>$keywordEntity</span>,CharList <span>$charlist</span><span>){ </span><span> 8</span> <span> 9</span> <span>$matchingDegree</span> = 0<span>; </span><span>10</span> <span>$elementWords</span> = <span>$keywordEntity</span>-><span>getElementWords(); </span><span>11</span> <span>foreach</span> (<span>$elementWords</span> <span>as</span> <span>$word</span><span>) { </span><span>12</span> <span>#</span><span> code...</span> <span>13</span> <span>if</span>(<span>in_array</span>(<span>$word</span>, <span>$charlist</span>-><span>getBlacklist())) </span><span>14</span> <span>return</span> <span>false</span><span>; </span><span>15</span> <span>if</span>(<span>in_array</span>(<span>$word</span>, <span>$charlist</span>-><span>getCore())) </span><span>16</span> <span>$matchingDegree</span>+=<span>$keywordEntity</span>->calculateWeight(<span>$word</span><span>); </span><span>17</span> <span>18</span> <span> } </span><span>19</span> <span>20</span> <span>if</span>(<span>$matchingDegree</span>>0.8<span>) </span><span>21</span> <span>return</span> <span>true</span><span>; </span><span>22</span> <span>return</span> <span>false</span><span>; </span><span>23</span> <span>24</span> <span> } </span><span>25</span> <span>26</span> }
Relatively speaking, the entire code has achieved the functions it should have. Xiao Shuaishuai is very happy, because after the project is completed, there will definitely be a project bonus, and maybe a rich dinner,
My mouth is watering just thinking about it.
Xiao Shuai Shuai handed the code to Boss Yu and waited expectantly for Boss Yu’s final affirmation.
What will be Boss Yu’s reaction after reading it? Please pay attention to Chapter 3: In-depth study of keyword matching projects (1)
Chapter 2 has been completed, source code address: Teach you step by step how to do keyword matching projects (Chapter 2 completed)