You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_fastNLP.py 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import sys
  2. sys.path.append("..")
  3. from fastNLP.fastnlp import FastNLP
  4. from fastNLP.fastnlp import interpret_word_seg_results
  5. PATH_TO_CWS_PICKLE_FILES = "/home/zyfeng/fastNLP/reproduction/chinese_word_segment/save/"
  6. def word_seg():
  7. nlp = FastNLP(model_dir=PATH_TO_CWS_PICKLE_FILES)
  8. nlp.load("cws_basic_model", config_file="cws.cfg", section_name="POS_test")
  9. text = ["这是最好的基于深度学习的中文分词系统。",
  10. "大王叫我来巡山。",
  11. "我党多年来致力于改善人民生活水平。"]
  12. results = nlp.run(text)
  13. print(results)
  14. for example in results:
  15. words, labels = [], []
  16. for res in example:
  17. words.append(res[0])
  18. labels.append(res[1])
  19. print(interpret_word_seg_results(words, labels))
  20. def text_class():
  21. nlp = FastNLP("./data_for_tests/")
  22. nlp.load("text_class_model")
  23. text = "这是最好的基于深度学习的中文分词系统。"
  24. result = nlp.run(text)
  25. print(result)
  26. print("FastNLP finished!")
  27. def test_word_seg_interpret():
  28. foo = [[('这', 'S'), ('是', 'S'), ('最', 'S'), ('好', 'S'), ('的', 'S'), ('基', 'B'), ('于', 'E'), ('深', 'B'), ('度', 'E'),
  29. ('学', 'B'), ('习', 'E'), ('的', 'S'), ('中', 'B'), ('文', 'E'), ('分', 'B'), ('词', 'E'), ('系', 'B'), ('统', 'E'),
  30. ('。', 'S')]]
  31. chars = [x[0] for x in foo[0]]
  32. labels = [x[1] for x in foo[0]]
  33. print(interpret_word_seg_results(chars, labels))
  34. if __name__ == "__main__":
  35. word_seg()

一款轻量级的自然语言处理(NLP)工具包,目标是减少用户项目中的工程型代码,例如数据处理循环、训练循环、多卡运行等