Web front-end style code generation method based on DOM
Through a DOM-based method, web page themes and block matching style templates are extracted to directly generate Web front-end style code, which solves the problems of low style code generation efficiency and insufficient content description in the existing technology, and achieves more efficient and beautiful CSS. Code generation.
Patent Information
- Application Number
- CN202410584258.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-11
- Publication Date
- 2024-09-10
AI Technical Summary
When the existing technology generates Web front-end style code, it is only based on the HTML structure and fails to explore the hierarchical requirements of the web page content in detail. As a result, the generated style code cannot fully describe the web page content, and the style template combination process is time-consuming, requires a lot of computing power, and is inefficient. Low.
Use a DOM-based method to build a style template library, extract web page text content through the DOM tree, remove stop words, determine the web page theme, match style templates in blocks, reduce template combinations, and directly generate the overall CSS code.
The efficiency of code generation is improved, the generated CSS code can better reflect the content of the web page, the computing power investment and time are reduced, the development efficiency is improved, and the generated style is more readable and beautiful.
Smart Images

Figure CN118626065A
Abstract
Description
Technical Field
[0001] The invention belongs to the field of computer technology, and in particular relates to a DOM-based Web front-end style code generation method. Background Art
[0002] In the Web front end, HTML code displays the content of the web page, and CSS code is the style code that displays the style of the web page. The current Web front end code generation work mainly generates HTML code, which sometimes comes with some simple style code, namely CSS code.
[0003] Some work is based on the model-driven approach, focusing on the establishment of models, using various models to analyze the various stages of front-end development, obtaining the system abstract structure, and implementing Web front-end code generation based on the model's code mapping rules. Unified Modeling Language UML is a typical representative of this method, and relevant personnel have generated Web front-end code based on business object models.
[0004] Other works use deep learning methods to learn the inherent rules and representation levels of sample data, and can obtain Web front-end code information during the learning process. Relevant personnel use computer vision technology and neural network models to achieve the generation from sketch interface to front-end code.
[0005] The web front-end codes generated by the model-driven method and the deep learning method both contain only a small amount of CSS code, which results in the final web page being simplistic, lacking readability and aesthetics. The effect of CSS code on the final presentation of the web page is self-evident.
[0006] Therefore, some works are based on template methods, which directly generate CSS code according to style description requirements. The principle is to build the unchanged code part into a template file according to the existing code structure and grammar rules, and write the variable part into a specific position in the template in the form of placeholder symbols. The CSS code generator of online web pages is the main product of this kind of work. Users can change the CSS property value by dragging or entering parameters, and then the generator synthesizes the inherent template and the changed value to provide the corresponding CSS code. However, this method can only generate CSS code for a single attribute.
[0007] Generating CSS code is conducive to improving the efficiency of Web front-end development, and can also provide more style description support for the simple web pages generated by Web front-end code. At present, there are few works that can generate overall CSS code. The existing scheme closest to the present invention is mainly the FaceOff method. FaceOff generates CSS code by searching for Web front-end style templates and using neural network models to recommend template combinations based on the principles of templates and deep learning. First, a style template library is built based on popular websites and professional design example web pages. Secondly, for a web page that needs to be styled, FaceOff divides it into multiple blocks, and retrieves the style template for each block according to the structure of each block. The retrieval results match one block with multiple style templates. Subsequently, in order to achieve the unity of style between blocks in the web page, different style templates of different blocks are combined based on the neural network model, so as to screen out the unique corresponding style template for each block. After the style template of each block is uniquely determined, the overall style of the web page can be unified. Finally, the result of the style template combination is output, and the style template of each block is added to the CSS file in sequence to achieve the generation of the overall CSS code.
[0008] There are two defects in the existing technology. In terms of generation requirements, the existing technology only generates style codes based on the HTML structure of the web page itself, without detailed exploration of the requirements of the web page content hierarchy, and the generated style codes cannot well describe the content displayed on the web page. In terms of the code generation process, combining a large number of style templates requires a certain amount of computing power and a long time to achieve style unification. Summary of the invention
[0009] In view of this, the present invention provides a DOM-based Web front-end style code generation method, which can generate overall CSS code.
[0010] The technical solution for implementing the present invention is as follows:
[0011] The method for generating web front-end style code based on DOM includes the following steps:
[0012] Step 1: Build a style template library based on web pages of different themes;
[0013] Step 2: construct a DOM tree of the web page according to the HTML structure of the web page, extract the text content of the web page based on the DOM tree, divide the extracted text into individual words, remove stop words from the words, and determine the web page theme;
[0014] Step 3: Divide the web page into multiple blocks;
[0015] Step 4: Perform structural matching between the block and the style template in the style template library of the corresponding theme. If the matching is successful, the style code in the style template is retained as the style code of the block, and the style template retrieval work is completed;
[0016] Step 5. Add the style code of each block to the CSS file in turn to realize the CSS code output of the entire web page.
[0017] Furthermore, the DOM is a document object model, which is a programming interface for HTML documents. It provides the complete structure and representation of HTML web pages and is used to access and operate the structure, content and style of HTML documents.
[0018] Furthermore, the specific process of step 2 is as follows:
[0019] Step 2.1, parse HTML tags and build the DOM tree of the web page;
[0020] Step 2.2, extract the text nodes of the DOM tree and divide the text into individual words;
[0021] Step 2.3, remove the stop words and redundant information in the words;
[0022] Step 2.4, locate words whose tags are web page URL, main title of web page, titles of different levels within the page, and hyperlinks;
[0023] Step 2.5, set the word scoring standard, and the words in different parts of the web page have different scores;
[0024] Step 2.6: Count the number of all words in the strongly related part of the webpage topic and the frequency of each word, and calculate the score of the word according to the scoring rule;
[0025] Step 2.7: Select the top five words with the highest scores as web page theme keywords and analyze them to obtain the web page theme.
[0026] Furthermore, in step 2.3, stop words are common words that are ignored or deleted during text processing. These words have high frequency of occurrence and low semantic value, including prepositions, conjunctions, articles, and pronouns. It is necessary to first detect the language of the web page text, and then delete the stop words of the text according to the stop word list of the language.
[0027] Furthermore, the word score formula is shown in formula (1), where s represents the score, f represents the word frequency, t represents the score that appears in the strongly related part of the web page topic, and n represents the number of all words in the strongly related part of the web page topic;
[0028] s=0.2×f+t,if n>50
[0029] s = 0.5×f + t, if n≤50 (1).
[0030] Beneficial effects:
[0031] 1. In the process of analyzing HTML, the present invention constructs a DOM tree and accesses the HTML structure content through the DOM tree, which helps to explore potential needs for code generation and more clearly understand the originally cumbersome HTML structure.
[0032] 2. The existing method only matches the HTML block structure and the style template. If the structures are the same, the style code in the style template is established as a style code of the block structure. The present invention also adds a matching element, the web page theme, to first ensure that the web page theme matches, and then compare the similarities and differences of the structure in detail. The web page theme can filter out the style code for the block structure that is more consistent with the web page content.
[0033] 3. The web page theme concept introduced by the method of the present invention is obtained based on the DOM tree and web page information extraction technology, which fully analyzes the main content of the web page. The style retrieval source of each block structure is the template library of the same theme with similar style. There is no need to combine templates, which reduces the code generation time, reduces the computing power investment, and improves the actual development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 Flowchart of the method for generating DOM-based web front-end style code.
[0035] Figure 2 The flowchart for determining the theme of a web page based on DOM. DETAILED DESCRIPTION
[0036] The present invention is described in detail below with reference to the accompanying drawings and embodiments.
[0037] The present invention provides a DOM-based Web front-end style code generation method, the main process of this method is as follows: Figure 1 shown.
[0038] For a web page that needs to generate style code, the HTML structure of the web page is analyzed to generate CSS code. First, a style template library is built based on well-designed web pages of different themes to prepare data for subsequent template retrieval. Then the theme of the web page is determined based on DOM. DOM, or Document Object Model, is a programming interface for HTML documents. It provides the complete structure and representation of HTML for web pages and is used to access and manipulate the structure, content and style of HTML documents. The DOM tree of the web page is built according to the HTML structure of the web page, and then the text content of the web page is extracted based on the DOM tree. The extracted text is divided into individual words, and the stop word information in the words is cleared. After clearing the stop word information that is less related to the main content of the web page, the web page URL, the main title of the web page, the titles of different levels in the page, and the hyperlinks that are strongly related to the web page theme are located. According to the scoring rules, the words in this part are scored, and the top five words with the highest scores are selected as the theme keywords of the web page. Combine the five theme keywords for classification to determine the theme of the web page. After determining the web page theme, the web page is divided into multiple blocks, and the structure of the blocks and style templates are matched in the style template library of the corresponding theme. If the match is successful, the style template is retained as the style code of the block, and the style template retrieval work is completed. Finally, the style code of each block is added to the CSS file in turn to realize the CSS code output of the entire web page.
[0039] The key to analyzing HTML structure to generate CSS code is to extract web page keywords based on DOM and determine the web page theme. Figure 2 shown.
[0040] Step 2.1: Parse HTML tags and build the DOM tree of the web page.
[0041] Step 2.2: Extract the text nodes of the DOM tree and divide the text into individual words.
[0042] Step 2.3: Remove stop words from the words. Stop words are common words that are ignored or deleted during text processing. These words are usually words with high frequency of occurrence and low semantic value, such as prepositions, conjunctions, articles, pronouns, etc. You need to first detect the language of the webpage text, and then delete the stop words of the text according to the stop word list of the language.
[0043] Step 2.4: Locate the words whose tags are the URL of the webpage, the main title of the webpage, the titles of different levels within the page, and the hyperlinks, because these parts of the webpage are strongly related to the theme of the webpage.
[0044] Step 2.5: Establish a word score standard. Words in different parts of a web page have different scores. For example, the score of a word that appears in an h1 title is greater than the score of a word that appears in an h2 title. Now define the h1 title as 6 points, the h2 title, the main title of the web page, and the URL as 5 points, the h3 title as 4 points, and the h4 title, h5 title, h6 title, and hyperlink as 2 points. The word score formula is shown in Formula 1, where s represents the score, f represents the word frequency, t represents the score that appears in the strongly related part of the web page theme, and n represents the number of all words that appear in the strongly related part of the web page theme.
[0045] s=0.2×f+t,if n>50
[0046] s = 0.5×f + t, if n ≤ 50 Equation 1
[0047] For example, when the total number of words n is less than 50, and h4 and the hyperlink appear 10 times in the web page, the score is calculated as: s=0.5×10+2+2=9.
[0048] Step 2.6: Count the number of all words and the frequency of individual words in the strongly related part of the web page, and calculate the score of the word according to the scoring rules.
[0049] Step 2.7: Select the top five words with the highest scores as web page topic keywords, and analyze to obtain the corresponding web page topics.
[0050] In summary, the above are only preferred embodiments of the present invention and are not intended to limit the protection scope of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A DOM-based Web front-end style code generation method, characterized in that: The following steps are involved: Step 1: Build a style template library based on web pages of different themes; Step 2: construct a DOM tree of the web page according to the HTML structure of the web page, extract the text content of the web page based on the DOM tree, divide the extracted text into individual words, remove stop words from the words, and determine the web page theme; Step 3: Divide the web page into multiple blocks; Step 4: Perform structural matching between the block and the style template in the style template library of the corresponding theme. If the matching is successful, the style code in the style template is retained as the style code of the block, and the style template retrieval work is completed; Step 5. Add the style code of each block to the CSS file in turn to realize the CSS code output of the entire web page.
2. The DOM-based Web front-end style code generation method according to claim 1, characterized in that: The DOM is the Document Object Model, which is a programming interface for HTML documents. It provides the complete structure and representation of HTML web pages and is used to access and manipulate the structure, content, and style of HTML documents.
3. The DOM-based Web front-end style code generation method according to claim 1 or 2, characterized in that: The specific process of step 2 is as follows: Step 2.1, parse HTML tags and build the DOM tree of the web page; Step 2.2, extract the text nodes of the DOM tree and divide the text into individual words; Step 2.3, remove the stop words and redundant information in the words; Step 2.4, locate words whose tags are web page URL, main title of web page, titles of different levels within the page, and hyperlinks; Step 2.5, set the word scoring standard, and the words in different parts of the web page have different scores; Step 2.6: Count the number of all words in the strongly related part of the webpage topic and the frequency of each word, and calculate the score of the word according to the scoring rule; Step 2.7: Select the top five words with the highest scores as web page theme keywords and analyze them to obtain the web page theme.
4. The DOM-based Web front-end style code generation method according to claim 3, characterized in that: In step 2.3, stop words are common words that are ignored or deleted during text processing. These words have high frequency of occurrence and low semantic value, including prepositions, conjunctions, articles, and pronouns. It is necessary to first detect the language of the web page text, and then delete the stop words in the text based on the stop word list of that language.
5. The DOM-based Web front-end style code generation method according to claim 3, characterized in that: In step 2.5, the word score formula is shown in formula (1), where s represents the score, f represents the word frequency, t represents the score that appears in the strongly related part of the web page topic, and n represents the number of all words in the strongly related part of the web page topic; s=0.2×f+t,if n>50 s = 0.5×f + t, if n≤50 (1).
Citation Information
Cited By
Page display method and device, electronic equipment and computer readable medium
CN119473289A
