Use Node.js to segment text content and extract keywords

Use Node.js to segment text content and extract keywords

Articles translated by Zhongcheng have tags. Users can quickly filter articles of interest based on tags, and articles can also be recommended based on tag associations. However, the tags of Zhongcheng Translation are set when recommending articles, and they are all in English. Moreover, manual settings are inevitably irregular and incomplete. Although articles can be manually edited after publication, we cannot expect users or administrators to edit appropriate tags all the time, so we need to use tools to automatically generate tags.

Among the current open source word segmentation tools, jieba is a powerful and high-performance word segmentation component. Fortunately, it has a node version.

The installation and use of nodejieba is very simple:

  1. npm install nodejieba
  1. var nodejieba = require( "nodejieba" );
  2. var result = nodejieba.cut( "Imperialism wants to divide up our sweet potatoes" );
  3. console.log(result);
  4. //[ 'Imperialism', 'want', 'to', 'divide', 'our', 'land', 'take', 'away' ]  
  5.  
  6. result = nodejieba.cut( 'Landlord, where is my golden hoop?' );
  7. console.log(result);
  8. //[ 'Land', ',', 'I', 'Old', 'Sun', 'of', 'golden hoop', 'where', '? ' ]  
  9.  
  10. result = nodejieba.cut( 'Great Sage, your golden hoop stick is great because it matches your head shape!' );
  11. console.log(result);
  12. //['Monkey King',','your','golden hoop','is','great','especially','match','your','head','!' ]  

We can load our own dictionary and set the weight and part of speech for each word in the dictionary:

Edit user.uft8

  1. Sweet potato 9999 n
  2. Golden Hoop 9999 n
  3. The best part is 9999

Then load the dictionary through nodejieba.load.

  1. var nodejieba = require( "nodejieba" );
  2.  
  3. nodejieba.load({
  4. userDict: './user.utf8' ,
  5. });
  6.  
  7. var result = nodejieba.cut( "Imperialism wants to divide up our sweet potatoes" );
  8. console.log(result);
  9. //[ 'Imperialism', 'want', 'to', 'our', 'sweet potatoes', 'divide', 'get rid of' ]  
  10.  
  11. result = nodejieba.cut( 'Landlord, where is my golden hoop?' );
  12. console.log(result);
  13. //[ 'Land', ',', 'I', 'Old', 'Sun', 'of', 'golden hoop', 'where', '? ' ]  
  14.  
  15. result = nodejieba.cut( 'Great Sage, your golden hoop stick is great because it matches your head shape!' );
  16. console.log(result);
  17. //[ 'Monkey King', ',', 'You', 'Your', 'Golden Hoop', 'It's great because', 'It's special', 'matches', 'You', 'Your', 'Head shape', '! ' ]  

In addition to word segmentation, we can use nodejieba to extract keywords:

  1. const content = `
  2. HTTP, HTTP/2, and performance optimization
  3.  
  4. The purpose of this article is to tell you through comparison why you should migrate from HTTP to HTTPS and why you should add support for HTTP/2. Before comparing HTTP and HTTP/2, let's take a look at what HTTP is.
  5.  
  6. What is HTTP
  7. HTTP is a set of rules for communicating on the World Wide Web. HTTP is an application layer protocol that runs on top of the TCP/IP layer. When a user requests a web page through a browser, HTTP is responsible for processing the request and establishing a connection between the web server and the client.
  8.  
  9. With HTTP/2, performance can be improved by not using sprites, compression, or concatenation. However, this does not mean that these techniques should not be used. However, it clearly shows the need to move from HTTP/1.1 to HTTP/2.
  10. `;
  11.  
  12. const nodejieba = require( "nodejieba" );
  13.  
  14. const result = nodejieba.extract(content, 20);
  15.  
  16. console.log(result);

The output is similar to the following:

  1. [ { word: 'HTTP' , weight: 140.8704516850025 },
  2. { word: 'request' , weight: 14.23018001394 },
  3. { word: 'should' , weight: 14.052171126120001 },
  4. { word: 'World Wide Web' , weight: 12.2912397395 },
  5. { word: 'TCP' , weight: 11.739204307083542 },
  6. { word: '1.1' , weight: 11.739204307083542 },
  7. { word: 'Web' , weight: 11.739204307083542 },
  8. { word: 'Sprite' , weight: 11.739204307083542 },
  9. { word: 'HTTPS' , weight: 11.739204307083542 },
  10. { word: 'IP' , weight: 11.739204307083542 },
  11. { word: 'Application layer' , weight: 11.2616203224 },
  12. { word: 'client' , weight: 11.1926274509 },
  13. { word: 'browser' , weight: 10.8561552143 },
  14. { word: 'splice' , weight: 9.85762638414 },
  15. { word: 'comparison' , weight: 9.5435285574 },
  16. { word: 'webpage' , weight: 9.53122979951 },
  17. { word: 'server' , weight: 9.41204128224 },
  18. { word: 'use' , weight: 9.03259988558 },
  19. { word: 'necessity' , weight: 8.81927328699 },
  20. { word: 'Add' , weight: 8.0484751722 } ]

We add some new keywords to the dictionary:

  1. performance
  2. HTTP/2

The output is as follows:

  1. [ { word: 'HTTP' , weight: 105.65283876375187 },
  2. { word: 'HTTP/2' , weight: 58.69602153541771 },
  3. { word: 'request' , weight: 14.23018001394 },
  4. { word: 'should' , weight: 14.052171126120001 },
  5. { word: 'Performance' , weight: 12.61259281884 },
  6. { word: 'World Wide Web' , weight: 12.2912397395 },
  7. { word: 'IP' , weight: 11.739204307083542 },
  8. { word: 'HTTPS' , weight: 11.739204307083542 },
  9. { word: '1.1' , weight: 11.739204307083542 },
  10. { word: 'TCP' , weight: 11.739204307083542 },
  11. { word: 'Web' , weight: 11.739204307083542 },
  12. { word: 'Sprite' , weight: 11.739204307083542 },
  13. { word: 'Application layer' , weight: 11.2616203224 },
  14. { word: 'client' , weight: 11.1926274509 },
  15. { word: 'browser' , weight: 10.8561552143 },
  16. { word: 'splice' , weight: 9.85762638414 },
  17. { word: 'comparison' , weight: 9.5435285574 },
  18. { word: 'webpage' , weight: 9.53122979951 },
  19. { word: 'server' , weight: 9.41204128224 },
  20. { word: 'use' , weight: 9.03259988558 } ]

On this basis, we use a whitelist approach to filter out some words that can be used as tags:

  1. const content = `
  2. HTTP, HTTP/2, and performance optimization
  3.  
  4. The purpose of this article is to tell you through comparison why you should migrate from HTTP to HTTPS and why you should add support for HTTP/2. Before comparing HTTP and HTTP/2, let's take a look at what HTTP is.
  5.  
  6. What is HTTP
  7. HTTP is a set of rules for communicating on the World Wide Web. HTTP is an application layer protocol that runs on top of the TCP/IP layer. When a user requests a web page through a browser, HTTP is responsible for processing the request and establishing a connection between the web server and the client.
  8.  
  9. With HTTP/2, performance can be improved by not using sprites, compression, or concatenation. However, this does not mean that these techniques should not be used. However, it clearly shows the need to move from HTTP/1.1 to HTTP/2.
  10. `;
  11.  
  12. const nodejieba = require( "nodejieba" );
  13.  
  14. nodejieba.load({
  15. userDict: './user.utf8' ,
  16. });
  17.  
  18. const result = nodejieba.extract(content, 20);
  19.  
  20. const tagList = [ 'HTTPS' , 'HTTP' , 'HTTP/2' , 'Web' , 'Browser' , 'Performance' ];
  21.  
  22. console.log(result.filter(item => tagList.indexOf(item.word) >= 0));

***get:

  1. [ { word: 'HTTP' , weight: 105.65283876375187 },
  2. { word: 'HTTP/2' , weight: 58.69602153541771 },
  3. { word: 'Performance' , weight: 12.61259281884 },
  4. { word: 'HTTPS' , weight: 11.739204307083542 },
  5. { word: 'Web' , weight: 11.739204307083542 },
  6. { word: 'browser' , weight: 10.8561552143 } ]

This is what we want.

The above is the basic usage of the nodejieba word segmentation library. In the future, we can use it to automatically analyze and add corresponding tags to the translations published by Zhongcheng Translation to provide a better user experience for translators and readers.

<<:  A brief introduction to MVP's practical exercises to make the code structure simpler~

>>:  Analysis and application of WebView cache principle

Recommend

Italy has 3526 new deaths and 345 deaths, with a total of 2503 deaths

According to the latest news from the Dutch media...

How should colleges and universities carry out promotion and operation?

College students have 37 million potential users,...

Creating QR codes using Google's Web API

Google Charts can be queried via POST (see here f...

Tik Tok promotion and monetization operation tutorial!

This video software, which has become extremely p...

Ant Financial is Alibaba's main weapon against Tencent

I have always believed that the outside world has...

Practical application of Internet finance: Who touched your promotion fees?

If you are attracting new customers for an Intern...

It was once the "twin star" of the Earth, but it has a completely different fate

Venus is a terrestrial planet in the solar system...

Soul Product Analysis

Socializing with strangers is something that many...

How can event operations improve user conversion?

Someone asked me: I have been working in the comm...