Hit synonym word forest python usage example

Source: Internet
Author: User

Hit the synonym of the word forest, should be the product of the last century, the word is relatively old, but at least can also use synonyms of the role of the word forest, with the Word2vec to obtain similar function comparison, the two play a comparative effect, see the specific application bar1.First download the TXT,CSDN with synonyms on the link: http://download.csdn.net/download/answerme11/73077712.The contents of the TXT are as follows:    each line is composed of a series of entriesThe letters and numbers of each line of the outfit indicate the category"=" stands for "equal" and "synonymous";“#"On behalf of" unequal ","category ", belonging to the relevant words;      "@" stands for "self-containment", "independence", which in the dictionary neitherThere are synonyms, there are no related words.  
3.python calls the synonym for the word forest Code
   
 
  1. def get_sym(w,word_set):
  2. # w: input word
  3. # word_set: 同义词词集或相关词词集
  4. results=[]
  5. if(len(w)==1):
  6. for each in word_set:
  7. for word in each:
  8. if w == word:
  9. results.append(each)
  10. break
  11. else:
  12. for each in word_set:
  13. for word in each:
  14. if w in word:
  15. results.append(each)
  16. break
  17. return results
  18. f=open(‘同义词.txt‘,‘r‘)
  19. lines=f.readlines()
  20. sym_words=[]
  21. sym_class_words=[]
  22. # 从txt中获取词条,构建同义词词集sym_words和相关词词集sym_class_words
  23. for line in lines:
  24. line=line.replace(‘\n‘,‘‘)
  25. items=line.split(‘ ‘)
  26. index=items[0]
  27. if(index[-1]==‘=‘):
  28. sym_words.append(items[1:])
  29. if (index[-1] == ‘#‘):
  30. sym_class_words.append(items[1:])
  31. print(sym_words)
  32. print(64*‘*‘)
  33. print(sym_class_words)
  34. while True:
  35. w=input()
  36. print(‘同义词‘,66*‘*‘)
  37. print(get_sym(w, sym_words))
  38. print(‘同类词‘, 66 * ‘*‘)
  39. print(get_sym(w, sym_class_words))
Operation Result:



 


Null

List of attachments

    Hit synonym word forest python usage example

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.