You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
9.1 KiB

  1. import ProjectService from '@/services/project.service'
  2. export const state = () => ({
  3. projects: [],
  4. selected: [],
  5. current: {},
  6. loading: false
  7. })
  8. export const getters = {
  9. isProjectSelected(state) {
  10. return state.selected.length > 0
  11. },
  12. currentProject(state) {
  13. return state.current
  14. },
  15. getCurrentUserRole(state) {
  16. return state.current.current_users_role || {}
  17. },
  18. getFilterOption(state) {
  19. if (state.current.project_type === 'DocumentClassification') {
  20. return 'doc_annotations__isnull'
  21. } else if (state.current.project_type === 'SequenceLabeling') {
  22. return 'seq_annotations__isnull'
  23. } else if (state.current.project_type === 'Seq2seq') {
  24. return 'seq2seq_annotations__isnull'
  25. } else {
  26. return ''
  27. }
  28. },
  29. getLink(state) {
  30. if (state.current.project_type === 'DocumentClassification') {
  31. return 'text-classification'
  32. } else if (state.current.project_type === 'SequenceLabeling') {
  33. return 'sequence-labeling'
  34. } else if (state.current.project_type === 'Seq2seq') {
  35. return 'sequence-to-sequence'
  36. } else {
  37. return ''
  38. }
  39. },
  40. getImportFormat(state) {
  41. const plain = {
  42. type: 'plain',
  43. text: 'Plain text',
  44. accept: '.txt',
  45. examples: [
  46. 'EU rejects German call to boycott British lamb.\n',
  47. 'Peter Blackburn\n',
  48. 'President Obama'
  49. ]
  50. }
  51. const csv = {
  52. type: 'csv',
  53. text: 'CSV',
  54. accept: '.csv'
  55. }
  56. const json = {
  57. type: 'json',
  58. text: 'JSONL',
  59. accept: '.json,.jsonl'
  60. }
  61. const conll = {
  62. type: 'conll',
  63. text: 'CoNLL',
  64. accept: '.conll'
  65. }
  66. const excel = {
  67. type: 'excel',
  68. text: 'Excel',
  69. accept: '.xlsx'
  70. }
  71. if (state.current.project_type === 'DocumentClassification') {
  72. json.examples = [
  73. '{"text": "Terrible customer service.", "labels": ["negative"]}\n',
  74. '{"text": "Really great transaction.", "labels": ["positive"]}\n',
  75. '{"text": "Great price.", "labels": ["positive"]}'
  76. ]
  77. csv.examples = [
  78. 'text,label\n',
  79. '"Terrible customer service.","negative"\n',
  80. '"Really great transaction.","positive"\n',
  81. '"Great price.","positive"'
  82. ]
  83. excel.examples = [
  84. 'text,label\n',
  85. '"Terrible customer service.","negative"\n',
  86. '"Really great transaction.","positive"\n',
  87. '"Great price.","positive"'
  88. ]
  89. return [
  90. plain,
  91. csv,
  92. json,
  93. excel
  94. ]
  95. } else if (state.current.project_type === 'SequenceLabeling') {
  96. json.examples = [
  97. '{"text": "EU rejects German call to boycott British lamb.", "labels": [ [0, 2, "ORG"], [11, 17, "MISC"], ... ]}\n',
  98. '{"text": "Peter Blackburn", "labels": [ [0, 15, "PERSON"] ]}\n',
  99. '{"text": "President Obama", "labels": [ [10, 15, "PERSON"] ]}'
  100. ]
  101. conll.examples = [
  102. 'EU\tB-ORG\n',
  103. 'rejects\tO\n',
  104. 'German\tB-MISC\n',
  105. 'call\tO\n',
  106. 'to\tO\n',
  107. 'boycott\tO\n',
  108. 'British\tB-MISC\n',
  109. 'lamb\tO\n',
  110. '.\tO\n\n',
  111. 'Peter\tB-PER\n',
  112. 'Blackburn\tI-PER'
  113. ]
  114. return [
  115. plain,
  116. json,
  117. conll
  118. ]
  119. } else if (state.current.project_type === 'Seq2seq') {
  120. json.examples = [
  121. '{"text": "Hello!", "labels": ["こんにちは!"]}\n',
  122. '{"text": "Good morning.", "labels": ["おはようございます。"]}\n',
  123. '{"text": "See you.", "labels": ["さようなら。"]}'
  124. ]
  125. csv.examples = [
  126. 'text,label\n',
  127. '"Hello!","こんにちは!"\n',
  128. '"Good morning.","おはようございます。"\n',
  129. '"See you.","さようなら。"'
  130. ]
  131. excel.examples = [
  132. 'text,label\n',
  133. '"Hello!","こんにちは!"\n',
  134. '"Good morning.","おはようございます。"\n',
  135. '"See you.","さようなら。"'
  136. ]
  137. return [
  138. plain,
  139. csv,
  140. json,
  141. excel
  142. ]
  143. } else {
  144. return []
  145. }
  146. },
  147. getExportFormat(state) {
  148. const csv = {
  149. type: 'csv',
  150. text: 'CSV'
  151. }
  152. const json = {
  153. type: 'json',
  154. text: 'JSONL'
  155. }
  156. const jsonl = {
  157. type: 'json1',
  158. text: 'JSONL(Text label)'
  159. }
  160. if (state.current.project_type === 'DocumentClassification') {
  161. json.examples = [
  162. '{"id": 1, "text": "Terrible customer service.", "annotations": [{"id": 1, "label": 1, "user": 1}]}\n',
  163. '{"id": 2, "text": "Really great transaction.", "annotations": [{"id": 2, "label": 2, "user": 1}]}\n',
  164. '{"id": 3, "text": "Great price.", "annotations": [{"id": 3, "label": 2, "user": 1}]}'
  165. ]
  166. csv.examples = [
  167. 'id,text,label,user\n',
  168. '1,"Terrible customer service.",1,1\n',
  169. '2,"Really great transaction.",2,1\n',
  170. '3,"Great price.",2,1'
  171. ]
  172. return [
  173. csv,
  174. json
  175. ]
  176. } else if (state.current.project_type === 'SequenceLabeling') {
  177. json.examples = [
  178. '{"id": 1, "text": "EU rejects ...", "annotations": [{"id": 1, "label": 2, "start_offset": 0, "end_offset": 2, "user": 1}]}\n',
  179. '{"id": 2, "text": "Peter Blackburn", "annotations": [{"id": 2, "label": 1, "start_offset": 0, "end_offset": 15, "user": 1}]}\n',
  180. '{"id": 3, "text": "President Obama", "annotations": [{"id": 3, "label": 1, "start_offset": 10, "end_offset": 15, "user": 1}]}'
  181. ]
  182. jsonl.examples = [
  183. '{"id": 1, "text": "EU rejects ...", "labels": [[0,2,"ORG"], [11,17, "MISC"], [34,41,"ORG"]]}\n',
  184. '{"id": 2, "text": "Peter Blackburn", "labels": [[0, 15, "PERSON"]]}\n',
  185. '{"id": 3, "text": "President Obama", "labels": [[10, 15, "PERSON"]]}\n'
  186. ]
  187. return [
  188. json,
  189. jsonl
  190. ]
  191. } else if (state.current.project_type === 'Seq2seq') {
  192. json.examples = [
  193. '{"id": 1, "text": "Hello!", "annotations": [{"id": 1, "label": "こんにちは!", "user": 1}]}\n',
  194. '{"id": 2, "text": "Good morning.", "annotations": [{"id": 2, "label": "おはようございます。", "user": 1}]}\n',
  195. '{"id": 3, "text": "See you.", "annotations": [{"id": 3, "label": "さようなら。", "user": 1}]}'
  196. ]
  197. csv.examples = [
  198. 'id,text,label,user\n',
  199. '1,"Hello!","こんにちは!",1\n',
  200. '2,"Good morning.","おはようございます。",1\n',
  201. '3,"See you.","さようなら。",1'
  202. ]
  203. return [
  204. csv,
  205. json
  206. ]
  207. } else {
  208. return []
  209. }
  210. },
  211. loadSearchOptions(state) {
  212. const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
  213. return checkpoint[state.current.id] ? checkpoint[state.current.id] : { page: 1 }
  214. }
  215. }
  216. export const mutations = {
  217. setProjectList(state, payload) {
  218. state.projects = payload
  219. },
  220. createProject(state, project) {
  221. state.projects.unshift(project)
  222. },
  223. updateProject(state, project) {
  224. const item = state.projects.find(item => item.id === project.id)
  225. Object.assign(item, project)
  226. },
  227. deleteProject(state, projectId) {
  228. state.projects = state.projects.filter(item => item.id !== projectId)
  229. },
  230. updateSelected(state, selected) {
  231. state.selected = selected
  232. },
  233. resetSelected(state) {
  234. state.selected = []
  235. },
  236. setLoading(state, payload) {
  237. state.loading = payload
  238. },
  239. setCurrent(state, payload) {
  240. state.current = payload
  241. },
  242. saveSearchOptions(state, options) {
  243. const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
  244. checkpoint[state.current.id] = options
  245. localStorage.setItem('checkpoint', JSON.stringify(checkpoint))
  246. }
  247. }
  248. export const actions = {
  249. getProjectList({ commit }, config) {
  250. commit('setLoading', true)
  251. ProjectService.getProjectList()
  252. .then((response) => {
  253. commit('setProjectList', response.data)
  254. })
  255. .catch((error) => {
  256. alert(error)
  257. })
  258. .finally(() => {
  259. commit('setLoading', false)
  260. })
  261. },
  262. createProject({ commit }, project) {
  263. ProjectService.createProject(project)
  264. .then((response) => {
  265. commit('createProject', response.data)
  266. })
  267. .catch((error) => {
  268. alert(error)
  269. })
  270. },
  271. updateProject({ commit }, data) {
  272. ProjectService.updateProject(data.projectId, data)
  273. .then((response) => {
  274. commit('updateProject', response.data)
  275. })
  276. .catch((error) => {
  277. alert(error)
  278. })
  279. },
  280. deleteProject({ commit, state }, config) {
  281. for (const project of state.selected) {
  282. ProjectService.deleteProject(project.id)
  283. .then((response) => {
  284. commit('deleteProject', project.id)
  285. })
  286. .catch((error) => {
  287. alert(error)
  288. })
  289. }
  290. commit('resetSelected')
  291. },
  292. setCurrentProject({ commit }, projectId) {
  293. return ProjectService.fetchProjectById(projectId)
  294. .then((response) => {
  295. commit('setCurrent', response.data)
  296. })
  297. .catch((error) => {
  298. alert(error)
  299. })
  300. },
  301. updateCurrentProject({ commit }, data) {
  302. ProjectService.updateProject(data.projectId, data)
  303. .then((response) => {
  304. commit('setCurrent', response.data)
  305. })
  306. .catch((error) => {
  307. alert(error)
  308. })
  309. }
  310. }