Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of Python programming using finite State Machine to identify address validity

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Python programming using finite state machine to identify the effectiveness of the example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

When sending and receiving express delivery, we often enter the address manually to allow the program to intelligently identify the address. For example, the standard address is the xx number of xx Road, xx County / District, xx City, xx Province, but sometimes it can be simply written: xx number of xx Road, xx County / District, xx City, or xx number of xx Road, xx County / District, xx Province, or xx number of xx Road, xx City.

But some are not legal addresses, such as the xx number of xx Street in xx Province, or the xx number of xx District in xx Province, xx City.

So the question is, how to identify whether an address is valid, exactly, how to program to identify whether a Chinese address is valid?

Although our brain can recognize it at a glance, it is not easy for calculators to recognize it. The fundamental reason is that although the description of the address looks simple, it is still a more complex context-sensitive grammar.

For example, "xx, Beijing East Road, Shanghai, xx, East Beijing Road, Nanjing", when scanning to Beijing East Road, whether the house number behind it constitutes the correct address depends on the context, that is, the city name.

Fortunately, the context of the address is relatively simple and limited, although we can violently enumerate all provinces, municipalities, districts and streets. But the effective method is finite state machine.

Every finite state machine has a start state and an end state, as well as several intermediate states, with one state on each arc to enter the next state, for example, if the current state in the above figure is a province, if you encounter the next phrase related to the district, enter the district, if you encounter the next phrase related to the city, then enter the city.

If an address can go from the start state of the state machine, through several intermediate states of the state machine, and finally to the end state, the address is valid, otherwise it is invalid.

For example, the xx number of xx District, xx Province, xx City is an invalid address and cannot be traveled from the city to the province.

Now we do it through a simple priority state machine, and the code is annotated and easy to understand.

From enum import Enumdef isAddress (address: str)-> bool: # definition status State = Enum ("State", ["STATE_INITIAL", # start "STATE_PROVINCE", # province "STATE_CITY", # city "STATE_AREA", # district / county "STATE_STREET", # street "STATE_NUM" # "STATE_END", # end "STATE_ILLEGAL" # error status]) def toAddressType (addr_slice: str)-> State: if "in addr_slice: return State.STATE_PROVINCE elif" City "in addr_slice: return State.STATE_CITY elif" District "in addr_slice or" County "in addr_slice: return State.STATE_AREA elif" Road "in addr_slice or" Street "in addr_slice: return State.STATE_STREET elif" number "in addr_slice: return State.STATE_NUM else: return State.STATE_ILLEGAL # defines state transfer transfer = {# can be converted to provincial or municipal State.STATE_INITIAL: {State.STATE_PROVINCE State.STATE_CITY,}, # provinces can be transferred to cities or districts and counties State.STATE_PROVINCE: {State.STATE_CITY, State.STATE_AREA,}, # cities can be transferred to districts or streets State.STATE_CITY: {State.STATE_AREA, State.STATE_STREET,} # districts and counties can change streets State.STATE_AREA: {State.STATE_STREET,}, # streets can change numbers or terminate State.STATE_STREET: {State.STATE_NUM, State.STATE_END,} # can only be transferred to State.STATE_NUM: {State.STATE_END,} st = State.STATE_INITIAL for ch in address: current_state = toAddressType (ch) if current_state not in transfer [st]: return False st = current_state return st in [State.STATE_STREET, State.STATE_NUM State.STATE_END] if _ name__ ='_ main__': address1 = ["Jiangsu Province", "Suzhou", "Wuzhong District", "Zhongshan North Road", "208"] address2 = [" Suzhou "," Wuzhong District "," Zhongshan North Road "," 208"] address3 = ["Suzhou", "Wujiang District", "Zhongshan North Road", "208"] address4 = [" Suzhou " Address5 = [Suzhou City, Zhongshan North Road] assert isAddress (address1) assert isAddress (address2) assert isAddress (address3) assert isAddress (address5) assert isAddress (address4) = False

Instead of segmenting the entire address string, the address is written directly in the form of a list, mainly to illustrate the implementation and application of the state machine. the above code can only ensure that the address is valid in format, not the address is true and valid. if you want to judge it to be true and valid, it is necessary to establish a hash table for all provinces, cities, districts, counties and streets in the country, and the house number can be expressed in a range. And then make a state transfer judgment.

The transfer of the above code is a hash table, which is equivalent to enumerating all the cases of correct transfer, and it exhausts the state that needs to be escaped corresponding to any input in either case.

Thank you for reading this article carefully. I hope the article "example Analysis of Python programming using finite State Machine to identify address validity" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report