Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更简洁的IMM #23

Open
Freakwill opened this issue Apr 21, 2019 · 0 comments
Open

更简洁的IMM #23

Freakwill opened this issue Apr 21, 2019 · 0 comments

Comments

@Freakwill
Copy link

Freakwill commented Apr 21, 2019

# 更加简洁(more pythonic)的IMM算法(可用OOP)

_dictionary = {'南京', '南京市', '南京市长', '市长', '长江', '长江大桥', '江大桥', '大桥', '桥'}

def imm(text, maxlen=4):
    # inverse maximum matching method
    dictionary = _dictionary
    result = []
    index = len(text)
    while index > 0:
        m = min((index, maxlen))
        # dictionary = {e for e in _dictionary if e.endswith(text[index-1])}  # for speeding up
        for size in range(m, 0, -1):
            piece = text[(index-size):index]
            if piece in dictionary:
                result.insert(0, piece)
                index -= size
                break
        else:
            index -= 1
    return result

result = imm('南京市长江大桥')
print(result)
@Freakwill Freakwill changed the title 更简介的IMM 更简洁的IMM Apr 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant