文本预处理库

plane的Python项目详细描述


平面

Build Status

Plane is a tool for shaping wood using muscle power to force the cutting blade over the wood surface.
from Wikipedia

plane(tool) from wikipedia

此包用于从文本中提取或替换特定部分,如url、电子邮件、html标记、电话号码等。还支持标点符号规范化和移除。

请参阅完整的Documents

安装

仅限python3.x

PIP

pipinstallplane

从源安装

python setup.py install

功能

  • 没有其他依赖关系
  • 内置正则表达式模式:plane.pattern.Regex
  • 自定义正则表达式模式
  • 模式组合
  • 提取、替换图案
  • 分段句子
  • 链函数调用:plane.plane.Plane
  • 管道:plane.Pipeline

用法

快速启动

使用regex extractreplace

fromplaneimportEMAIL,extract,replacetext='fake@no.com & fakefake@nothing.com'emails=extract(text,EMAIL)# this return a generator objectforeinemails:print(e)>>>Token(name='Email',value='fake@no.com',start=0,end=11)>>>Token(name='Email',value='fakefake@nothing.com',start=14,end=34)print(EMAIL)>>>Regex(name='Email',pattern='([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-]+)',repl='<Email>')replace(text,EMAIL)# replace(text, Regex, repl), if repl is not provided, Regex.repl will be used>>>'<Email> & <Email>'replace(text,EMAIL,'')>>>' & '

模式

Regex是一个有3个项的命名耦合器:

  • name
  • pattern:正则表达式
  • repl:替换标记,这将在使用replace函数时替换匹配的正则表达式
# create new patternfromplaneimportbuild_new_regexcustom_regex=build_new_regex('my_regex',r'(\d{4})','<my-replacement-tag>')

此外,还可以从默认模式构建新模式。

注意:这只应用于语言范围。

fromplaneimportextract,build_new_regex,CHINESE_WORDSASCII=build_new_regex('ascii',r'[a-zA-Z0-9]+',' ')WORDS=ASCII+CHINESE_WORDSprint(WORDS)>>>Regex(name='ascii_Chinese_words',pattern='[a-zA-Z0-9]+|[\\U00004E00-\\U00009FFF\\U00003400-\\U00004DBF\\U00020000-\\U0002A6DF\\U0002A700-\\U0002B73F\\U0002B740-\\U0002B81F\\U0002B820-\\U0002CEAF\\U0002CEB0-\\U0002EBEF]+',repl=' ')text="自然语言处理太难了!who can help me? (╯▔?▔)╯"print(' '.join([t.valuefortinlist(extract(text,WORDS))]))>>>"自然语言处理太难了 who can help me"fromplaneimportCHINESE,ENGLISH,NUMBERCN_EN_NUM=sum([CHINESE,ENGLISH,NUMBER])text="佛是虚名,道亦妄立。एवं मया श्रुतम्। 1999 is not the end of the world. "print(' '.join([t.valuefortinextract(text,CN_EN_NUM)]))>>>"佛是虚名,道亦妄立。 1999 is not the end of the world."

默认正则表达式:Details

  • URL:仅限ascii
  • EMAIL:本地部分@域
  • TELEPHONE:比如xxx-xxxx-xxxx
  • SPACE, \t\n\r\f\v
  • HTML:html标记、脚本部分和css部分
  • ASCII_WORD:英语单词、数字、<tag>等等。
  • CHINESE:所有中文字符(仅限汉字和标点符号)
  • CJK:所有中文、日文、韩文(cjk)字符和标点符号
  • THAI:所有泰语和标点符号
  • VIETNAMESE:所有vietnames和标点符号
  • ENGLISH:所有英文字符和标点符号
  • NUMBER:0-9
Regex namereplace
URL^{}
EMAIL^{}
TELEPHONE^{}
SPACE^{}
HTML^{}
ASCII_WORD^{}
CHINESE^{}
CJK^{}

segment可以用来分割句子,英语和数字如'ps4'将被保留,其他如中文'中文字'将被拆分为单个单词格式['中', '文']

fromplaneimportsegmentsegment('你看起来guaiguai的。<EOS>')>>>['你','看','起','来','guaiguai','的','。','<EOS>']

标点符号

punc.remove将把所有unicode标点替换为' '或作为参数发送到此函数的内容replpunc.normalize将一些unicode标点标准化为英文标点。

注意:'+'、'^'、'$'、'~'和一些字符不是标点符号。

fromplaneimportpunctext='Hello world!'punc.remove(text)>>>'Hello world '# replace punctuation with special stringpunc.remove(text,'<P>')>>>'Hello world<P>'# normalize punctuationspunc.normalize('你读过那本《边城》吗?什么编程?!人生苦短,我用 Python。')>>>'你读过那本(边城)吗?什么编程?!人生苦短,我用 Python.'

链函数

Plane包含extractreplacesegmentpunc.removepunc.normalize,这些方法可以在链中调用。由于segment返回list,因此只能在链的末尾调用它。

Plane.text保存处理文本的结果,Plane.values保存提取字符串的结果。

fromplaneimportPlanefromplane.patternimportEMAILp=Plane()p.update('My email is my@email.com.').replace(EMAIL,'').text# update() will init Plane.text and Plane.values>>>'My email is .'p.update('My email is my@email.com.').replace(EMAIL).segment()>>>['My','email','is','<Email>','.']p.update('My email is my@email.com.').extract(EMAIL).values>>>[Token(name='Email',value='my@email.com',start=12,end=24)]

管道

如果您愿意,可以使用Pipeline

segmentextract只能在最后出现。

fromplaneimportPipeline,replace,segmentfromplane.patternimportURLpipe=Pipeline()pipe.add(replace,URL,'')pipe.add(segment)pipe('http://www.guokr.com is online.')>>>['is','online','.']

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java有没有一种方法可以比较两个html文件以跟踪更改   Android中复杂WCF响应的java解析   java如何使用onSaveInstanceState保存复选框状态   java使用流api将流<@Nullable T>转换为流<@NonNull T>   java我需要使用可比较的界面比较两个形状,以确定哪个比另一个大或小   java在返回可能的alreadyset实例时,DeferredResult是否有竞争条件?   Java:FTP服务器列表的正则表达式?   java如何连接数组   java填充屏幕Android不能使用S3 S4屏幕分辨率   用于Java的安卓mapsutils?   安卓运行时异常:无法启动活动组件信息:java。lang.NullPointerException致命异常   java JComboBox列出了对象ArrayList中的字符串   java如何在遗传算法中表示时间表问题的时间表?   Java中空指针异常的帮助   由于找不到列,java Hibernate联接表策略多态性不起作用   java元映射运行本地raise“查询Prolog服务器时出错:连接被拒绝”   java是否创建一个具有不同目标对象的方法引用列表?   java所有线程都被锁定了?