Exemplo n.º 1
0
def cut_filter_words(cutted_sentences, stopwords, use_stopwords=False):
	sentences = []
	sents = []
	for sent in cutted_sentences:
		sentences.append(sent)
		if use_stopwords:
			sents.append([word for word in jiagu.cut(sent) if word and word not in stopwords])  # 把句子分成词语
		else:
			sents.append([word for word in jiagu.cut(sent) if word])
	return sentences, sents
Exemplo n.º 2
0
def sort_sentences_by_pagerank(sentences,
                               sim_method=sentences_similarity,
                               pg_conf=None):
    """基于 pagerank 对句子进行排序

    :param sentences: list of str
        从 text 中得到的句子列表
    :param sim_method: function
        两个句子之间的相似度计算函数,输入为 sentence1 和 sentence2
    :param pg_conf: dict
        pagerank 算法相关参数
    :return:
    """
    if not pg_conf:
        pg_conf = {"alpha": 0.85}

    numbers = len(sentences)

    graph = np.zeros((numbers, numbers))

    for si in range(numbers):
        for sj in range(si, numbers):
            # sentence1 = list(sentences[si])
            sentence1 = jiagu.cut(sentences[si])
            # sentence2 = list(sentences[sj])
            sentence2 = jiagu.cut(sentences[sj])
            sim = sim_method(sentence1, sentence2)
            graph[si, sj] = sim
            graph[sj, si] = sim

    nx_graph = nx.from_numpy_matrix(graph)
    scores = nx.pagerank(nx_graph, **pg_conf)

    sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)

    sentences_sorted = []
    for index, score in sorted_scores:
        item = AttrDict(index=index, sentence=sentences[index], weight=score)
        sentences_sorted.append(item)

    return sentences_sorted
Exemplo n.º 3
0
import jiagu

# jiagu.init() # 可手动初始化,也可以动态初始化

text = '苏州的天气不错'

words = jiagu.seg(text)  # 分词
print(words)

words = jiagu.cut(text)  # 分词
print(words)

pos = jiagu.pos(words)  # 词性标注
print(pos)

ner = jiagu.ner(words)  # 命名实体识别
print(ner)

# 字典模式分词
text = '思知机器人挺好用的'
words = jiagu.seg(text)
print(words)

# jiagu.load_userdict('dict/user.dict') # 加载自定义字典,支持字典路径、字典列表形式。
jiagu.load_userdict(['思知机器人'])

words = jiagu.seg(text)
print(words)

text = '''
该研究主持者之一、波士顿大学地球与环境科学系博士陈池(音)表示,“尽管中国和印度国土面积仅占全球陆地的9%,但两国为这一绿化过程贡献超过三分之一。考虑到人口过多的国家一般存在对土地过度利用的问题,这个发现令人吃惊。”
Exemplo n.º 4
0
words = jiagu.seg(text)  # 分词
print(words)

pos = jiagu.pos(words)  # 词性标注
print(pos)

ner = jiagu.ner(words)  # 命名实体识别
print(ner)
# 独立标准模型路径
# msr:test/extra_data/model/msr.model
# pku:test/extra_data/model/pku.model
# cnc:test/extra_data/model/cnc.model
jiagu.load_model('test/extra_data/model/cnc.model')  # 使用国家语委分词标准

words = jiagu.cut('天下无贼是一部电影')

print(words)

# 分词各种模式使用方式

text = '汉服和服装、知识图谱机器人'

words = jiagu.cut(text)  # 深度学习分词
print(words)

words = jiagu.seg(text)  # 字典分词
print(words)

# jiagu.load_userdict('dict/user.dict') # 加载自定义字典,支持字典路径、字典列表形式。
jiagu.load_userdict(['知识图谱机器人'])
Exemplo n.º 5
0
import jiagu

text = '姚明(Yao Ming),1980年9月12日出生于上海市徐汇区,祖籍江苏省苏州市吴江区震泽镇,前中国职业篮球运动员,司职中锋,现任中职联公司董事长兼总经理。'
text='北京故宫是中国明清两代的皇家宫殿,旧称为紫禁城,位于北京中轴线的中心,是中国古代宫廷建筑之精华。北京故宫以三大殿为中心,占地面积72万平方米,建筑面积约15万平方米,有大小宫殿七十多座,房屋九千余间。是世界上现存规模最大、保存最为完整的木质结构古建筑之一。'

knowledge = jiagu.knowledge(text)
print(knowledge)
print(jiagu.summarize(text,1))
c=jiagu.cut('前德国国防军军官')
print(c)