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.

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