Home  >  Article  >  Backend Development  >  Solution to Python user comment tag matching

Solution to Python user comment tag matching

不言
不言Original
2018-06-04 17:55:421900browse

This article mainly introduces the solution of Python user comment tag matching in detail, which has certain reference value. Interested friends can refer to it

We observed user comments and found: attribute words They often appear together with emotional words because users usually express emotions when describing attributes, and attributes are the objects of emotional expression. It was also found that attribute words and special emotion words are basically nouns or adjectives (formal predicates).

The algorithm flow chart is as follows:

The comment data is as follows:

The code is as follows:

#encoding=utf-8 
 
############################# 
# 
# 功能:给定一些中文的产品评论,希望从中找到评价对象及评价词。 
# 
# @author:licl 
# 
############################## 
 
fdata = open('JD_DFB_comments_out.txt','r') 
Output = open('Pattern_Result.txt','a') 
 
try: 
 data = fdata.readlines() 
  
 listline = [] 
  
  
 for line in data: 
   
  listline = line.replace(" ","/") 
  listline = listline.split("/") 
   
  i = 1 
   
  while i < len(listline): 
    
   if listline[i] != "名词": 
     
    i = i+2 
     
   else: 
     
    new_list = ["","",""] 
    new_list[0] = listline[i-1] 
     
    a = i-1 
    i = i+2 
     
    while i < len(listline):     
      
     if listline[i] == "标点": 
       
      i = i+2 
      break 
      
     else: 
       
      if listline[i-1]==&#39;不&#39; or listline[i-1]==&#39;不怎么样&#39; or listline[i-1]==&#39;不怎么&#39; or listline[i-1]==&#39;不太&#39;:  
       new_list[1] = listline[i-1] 
      if listline[i] == "形容词" or listline[i] == "形谓词": 
       new_list[1] += listline[i-1] 
        
       b = i-1     
       t = (b-a)/2   
       new_list[2] = str(t) 
       for line in new_list: 
         
        Output.write(line + " ") 
         
       Output.write("\n")  
       break 
       
      else: 
      
       i = i+2  
except: 
 print "‘文件不存在&#39;或者‘文件无法打开&#39;" 
   
finally: 
  
 fdata.close() 
 Output.close()

Related recommendations:

Example explanation of python user management system

The above is the detailed content of Solution to Python user comment tag matching. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn