如何使用 jieba 断词工具的 jieba.suggest_freq 设定 URL 不被切开(设定字串范围)

我有一个CSV档,是关于论坛内容

现在我需要对CSV资料进行分析和使用断词工具断开中文句子

以下是论坛内容的例子:

我使用了以下代码撰写,但是不知道得出的结果是错误原因:

from datetime import date,datetime
import csv
import codecs
import time
import re
import sys
import os
import jieba

jieba.load_userdict('./data/dict.txt')
file_name = 'head300.csv'
jieba.suggest_freq('李嘉誠',True)
jieba.suggest_freq('http:.*? ',True)

with open (file_name, 'r', encoding="utf-8") as csvfile:

reader = csv.reader(csvfile)

column = [row[4] for row in reader]

content_list = list(column)

print("Total: "+str(len(content_list)))
for no_list in range(0,Total_List):
    print(content_list[no_list])
    content_output = jieba.cut(content_list[no_list])
    print_list = '/'.join(content_output) + '\n'
    with open('seg.csv', 'a', newline='', encoding='utf-8') as csvfile:
        csvfile.write(print_list)

我需要设定URL不会被断开,我尝试了使用以下代码,但不成功:

jieba.suggest_freq('http:.*? ',True)

我知道 replace 功能可以使用 .*? 获取特定范围。但是,我不知道如何在suggest设定URL的范围。我需要在 jieba.suggest_freq 功能 找到头4个字元 http 一直到最后一个字元 (空白格)

以下是我预期的需要的结果(URL不会被断开):

救援/人員/到場//發現//2//女子/不適///安排/救護/車將///送院/治理//l/in/k/:/ /https://hk.on.cc/hk/bkn/cnt/news/20200110/bkn-20200110181321251-0110_00822_001.html / /受事//影響//警方//部分/行人/路段/封鎖///消防/人員//調查///初步/相信/事件/並無/可疑///氣體/源頭/// 
調查/

希望大家可以帮到我,谢谢喔❤

Jason990420
最佳答案

內定就是侦测第一个空白格, 我试了一下, OK啊

column = ['光绪年间有这样一个真实的故事。 https://ibook.idv.tw/enews/enews1531-1560/enews1536.html 江苏的贾先生,在上海租界一洋行工作,深得老板信任。 https://ibook.idv.tw/enews/enews1531-1560/enews1536.html 端午节前,老板派他去城南一带收欠款,他带上皮袋子就出发了。 ']
print(f"Total: {len(column)}")

for item in column:
    # print(item)
    content_output = cut_all(url_split(item))
    print_list = '/'.join(content_output) + '\n'
    #with open('seg.csv', 'a', newline='', encoding='utf-8') as csvfile:
    # csvfile.write(print_list)
    print(print_list)
Total: 1
光绪/年间//这样/一个/真实//故事//https://ibook.idv.tw/enews/enews1531-1560/enews1536.html/ /江苏///先生///上海/租界//洋行/工作//深得///信任//https://ibook.idv.tw/enews/enews1531-1560/enews1536.html/ /端午/节前///////城南/一带//欠款/////皮袋子//出发//
4年前 评论
fd5556 (楼主) 4年前
讨论数量: 2
Jason990420

好像 jieba.suggest_freq 并没有提供正则式的功能, 提供自己写的两个函数, 试试.

from datetime import date,datetime
import csv
import codecs
import time
import re
import sys
import os
import jieba
from itertools import repeat

def url_split(string, sep=['https://', 'http://'], end=' '):
    if not string:
        return []
    if not any(map(str.__contains__, repeat(string), sep)):
        return [string]
    items = []
    for head in sep:
        try:
            start_ = string.index(head)
            items.append(start_)
        except:
            pass
    start = min(items)
    try:
        stop = string.index(end, start)
    except:
        stop = len(string)
    return [string[:start], string[start:stop]] + url_split(string[stop:])

def cut_all(sequence):
    result = []
    for item in sequence:
        result += [item] if any(map(str.startswith, repeat(item), sep)) else jieba.cut(item)
    return result

sep = ['https://', 'http://']
end = ' '
jieba.load_userdict('./data/dict.txt')
jieba.suggest_freq('李嘉誠',True)

file_name = 'head300.csv'

with open (file_name, 'r', encoding="utf-8") as csvfile:
    reader = csv.reader(csvfile)
    column = [row[4] for row in reader]

print(f"Total: {len(column)}")

for item in column:
    print(item)
    content_output = cut_all(url_split(item))
    print_list = '/'.join(content_output) + '\n'
    with open('seg.csv', 'a', newline='', encoding='utf-8') as csvfile:
        csvfile.write(print_list)
    print(print_list)
4年前 评论
fd5556 (楼主) 4年前
Jason990420

內定就是侦测第一个空白格, 我试了一下, OK啊

column = ['光绪年间有这样一个真实的故事。 https://ibook.idv.tw/enews/enews1531-1560/enews1536.html 江苏的贾先生,在上海租界一洋行工作,深得老板信任。 https://ibook.idv.tw/enews/enews1531-1560/enews1536.html 端午节前,老板派他去城南一带收欠款,他带上皮袋子就出发了。 ']
print(f"Total: {len(column)}")

for item in column:
    # print(item)
    content_output = cut_all(url_split(item))
    print_list = '/'.join(content_output) + '\n'
    #with open('seg.csv', 'a', newline='', encoding='utf-8') as csvfile:
    # csvfile.write(print_list)
    print(print_list)
Total: 1
光绪/年间//这样/一个/真实//故事//https://ibook.idv.tw/enews/enews1531-1560/enews1536.html/ /江苏///先生///上海/租界//洋行/工作//深得///信任//https://ibook.idv.tw/enews/enews1531-1560/enews1536.html/ /端午/节前///////城南/一带//欠款/////皮袋子//出发//
4年前 评论
fd5556 (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!