API Reference
HBaseOps
Bases: object
demo key = 'test' db = HBaseHelper(host=hbase_host) data = db.query_single_line(table='table', row_key=key) print(data)
Source code in src/nlpertools/data_client.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
MongoOps
Bases: object
Source code in src/nlpertools/data_client.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
fetch_all()
读取所有数据
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/data_client.py
80 81 82 83 84 85 86 87 88 89 90 | |
load_from_mongo(special_value)
读取mongodb该special_value下所有值为special_value的数据
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/data_client.py
92 93 94 95 96 97 98 99 100 101 102 103 104 | |
save_to_mongo(special_value, each_item)
数据存入mongo
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
special_value |
required | ||
each_item |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/data_client.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
update_to_mongo(condition_term, condition_value, new_value)
根据提供的字段和值,查询出对应的数据,更新数据存入mongo 类似 updata
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition_term |
条件字段term |
required | |
condition_value |
条件字段值 |
required | |
new_value |
新的值。最好是dict,不是dict的话不知道行不行 |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/data_client.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
CopyFunc
Source code in src/nlpertools/dataprocess.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |
is_chinese_char(cp)
Checks whether CP is the codepoint of a CJK character.
Source code in src/nlpertools/dataprocess.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
Pattern
pattern_special_char = re.compile("[{}{}]".format(pattern_special_char_x[1:-1], pattern_special_char_u[1:-1])) a = "asdasdas v啊实打实 v阿松大 " res = re.sub(pattern_special_char, "$",a)
Source code in src/nlpertools/dataprocess.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
TextProcess
Bases: object
数据处理类 这是基类,如果是定制化的语言处理,请继承该类
Source code in src/nlpertools/dataprocess.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
__init__(patterns_filter=None, patterns_replace=None, words_filter=[])
pattern_list:
Source code in src/nlpertools/dataprocess.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
filter_exclusive(text)
去除 @、 #、 表情等twitter、微博“特有”的情况
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/dataprocess.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
filter_html(text)
过滤html标签
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/dataprocess.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
filter_html_special(text)
替换所有html转义字符 这个好像只有新闻有?
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/dataprocess.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
filter_pattern(text)
返回True表示命中规则,需要过滤
Source code in src/nlpertools/dataprocess.py
512 513 514 515 516 517 518 519 520 521 | |
full2half(text)
全角转化为半角
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/dataprocess.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
split_sentence(sentence, language='chinese')
staticmethod
分句,英文有nltk,中文怎么能没有好的分句工具呢
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence |
required | ||
language |
'chinese'
|
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/dataprocess.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
trandition2simple(text)
https://juejin.cn/post/7234554420163100728
Source code in src/nlpertools/dataprocess.py
309 310 311 312 313 314 315 316 317 | |
uniform_whitespace(document, whitespace=[' ', '\u2009', '\u200a', '\u202f', '\u2005', '\u3000', '\u2002', '\xa0', '\u2008', '\u2003', '', '\x84'])
staticmethod
There are different whitespace characters.
Source code in src/nlpertools/dataprocess.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
DataAnalysis
Source code in src/nlpertools/ml.py
20 21 22 23 24 25 26 27 28 29 30 31 32 | |
draw_pic(df, save_path)
staticmethod
画直方图,对比两个不同类别差异
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
pd.DataFrame |
required | |
save_path |
str |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/ml.py
21 22 23 24 25 26 27 28 29 30 31 32 | |
STEM
Bases: object
Source code in src/nlpertools/ml.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
start_by_srl(sentence)
用语义角色标注工具
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence |
"他叫汤姆去拿外衣。" |
required |
Returns:
| Type | Description |
|---|---|
events: [['他', '叫', '汤姆', '去', '拿', '外衣'], ['汤姆', '拿', '外衣']] |
Source code in src/nlpertools/ml.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
convert_crf_format_10_fold(corpus, objdir_path)
把已经是crf格式的数据,分成十折。 para:
Source code in src/nlpertools/ml.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
kfold(corpus, path, k=9, is_shuffle=True)
k是10份中训练集占了几份
Source code in src/nlpertools/ml.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
label(text, labels)
返回两列的标记数据序列
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
required | ||
labels |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/ml.py
175 176 177 178 179 180 181 182 183 184 | |
read_seq_res(path, labels)
读序列标注三列数据的方法
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
required | ||
labels |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/ml.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
seed_everything(seed=7777777)
设置整个开发环境的seed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed |
7777777
|
||
device |
required |
Returns:
| Type | Description |
|---|---|
None
|
Source code in src/nlpertools/ml.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
split_sentence(sentence, language='chinese', cross_line=True)
分句,英文有nltk,中文怎么能没有好的分句工具呢
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentence |
required | ||
language |
'chinese'
|
||
cross_line |
True
|
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/ml.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
subject_object_labeling(spo_list, text)
百度那种有spo字典的数据,给标成。草,看不懂,得找找哪里用的
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spo_list |
required | ||
text |
required |
Returns:
| Type | Description |
|---|---|
labeling_list |
Source code in src/nlpertools/ml.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
GaussDecay
Bases: object
当前只实现了时间的,全部使用默认值
Source code in src/nlpertools/other.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
calc_gauss(raw_score, field_value)
$$S(doc)=exp(-rac{max(0,|fieldvalues_{doc}-origin|-offset)^2}{2σ^2})$$ - $$σ^2=-scale^2/(2·ln(decay))$$
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_score |
required | ||
field_value |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/other.py
350 351 352 353 354 355 356 357 358 359 360 361 362 | |
translate(scale, offset)
将领域的输入转化为标准
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/other.py
325 326 327 328 329 330 331 332 333 334 335 336 | |
auto_close()
针对企业微信15分钟会显示离开的机制,假装自己还在上班
Source code in src/nlpertools/other.py
275 276 277 278 279 280 281 282 283 284 285 286 287 | |
camel_to_snake(s)
将 camel case 转换到 snake case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s |
str
|
camel case variable |
required |
Returns:
| Type | Description |
|---|---|
str
|
Source code in src/nlpertools/other.py
128 129 130 131 132 133 134 | |
git_push()
针对国内提交github经常失败,自动提交
Source code in src/nlpertools/other.py
103 104 105 106 107 108 109 110 111 112 113 114 115 | |
snake_to_camel(s)
author: u 将 snake case 转换到 camel case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s |
str
|
snake case variable |
required |
Returns:
| Type | Description |
|---|---|
str
|
Source code in src/nlpertools/other.py
118 119 120 121 122 123 124 125 | |
spider(url)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url |
required |
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/other.py
187 188 189 190 191 192 193 194 195 196 197 198 199 | |
EmailClient
Bases: object
Source code in src/nlpertools/plugin.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
sent_email(title, content)
mail_user = 'xxx'
mail_pass = 'xxx'
receiver = 'xxx'
sent_email(mail_user, mail_pass, receiver)
Source code in src/nlpertools/plugin.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
convert_import_string_to_import_list(text)
该方法将 import 转变为 try import
Source code in src/nlpertools/utils_for_nlpertools.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
fn_async_timer(function)
针对异步函数的装饰器
Source code in src/nlpertools/wrapper.py
10 11 12 13 14 15 16 17 18 19 20 21 22 | |
fn_timeout_checker(wait_time, callback)
超时判断的装饰器 两个包,使用gevent出现bug
Source code in src/nlpertools/wrapper.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
fn_timer(async_func=False, analyse=False)
@fn_timer() def example(): time.sleep(2)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analyse |
False
|
Returns:
| Type | Description |
|---|---|
Source code in src/nlpertools/wrapper.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
fn_try(parameter)
该函数把try...catch...封装成装饰器, 接收一个字典参数,并把其中的msg字段改为具体报错信息
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameter |
{"msg": "", etc.} |
required |
Returns:
| Type | Description |
|---|---|
parameter: {"msg": 内容填充为具体的报错信息, etc.} |
Source code in src/nlpertools/wrapper.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |