nlp中文数据预处理方法是什么

发布时间:2021-12-16 09:34:59 作者:iii
来源:亿速云 阅读:207

这篇文章主要介绍“nlp中文数据预处理方法是什么”,在日常操作中,相信很多人在nlp中文数据预处理方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”nlp中文数据预处理方法是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

  数据加载(默认csv格式)

  import pandas as pd

  datas = pd.read_csv("./test.csv", header=0, index_col=0) # DataFrame

  n_datas = data.to_numpy() # ndarray 转成numpy更好处理(个人喜好)

  去除空行

  def delete_blank_lines(sentences):

  return [s for s in sentences if s.split()]

  no_line_datas = delete_blank_lines(n_datas)

  去除数字

  DIGIT_RE = re.compile(r'\d+')

  no_digit_datas = DIGIT_RE.sub('', no_line_datas)

  def delete_digit(sentences):

  return [DIGIT_RE.sub('', s) for s in sentences]

  判断句子形式(简单句或者复杂句)

  STOPS = ['。', '.', '?', '?', '!', '!'] # 中英文句末字符

  def is_sample_sentence(sentence):

  count = 0

  for word in sentence:

  if word in STOPS:

  count += 1

  if count > 1:

  return False

  return True

  去除中英文标点

  from string import punctuation

  import re

  punc = punctuation + u'

  def delete_punc(sentences):

  return [re.sub(r"[{}]+".format(punc), '', s) for s in a]

  去除英文(仅留汉字)

  ENGLISH_RE = re.compile(r'[a-zA-Z]+')

  def delete_e_word(sentences):

  return [ENGLISH_RE.sub('', s) for s in sentences]

  去除乱码和特殊符号

  使用正则表达式去除相关无用符号和乱码

  # 该操作可以去掉所有的符号,标点和英文,由于前期可能需要标点进一步判断句子是否为简单句,所以该操作可以放到最后使用。郑州做妇科检查价格 http://www.zzkdfk.com/

  SPECIAL_SYMBOL_RE = re.compile(r'[^\w\s\u4e00-\u9fa5]+')

  def delete_special_symbol(sentences):

  return [SPECIAL_SYMBOL_RE.sub('', s) for s in sentences]

  中文分词

  # 使用jieba

  def seg_sentences(sentences):

  cut_words = map(lambda s: list(jieba.cut(s)), sentences)

  return list(cut_words)

  # 使用pyltp分词

  def seg_sentences(sentences):

  segmentor = Segmentor()

  segmentor.load('./cws.model') # 加载分词模型参数

  seg_sents = [list(segmentor.segment(sent)) for sent in sentences]

  segmentor.release()

  return seg_sents

  去除停用词

  # 停用词列表需要自行下载

  stopwords = []

  def delete_stop_word(sentences):

  return [[word for word in s if word not in stopwords] for s in sentences]

到此,关于“nlp中文数据预处理方法是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 数据预处理
  2. dem数据预处理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nlp

上一篇:html5新增加的结构标签是什么

下一篇:Linux sftp命令的用法是怎样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》